For my assignment, I’m told to write a program that takes height H as an input and display the integer values from 1 to input N in the following rules
Rule 1. One column can hold H numbers at maximum.
Rule 2. Print the numbers in the given order as below. (Example: H=4)
- Begin at top left.
- Go right once. Then, change direction to bottom-left. Keep going until wall-hit.
If wall-hit, go step 3. - If botton direction is available, go bottom once. Otherwise, go right once. Then,
change direction to upper-right. Keep going until wall-hit. If wall-hit, go step 2.
Rule 3. Use a field width of 3 characters per number (and no extra blank between numbers).
example
H = int(input('Display height?: '))
N = int(input('Up to which number to print?: '))
direction = 'right'
row = 0
col = 0
mylist = []
for x in range(H):
mylist.append([])
i = 1
while i <= N:
if direction == 'right':
mylist[row].append(i)
i += 1
col += 1
mylist[row].append(i)
if row == 0:
direction = 'down'
elif row == H - 1:
direction = 'up'
elif direction == 'down':
col -= 1
row += 1
mylist[row].append(i)
if row != H - 1:
col -= 1
row += 1
mylist[row].append(i)
elif row == H - 1:
direction = 'right'
elif col == 0:
row += 1
mylist[row].append(i)
direction = 'up'
elif direction == 'up':
col += 1
row -= 1
mylist[row].append(i)
if row == 0:
direction = 'right'
else:
col += 1
row -= 1
mylist[row].append(i)
i += 1
# Print the nested list
for row in mylist:
print(row)
I figured out that I’m supposed to use nested list and update my col and vol. However, I never quite got the answer right. Please help me.
New contributor
noob123431243124124 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.