Reputation: 11
alphabet = 'abcdefghijklmnopqrstuvwxyz'
alphalist = list(alphabet)
size = int(input('enter size:'))
dash = 2*size
iter = size
list_reverse = []
rangoli_final = []
while not (dash<0 and iter <0):
dash -=2
string = '-'*dash + '-'.join(alphalist[iter-1:size])
if string != '':
list_reverse.append(string)
iter -=1
for rangoli in list_reverse:
rangoli_final.append(rangoli[:-1] +rangoli[::-1])
for val in rangoli_final[:len(rangoli_final)-1]:
print(val)
for mirror in rangoli_final[::-1]:
print(mirror)
It works fine in vscode but there were apparently some hidden tests for which it did not work.
Upvotes: 0
Views: 62
Reputation: 13
I am not sure which problem you are solving but if that's this one:
https://www.hackerrank.com/challenges/alphabet-rangoli/problem?isFullScreen=true
then your solution wouldn't pass. For size = 5 it should be:
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
but when I run yours I get
--------e--------
------d-e-d------
----c-d-e-d-c----
--b-c-d-e-d-c-b--
a-b-c-d-e-d-c-b-a
--b-c-d-e-d-c-b--
----c-d-e-d-c----
------d-e-d------
--------e--------
'a' should be in the center.
For the problem linked above here is the code:
def print_rangoli(size):
width = 4 * (size - 1) + 1
a_ord = ord("a")
upper_rows = []
for i in range(1, size + 1):
start = a_ord + size - 1
stop = a_ord + size - i - 1
step = -1
left = [chr(j) for j in range(start, stop, step)]
right = left[-2::-1]
upper_rows.append(("-".join(left + right)).center(width, "-"))
lower_rows = upper_rows[-2::-1]
print("\n".join(upper_rows + lower_rows))
if __name__ == "__main__":
n = int(input())
print_rangoli(n)
And the steps are as follows (if size = 5):
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
then I sum them, join with '\n' and print.
Hope this helps!
Upvotes: 0