Skip to content

Instantly share code, notes, and snippets.

@idarthjedi
Created December 22, 2021 06:12
Show Gist options
  • Select an option

  • Save idarthjedi/d1ab68e84f11babd81b214bcddb94c0d to your computer and use it in GitHub Desktop.

Select an option

Save idarthjedi/d1ab68e84f11babd81b214bcddb94c0d to your computer and use it in GitHub Desktop.
Coding exercise to build an alphabet rangoli - I'm sure there is a much more efficient way to do this, but I was also watching Transcendence with my daugther. :-)
def print_alpha_rangoli(size):
alpha = [x for x in 'abcdefghijklmnopqrstuvwxyz']
output = []
display = []
# determine the total width of the alphabet rangoli
width = (size + (size - 1)) + ((size + (size - 1)) - 1)
# for each of the letters in reverse order (starting inside out)
for lin in range(size -1, -1, -1):
design = []
# build the individual line top down
output.append(str(alpha[lin]))
for int_lin in range(len(output) -1, -1, -1):
design.append(output[int_lin])
if int_lin != len(output) - 1:
design.insert(0, output[int_lin])
# pull the line together
display.append(('-'.join(design)).center(width, '-'))
# built to the center, now reverse to the bottom
for lin in range(len(display) -2, -1, -1):
display.append(display[lin])
print('\n'.join(display))
if __name__ == '__main__':
while True:
n = int(input('Please enter a number of letters between 2 and 26: '))
if n < 2 or n > 26:
continue
else:
print_alpha_rangoli(n)
break
@idarthjedi
Copy link
Author

i.e. >> 12

----------------------l----------------------
--------------------l-k-l--------------------
------------------l-k-j-k-l------------------
----------------l-k-j-i-j-k-l----------------
--------------l-k-j-i-h-i-j-k-l--------------
------------l-k-j-i-h-g-h-i-j-k-l------------
----------l-k-j-i-h-g-f-g-h-i-j-k-l----------
--------l-k-j-i-h-g-f-e-f-g-h-i-j-k-l--------
------l-k-j-i-h-g-f-e-d-e-f-g-h-i-j-k-l------
----l-k-j-i-h-g-f-e-d-c-d-e-f-g-h-i-j-k-l----
--l-k-j-i-h-g-f-e-d-c-b-c-d-e-f-g-h-i-j-k-l--
l-k-j-i-h-g-f-e-d-c-b-a-b-c-d-e-f-g-h-i-j-k-l
--l-k-j-i-h-g-f-e-d-c-b-c-d-e-f-g-h-i-j-k-l--
----l-k-j-i-h-g-f-e-d-c-d-e-f-g-h-i-j-k-l----
------l-k-j-i-h-g-f-e-d-e-f-g-h-i-j-k-l------
--------l-k-j-i-h-g-f-e-f-g-h-i-j-k-l--------
----------l-k-j-i-h-g-f-g-h-i-j-k-l----------
------------l-k-j-i-h-g-h-i-j-k-l------------
--------------l-k-j-i-h-i-j-k-l--------------
----------------l-k-j-i-j-k-l----------------
------------------l-k-j-k-l------------------
--------------------l-k-l--------------------
----------------------l----------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment