patternpythonMinor
Building a number pyramid
Viewed 0 times
numberbuildingpyramid
Problem
I have been working a number pyramid program. This is a question from Y. Daniel Laing's Introduction to programming using Python (which is based on Python 3.2). The question is from chapter 5, and it is number 5.19 in programming exercises portion of the chapter. The question is stated as follows:
Write a program that prompts the user to enter an integer from 1 to 15
and displays it as the following sample run:
I don't have the image, but it is something like this:
Enter the number of lines: 3
The solution I have come up with is this:
If anyone has a better solution that would be simpler, please let me know.
Write a program that prompts the user to enter an integer from 1 to 15
and displays it as the following sample run:
I don't have the image, but it is something like this:
Enter the number of lines: 3
1
2 1 2
3 2 1 2 3The solution I have come up with is this:
p = eval(input("Enter the number of lines: "))
num = 0
y = 1
while num = 1:
y -= 1
line += str(y+1) + " "
while y < num:
line += str(y + 2) + " "
y +=1
print(format(line, "^80s"))
num +=1If anyone has a better solution that would be simpler, please let me know.
Solution
Like 200_success said, the key is in thinking at the higher level. But I think his approach can be simplified.
Lets first define a
And now we can use this to print out the lines:
Giving us the following output:
The core of the problem is now solved. The only thing left is formatting. For this we can write another function, which formats each number in list into a three-character box (with
Now we're almost there. Just need to tie it all together:
Lets first define a
row function, which simply calculates a list of numbers in an n'th row of our output:# When n=3, returns [3, 2, 1, 2, 3]
def row(n):
return list(reversed(range(2, n+1))) + list(range(1, n+1))And now we can use this to print out the lines:
for n in range(1, 5):
print(row(n))Giving us the following output:
[1]
[2, 1, 2]
[3, 2, 1, 2, 3]
[4, 3, 2, 1, 2, 3, 4]The core of the problem is now solved. The only thing left is formatting. For this we can write another function, which formats each number in list into a three-character box (with
"%3s"), and also appends a certain amount of padding to make sure lines are aligned like a pyramid:def formatted_row(n, num_rows):
padding = ' ' * (num_rows-n)
return padding + ''.join(["%3s" % x for x in row(n)])Now we're almost there. Just need to tie it all together:
# When n=3, returns [3, 2, 1, 2, 3]
def row(n):
return list(reversed(range(2, n+1))) + list(range(1, n+1))
def formatted_row(n, num_rows):
padding = ' ' * (num_rows-n)
return padding + ''.join(["%3s" % x for x in row(n)])
num_lines = eval(input("Enter the number of lines: "))
for n in range(1, num_lines+1):
print(formatted_row(n, num_lines))Code Snippets
# When n=3, returns [3, 2, 1, 2, 3]
def row(n):
return list(reversed(range(2, n+1))) + list(range(1, n+1))for n in range(1, 5):
print(row(n))[1]
[2, 1, 2]
[3, 2, 1, 2, 3]
[4, 3, 2, 1, 2, 3, 4]def formatted_row(n, num_rows):
padding = ' ' * (num_rows-n)
return padding + ''.join(["%3s" % x for x in row(n)])# When n=3, returns [3, 2, 1, 2, 3]
def row(n):
return list(reversed(range(2, n+1))) + list(range(1, n+1))
def formatted_row(n, num_rows):
padding = ' ' * (num_rows-n)
return padding + ''.join(["%3s" % x for x in row(n)])
num_lines = eval(input("Enter the number of lines: "))
for n in range(1, num_lines+1):
print(formatted_row(n, num_lines))Context
StackExchange Code Review Q#32050, answer score: 4
Revisions (0)
No revisions yet.