DEV Community

daftyw
daftyw

Posted on

Challenge of making a diamond of numbers

Recently, I got some message from my fellow about a quiz that she has to involve in a next couple of weeks. The quiz itself is actually has 5 assignments but I have remember just one.

I guess this is one simple assignment for any CS student. But as a long time software developer I think it one hell of a quest for me.

The assignment content here.

Make a python program that input any number then put that number in one diamond like in an example:

Input number: 5
    1
   121
  12321
 1234321
123454321
 1234321
  12321
   121
    1
Enter fullscreen mode Exit fullscreen mode

This is my code in gist

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited
def main():
    n = int(input("Input any number : "))
    print_diamond(n)

def print_diamond(n):
    s = str(int('1'*n)**2)
    f = '%'+str(n-1)+'s'
    for i in map(int,s):print f%s[:i-1]+s[-i:]

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
daftyw profile image
daftyw

Nice to having you on this post πŸ‘‹πŸ™‚

Collapse
 
codefinity profile image
Manav Misra

And nice to have you join our community and partcipatd! πŸ€“