Python Forum

Full Version: Please help me to write code for this
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
We won't do your homework for you, but are happy to help. What have you written so far? Please post your effort.
Remember to also use proper code tags while posting a thread - see BBCode to know more
(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
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.
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
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
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.
@alfredharper, no one does a student's work, we only help them by showing clues/hints etc.