Python Forum
Please help me to write code for this
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help me to write code for this
#1
Hi,

can anyone please help me to write code for this one

5 5 5 5 5
5 4 4 4 4
5 4 3 3 3
5 4 3 2 2
5 4 3 2 1

kindly help, matrix value changes with Value of N
if n is 6 then

6 6 6 6 6 6
6 5 5 5 5 5
6 5 4 4 4 4
6 5 4 3 3 3
6 5 4 3 2 2
6 5 4 3 2 1

kindly help
Reply
#2
We won't do your homework for you, but are happy to help. What have you written so far? Please post your effort.
Reply
#3
Remember to also use proper code tags while posting a thread - see BBCode to know more
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
(Jun-09-2020, 12:57 PM)jefsummers Wrote: We won't do your homework for you, but are happy to help. What have you written so far? Please post your effort.

Hi,

i have tried and its not that much good code since i have started to learn python
and my code works only when n = 4

please guide me to refine so that i can take one variable an keep reducing that and print
n = int(input("value of n:"))
for i in range(n):
    print((n), end= ' ')
print()
print(n, end =' ')
for i in range(n-1):
    print((n-1), end= ' ')
print()
print((n),end = ' ')
print((n-1), end = ' ')
for i in range(n-2):
    print((n-2), end= ' ')
print()
print(n, end = ' ')
print(n-1, end = ' ')
print(n-2, end = ' ')
print(n-3, end =' ')
thank you

Hi,
This is the program which i written and i want to use loop with in the loop i keeps reducing like

and out put is

value of n:4
Output:
4 4 4 4 4 3 3 3 4 3 2 2 4 3 2 1
and when is n is greater 4 output will be like

value of n:5
Output:
5 5 5 5 5 5 4 4 4 4 5 4 3 3 3 5 4 3 2 2 5 4 3 2 1
kindly help me to write loop

Thank you
Reply
#5
To get you started -
The for statement in line 2 goes from 0 to one less than n (see how to use range in the docs). Better would be for i in range(n,0,-1):

You then don't want to print n in each column each row. Rather, if we count the columns and rows starting with 0 (typical Python), you want to subtract the row value from each column starting with the column value

Easiest way I can see is to create a list of n values of n. Print that. Next use that list and subtract 1 from every element starting with element 1. Print that. Next use the same list and subtract 1 from every element starting with element 2. And so on.
Reply
#6
Hi,

Thank you jef, i got my code working in early morning 2 AM
Thank you for help and support
code:

n = int(input("value of n:"))
for i in range(n, 0,-1):#5, 4, 3, 2,1
for j in range(n,0,-1): #5, 4, 3, 2, 1
if j <= i:
print(i, end=' ')
else:
print(j, end=' ')
print()

Thanks and Regards
Vij
Reply
#7
Hi,
Found this exercise amusing.
The post above (jefsummers) suggests numbers in lists, an exercise in indexing.
You may also try string slicing, possibly a bit easier, if your number < 10 :-)
Without comprehensions, the number of codelines for the list solution would be 8 and the slicing solution 6.
So way less than what you proposed originally.
I do believe that this is some solid beginner's exercise :-)
Paul

n = int(input("value of n:"))
for i in range(n, 0,-1):#5, 4, 3, 2,1
    for j in range(n,0,-1): #5, 4, 3, 2, 1
        if j <= i:
            print(i, end=' ')
        else:
            print(j, end=' ')
    print()
You actually found a third solution Smile
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#8
Ahah. You wanted to find someone who will solve this problem for you? It can take enough time and I think that no one will do this for free) Only suggestions. Maybe you can find some help **removed**. Some of these websites can provide you with additional programming help.
Reply
#9
@alfredharper, no one does a student's work, we only help them by showing clues/hints etc.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a dict code in python ariellea88 4 1,016 Oct-31-2023, 08:45 AM
Last Post: buran
  how to write code demon_calcifer 1 1,626 Nov-16-2021, 04:09 PM
Last Post: Larz60+
  Can someone please help me write the code Ram 8 3,350 Feb-08-2021, 08:21 AM
Last Post: Serafim
  Write pseudo code for a class assignment Scrimshot 3 3,402 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Write a code to output in alphabetical order AbdelaliPython 1 4,627 Jan-19-2018, 09:03 PM
Last Post: j.crater
  how to write a code for term that can't begin or end with a stopword desul 3 3,793 Mar-18-2017, 03:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020