Alphabet Rangoli Python Hackerrank solution

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.)
Admin

Alphabet Rangoli Python Hackerrank solution

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)

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.