You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank - Alphabet Rangoli

Hackerrank - Alphabet Rangoli

Beeze Aal
Beeze Aal

You are given an integer, . Your task is to print an alphabet rangoli of size . (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  alphabet letter (in alphabetical order).

Input Format

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

Constraints

Output Format

Print the alphabet rangoli in the format explained above.

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

def print_rangoli(n):
    alpha = "abcdefghijklmnopqrstuvwxyz"
    data = [alpha[i] for i in range(n)]
    items = list(range(n))
    items = items[:-1]+items[::-1]
    for i in items:
        temp = data[-(i+1):]
        row = temp[::-1]+temp[1:]
        print("-".join(row).center(n*4-3, "-"))


n = int(input())
print_rangoli(n)

Answer Breakdown

Required knowledge

  • List comprehension
  • Text alignment
string.center(), string.ljust(), string.rjust()
  • Adding list.
[1,2] + [3,4] = [1,2,3,4]
  • Reversing of list.
reverse = originallist[::-1]
  • List slicing.

Explanation

n = int(input())

We take the size of rangoli from the user.

Let us assume we want a rangoli of size 5. So n = 5.

Our desired output is

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

The following line doesn't need an explanation I suppose.

alpha = "abcdefghijklmnopqrstuvwxyz"

We use list comprehension to store the first n alphabet.

data = [alpha[i] for i in range(n)]
#['a', 'b', 'c', 'd', 'e']

Then

items = list(range(n)) 
#[0, 1, 2, 3, 4]

Following code reverses our items list and combines it with our original list

items = items+items[::-1]
#[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]

However our desired pattern of rangoli doesn't repeat the center item. So we can either remove the first item from reversed list or last item from our original list.

Hence our code for this part becomes like this

items = items[:-1]+items[::-1]
#[0, 1, 2, 3, 4, 3, 2, 1, 0]

Now if we directly loop our items list

for i in items:
    start_index = i+1
    print(data[-start_index:])

Output

['e']
['d', 'e']
['c', 'd', 'e']
['b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
['b', 'c', 'd', 'e']
['c', 'd', 'e']
['d', 'e']
['e']

As you can see we have got the right part of our pattern. To get the left part we just have to print our original list along with its reverse. Also since the center part will appear twice which we don't want, we will remove the first item from original list.

for i in items:
    start_index = (i+1)
    original =data[-start_index:]
    reverse = original[::-1]
    row = reverse+original[1:]
    print(row)

This gives us

['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']

Perfect! Now we just have to combine the row items with "-" and add extra "-" to fill the spaces to achieve our rangoli pattern.

Let's join the row items and see how it looks like.

for i in items:
    start_index = (i+1)
    original =data[-start_index:]
    reverse = original[::-1]
    row = reverse+original[1:]
    row = "-".join(row)
    print(row)

Notice, I replaced print(row) with print("-".join(row)). And our output is given below.

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

Now, last part is to pad our spaces with "-". We will use center function for this.

The width of a rangoli of size 5 is 5*4-3 which is n*4-3

Let's add it to our code.

for i in items:
    start_index = (i+1)
    original =data[-start_index:]
    reverse = original[::-1]
    row = reverse+original[1:]
    row = "-".join(row)
    width = n*4-3
    row = row.center(width, "-")
    print(row)

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

And that's it. Our code is complete.

Leave a comment below if you have any confusion.