Skip to content
the poor coder

Hackerrank - Alphabet Rangoli

Poor Man
Published
Updated
Reading time
4 min read
On this page

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:

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

Text
5

Sample Output

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

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
Text
string.center(), string.ljust(), string.rjust()
  • Adding list.
Text
[1,2] + [3,4] = [1,2,3,4]
  • Reversing of list.
Text
reverse = originallist[::-1]
  • List slicing.

Explanation#

Text
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

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

Text
alpha = "abcdefghijklmnopqrstuvwxyz"

We use list comprehension to store the first n alphabet.

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

Then

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

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

Text
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

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

Now if we directly loop our items list

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

Output

Text
['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.

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

This gives us

Text
['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.

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

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

Text
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

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

Keep going

2020

Hackerrank - Bus Station Solution

There are groups of friends, and each group is numbered from 1 to . The th group contains people. They live near a bus stop, and only a single bus operates on this route. An empty bus arrives at the bus stop and all the…

2020

Hackerrank - K Candy Store Solution

Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to buy K candies. In how many different ways can he purchase K candies if there are infinite candies…

2020

Hackerrank - Diwali Lights Solution

On the eve of Diwali, Hari is decorating his house with a serial light bulb set. The serial light bulb set has N bulbs placed sequentially on a string which is programmed to change patterns every second. If at least one…

ESC