Posts: 3
Threads: 1
Joined: Jun 2020
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
Posts: 1,358
Threads: 2
Joined: May 2019
We won't do your homework for you, but are happy to help. What have you written so far? Please post your effort.
Posts: 353
Threads: 13
Joined: Mar 2020
Remember to also use proper code tags while posting a thread - see BBCode to know more
Posts: 3
Threads: 1
Joined: Jun 2020
Jun-09-2020, 03:22 PM
(This post was last modified: Jun-09-2020, 08:42 PM by Yoriz.)
(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
Posts: 1,358
Threads: 2
Joined: May 2019
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.
Posts: 3
Threads: 1
Joined: Jun 2020
Jun-10-2020, 07:59 AM
(This post was last modified: Jun-10-2020, 08:06 AM by buran.)
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
Posts: 741
Threads: 122
Joined: Dec 2017
Jun-10-2020, 08:07 AM
(This post was last modified: Jun-10-2020, 08:21 AM by DPaul.)
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
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'.
Posts: 1
Threads: 0
Joined: Jun 2020
Jun-10-2020, 11:54 AM
(This post was last modified: Jun-10-2020, 11:57 AM by Yoriz.)
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.
Posts: 353
Threads: 13
Joined: Mar 2020
@ alfredharper, no one does a student's work, we only help them by showing clues/hints etc.
|