HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Drawing inverted triangles

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
drawinginvertedtriangles

Problem

I was working through another question here and have hacked up an answer:

def get_rows():
""" Get height of triangle from user"""

    while True:
        rows = input('Enter the number of rows:  ')
        if 3 <= rows <= 33:
            return rows

def triangle(rows, invert=True):
""" Print the outline of a triangle, of variable size. 
    Print out a regular or inverted version of the triangle as necessary """

    if invert:
        height = -1 * rows
    else:
        height = 0

    # inner padding for min height (3)
    inner_buffer = [0, 1, 3]
    while len(inner_buffer) <= rows:
        inner_buffer.append(inner_buffer[-1]+2)

    level = 0        
    while level <= rows:
        outer_padding = ' '*(rows - abs(height))

        if height == 0:
            print(outer_padding + '*')
        else:
            inner_padding = ' '*( inner_buffer[ abs(height) ] )
            print(outer_padding + '*' + inner_padding + '*')

        height += 1
        level += 1


At this stage I am working to re-implement this two ways using: recursion and itertools.

But I am looking at this and wonder if it would be a good place to use decorators. If someone is happy to implement one triangle (either regular or inverted, I don't mind) I would love to see the difference in the 3 implementations.

Solution

Decorators are a tool that you use when you have many functions that share a behaviour, for example a particular calling convention or treatment of results. See the Python Decorator Library on the Python wiki for some examples, and this answer on Stack Overflow for a tutorial.

Here you only have two functions, so it doesn't seem likely that decorators will be helpful. Use the right tool for the job!

Anyway, comments on your code.

-
No docstrings! What do these functions do and how do you call them?

-
The else: continue in get_rows() is unnecessary.

-
The regular keyword argument to triangle is ignored.

-
Your function triangle seems very long for what it's doing. Why not use Python's format language to do the padding? Like this:

def triangle(n, invert=True):
    """Print inverted triangle of asterisks (with `n`+1 rows) to standard output.
    Print it the right way up if keyword argument `invert=False` is given.
    """
    for i in reversed(xrange(n + 1)) if invert else xrange(n + 1):
        print('{:>{}}{:>{}}'.format('*', n - i + 1, '*' if i else '', 2 * i))

Code Snippets

def triangle(n, invert=True):
    """Print inverted triangle of asterisks (with `n`+1 rows) to standard output.
    Print it the right way up if keyword argument `invert=False` is given.
    """
    for i in reversed(xrange(n + 1)) if invert else xrange(n + 1):
        print('{:>{}}{:>{}}'.format('*', n - i + 1, '*' if i else '', 2 * i))

Context

StackExchange Code Review Q#24372, answer score: 4

Revisions (0)

No revisions yet.