vighnesh Sathya
vighnesh Sathya

Reputation: 11

Can anyone help me with what hidden test cases I am missing for the alphabet rangoli?

    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

Answers (1)

user582956
user582956

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):

  1. Define the width by counting the number of characters per line - width = 4 * (size - 1) + 1 and upper_rows empty list. For size = 5 upper_rows should result in

--------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

  1. Get the unicode code point for 'a' - a_ord = ord("a")
  2. In the loop 'left' variable is set to ['e'], ['e', 'd'], ['e', 'd', 'c'], etc. and 'right' is the remaining part respectively being [], ['d'], ['d', 'e'], etc.
  3. Then I join the added left + right with "-", center it with "-" with the "width" defined above and append it to upper_rows list.
  4. After the loop lower_rows is created using the upper_rows. It looks like this:

--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

Related Questions