HackerRank Python: Alphabet Rangoli Solution

Table of Contents

Question

You are given an integer, N. Your task is to print an alphabet rangoli of size N. (Rangoli is a form of Indian folk art based on creation of patterns.)

Different sizes of alphabet rangoli are shown below:

#size 3

----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----

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

#size 10

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

The center of the rangoli has the first alphabet letter a, and the boundary has the Nth alphabet letter (in alphabetical order).

Function Description

Complete the rangoli function in the editor below.

rangoli has the following parameters:

  • int size: the size of the rangoli

Returns

  • string: a single string made up of each of the lines of the rangoli separated by a newline character (\n)

Input Format

Only one line of input containing size, the size of the rangoli.

Constraints

0 < size < 27

Sample Input

5

Sample Output

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

Solution

				
					def print_rangoli(size):
    # your code goes here
    import string
    design = string.ascii_lowercase

    L = []
    for i in range(n):
        s = "-".join(design[i:n])
        L.append((s[::-1]+s[1:]).center(4*n-3, "-"))
        
    print('\n'.join(L[:0:-1]+L))
if __name__ == '__main__':
    n = int(input())
    print_rangoli(n)
				
			

1. `import string`: This line imports the `string` module, which provides a set of useful string operations and constants.

2. `design = string.ascii_lowercase`: This line assigns the lowercase alphabet letters to the `design` variable. The `string.ascii_lowercase` constant from the `string` module represents all the lowercase letters from ‘a’ to ‘z’.

3. `L = []`: This line initializes an empty list `L` that will store the lines of the rangoli.

4. `for i in range(n):`: This loop iterates from `0` to `n-1`, where `n` is the size of the rangoli.

5. `s = “-“.join(design[i:n])`: This line creates a string `s` by joining a subset of `design` from index `i` to `n-1` using the “-” separator. It forms a part of the rangoli line, where the characters are separated by “-“.

6. `L.append((s[::-1]+s[1:]).center(4*n-3, “-“))`: This line adds a line of the rangoli to the list `L`. It takes the reversed string `s` and concatenates it with a slice of `s` excluding the first character. Then, it centers the resulting string in a line of width `4*n-3` by filling the remaining space with “-” characters.

7. `print(‘\n’.join(L[:0:-1]+L))`: This line prints the alphabet rangoli. It first joins all the lines in `L` in reverse order (excluding the first line) with a newline character ‘\n’ as the separator. Then, it concatenates this reversed part of the list with the original `L` list, representing the full rangoli pattern. Finally, it prints the entire rangoli pattern.

8. `if __name__ == ‘__main__’:`: This condition checks if the code is being executed as the main program.

9. `n = int(input())`: This line takes user input to determine the size `n` of the rangoli.

10. `print_rangoli(n)`: This line calls the `print_rangoli` function with the given size `n` to generate and print the alphabet rangoli.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment