0

I'm trying to create a pyramid that looks like the picture below(numberPyramid(6)), where the pyramid isn't made of numbers but actually a black space with the numbers around it. The function takes in a parameter called "num" and which is the number of rows in the pyramid. How would I go about doing this? I need to use a for loop but I'm not sure how I implement it. Thanks!

                                 666666666666
                                 55555  55555
                                 4444    4444
                                 333      333
                                 22        22
                                 1          1
Sergey
  • 10,228
  • 2
  • 36
  • 46
swimdawg12
  • 63
  • 1
  • 1
  • 6
  • 1
    Can you provide some code samples in your question please? This will make your question more likely to attract good answers rather than down-votes – ScottMcGready Sep 06 '15 at 20:01

2 Answers2

3
def pyramid(num_rows, block=' ', left='', right=''):
    for idx in range(num_rows):
        print '{py_layer:{num_fill}{align}{width}}'.format(
            py_layer='{left}{blocks}{right}'.format(
                left=left,
                blocks=block * (idx*2),
                right=right),
            num_fill=format((num_rows - idx) % 16, 'x'),
            align='^',
            width=num_rows * 2)

This works by using python's string format method in an interesting way. The spaces are the string to be printed, and the number used as the character to fill in the rest of the row.

Using the built-in format() function to chop off the leading 0x in the hex string lets you build pyramids up to 15.

Sample:

In [45]: pyramid(9)
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1

Other pyramid "blocks" could be interesting:

In [52]: pyramid(9, '_')
999999999999999999
88888888__88888888
7777777____7777777
666666______666666
55555________55555
4444__________4444
333____________333
22______________22
1________________1

With the added left and right options and showing hex support:

In [57]: pyramid(15, '_', '/', '\\')
ffffffffffffff/\ffffffffffffff
eeeeeeeeeeeee/__\eeeeeeeeeeeee
dddddddddddd/____\dddddddddddd
ccccccccccc/______\ccccccccccc
bbbbbbbbbb/________\bbbbbbbbbb
aaaaaaaaa/__________\aaaaaaaaa
99999999/____________\99999999
8888888/______________\8888888
777777/________________\777777
66666/__________________\66666
5555/____________________\5555
444/______________________\444
33/________________________\33
2/__________________________\2
/____________________________\
CivFan
  • 10,079
  • 9
  • 34
  • 55
  • Thank you for answering! I was wondering if you could please explain the formatting method in the print statement? And also, is it possible to just have the num in the parameter and not the block = ' ', left = ' '...etc. and still make the code work? – swimdawg12 Sep 06 '15 at 23:52
  • 1
    @swimdawg12 Google "python string format". Also, you can have it without the other parameters, but they're no harm there, you can use the function without them. You should study the function and string formatting until you understand how to remove them yourself. – Markus Meskanen Sep 07 '15 at 00:10
1

First the code:

max_depth = int(raw_input("Enter max depth of pyramid (2 - 9): "))
for i in range(max_depth, 0, -1):
    print str(i)*i + " "*((max_depth-i)*2) + str(i)*i

Output:

(numpyramid)macbook:numpyramid joeyoung$ python numpyramid.py
Enter max depth of pyramid (2 - 9): 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1

How this works:

Python has a built-in function named range() which can help you build the iterator for your for-loop. You can make it decrement instead of increment by passing in -1 as the 3rd argument.

Our for loop will start at the user supplied max_depth (6 for our example) and i will decrement by 1 for each iteration of the loop.

Now the output line should do the following:

  • Print out the current iterator number (i) and repeat it itimes.
  • Figure out how much white space to add in the middle.
    • This will be the max_depth minus the current iterator number, then multiply that result by 2 because you'll need to double the whitespace for each iteration
  • Attach the whitespace to the first set of repeated numbers.
  • Attach a second set of repeated numbers: the current iterator number (i) repeated itimes

When your print characters, they can be repeated by following the character with an asterisk * and the number of times you want the character to be repeated. For example:

>>> # Repeats the character 'A' 5 times
... print "A"*5
AAAAA
Joe Young
  • 5,456
  • 3
  • 23
  • 24