i need output like this:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
this is what i can do but it's not right
size=int(input())
for row in range(1,size 1):
for col in range(1,size 1):
print(row,end=' ')
print(col)
only use forloop
CodePudding user response:
Single loop:
size=int(input())
for i in range(1, size 1):
print(' '.join([str(max([i, j])) for j in range(1, size 1)]))
CodePudding user response:
Here is an easy to understand example :
num = int(input("Input a number : "))
for i in range(1, num 1):
row = ""
for j in range(1, num 1):
row = " " str(j if j > i else i)
print(row)
CodePudding user response:
You can do:
size = int(input())
for i in range(1, size 1):
print(' '.join(str(max(i, j)) for j in range(1, size 1)))
Example for size of 9:
1 2 3 4 5 6 7 8 9
2 2 3 4 5 6 7 8 9
3 3 3 4 5 6 7 8 9
4 4 4 4 5 6 7 8 9
5 5 5 5 5 6 7 8 9
6 6 6 6 6 6 7 8 9
7 7 7 7 7 7 7 8 9
8 8 8 8 8 8 8 8 9
9 9 9 9 9 9 9 9 9
It however doesn't print nicely for double digits:
1 2 3 4 5 6 7 8 9 10
2 2 3 4 5 6 7 8 9 10
3 3 3 4 5 6 7 8 9 10
4 4 4 4 5 6 7 8 9 10
5 5 5 5 5 6 7 8 9 10
6 6 6 6 6 6 7 8 9 10
7 7 7 7 7 7 7 8 9 10
8 8 8 8 8 8 8 8 9 10
9 9 9 9 9 9 9 9 9 10
10 10 10 10 10 10 10 10 10 10
So you can find the number of digits, and pad the strings:
import math
max_n = 10
digits = int(math.log10(max_n)) 1
for i in range(1, max_n 1):
print(' '.join(str(max(i, j)).rjust(digits) for j in range(1, max_n 1)))
1 2 3 4 5 6 7 8 9 10
2 2 3 4 5 6 7 8 9 10
3 3 3 4 5 6 7 8 9 10
4 4 4 4 5 6 7 8 9 10
5 5 5 5 5 6 7 8 9 10
6 6 6 6 6 6 7 8 9 10
7 7 7 7 7 7 7 8 9 10
8 8 8 8 8 8 8 8 9 10
9 9 9 9 9 9 9 9 9 10
10 10 10 10 10 10 10 10 10 10
CodePudding user response:
I make a list for each row. If the row number is i
then the row starts with i
i's. After that it counts up to the given size.
size = int(input())
for row in range(1, size 1):
lst = [row] * row list(range(row 1, size 1))
print(' '.join([str(i) for i in lst]))
Output for size = 5
:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
CodePudding user response:
A solution following your code's logic:
for row in range(1,size 1):
cnt = 1
for col in range(1,size 1):
if(cnt < row):
printNum = row
else:
printNum = cnt
cnt = cnt 1
if(printNum > 9):
print(printNum,end=' ')
else:
print(printNum,end=' ')
print("\n")
CodePudding user response:
Yet another variation:
size = input()
width = len(size)
size = int(size) 1
for i in range(1, size):
print(*(f'{max(i, j):>{width}}' for j in range(1, size)))
CodePudding user response:
This does it:
size=int(input())
for col in range(1,size 1):
for row in range(1,size 1):
if row <= col:
print(col, end=' ')
else:
print(row, end=' ')
print()
It's not the most elegant answer but it works for any size.
CodePudding user response:
Walrus to rescue. For Python 3.8
(size := int(input("Input a number : ")) 1)
for i in range(1, size):
print(' '.join(str(max(i, j)) for j in range(1, size)))
Result:
Enter a number : 5
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
CodePudding user response:
I just did this for fun... it's a recursive lambda that outputs correctly and includes a list comprehension for loop.
square_numbers = lambda val: '1 ' if val == 1 else '\n'.join([i str(val) ' ' for i in square_numbers(val-1).split('\n')] [(str(val) ' ') * val])
test
print('\n\n'.join([square_numbers(val) for val in range(1,int(input()))]))
output
...
1 2 3 4 5 6
2 2 3 4 5 6
3 3 3 4 5 6
4 4 4 4 5 6
5 5 5 5 5 6
6 6 6 6 6 6
...
CodePudding user response:
# write a program to traverse every element of the two-dimensional array in Python.
dt = [ [1, 2, 3, 4, 5], [2, 2, 3, 4, 5], [3, 3, 3, 4, 5 ], [4, 4, 4, 4, 5], [5, 5, 5, 5, 5] ]
# Use for loop to print the entire elements of the two dimensional array.
for x in dt: # outer loop
for i in x: # inner loop
print(i, end = " ") # print the elements
print()
This code will give you output like this:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
but you have to input the array manually