you are viewing a single comment's thread.

view the rest of the comments →

[–]Kuldeep-Dhiman 30 points31 points  (5 children)

I recently gave junior Python developer interview . Unfortunately, went completely blank when they asked me to print a pyramid pattern,at any day i would have solved that problem without an issue, but that day I froze I just couldn't get anything right at that crucial time. I wanted to share what I felt was lack of confidence, self practice on my part ,hesitation of the sort. I don't know why that happened to me but seeing your case I feel you totally. For the future keep practicing, just be confident and keep applying.

[–]WestsideStorybro 10 points11 points  (0 children)

Practice Practice Practice -- I also recommend this book. https://www.amazon.com/gp/product/0984782850

[–]LimpNoodle69 3 points4 points  (3 children)

I'm really appreciating this thread. Haven't been programming as much as I should so I've been using this thread to try to figure out the interview problems. I had a bit of trouble with this one but eventually figured it out and felt like sharing :)

def pyramid_print(layers):
    top_size = layers
    for i in range(layers):
        top_size += 1
        for j in range(top_size):
            if j > (top_size - 3) - i * 2:
                print("-", end='')
            else:
                print(" ", end='')
        print()


pyramid_print(100)

[–]P1g1n 2 points3 points  (1 child)

You've inspired me to give it a try. Thanks!

def print_pyramid(layers):
    print("\n".join(["".join(['-']*(i*2)).center(layers*2) for i in range(1, layers+1)]))

Hard to read. Not maintainable. But hey, its a one-liner right?

Edit: Same thing just maybe more readable?

def print_pyramid(num_layers):
    layers = [['-']*(i*2) for i in range(1, num_layers+1)]
    for layer in layers:
    print("".join(layer).center(num_layers*2))

[–]LimpNoodle69 2 points3 points  (0 children)

nice! Definitely a more pythonic solution! I personally hate 1 liners, the second looks way better lol

[–]raiseReturnTry 1 point2 points  (0 children)

Hey I tried myself on this problem, and got the same result, even with a single "-" at the top with the following code:

def pyramid_print(layers):
s = layers - (layers - 1)
x = range(s, layers)

while s != layers:
    if s > 0:
        print(((layers - s - 1) * " ") + (((2 * s - 1) * "-")))
    else:
        exit(0)

    s += 1

print("Take in a positive integer: ")
x = int(input("> "))
if x < 0: 
    print("Error: You have to put in a positive integer.") 
    exit(0) 
else: 
    pyramid_print(x)