# HackerRank Solution: Python Alphabet Rangoli [3 Methods]

Written By - Bashir Alam

## Question: Python Alphabet Rangoli

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 the 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 , 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--------``````

## Possible Solutions

Now let us consider the possible solutions to the given problem. The following code is already given in the hacker rank editor:

``````def print_rangoli(size):

if __name__ == '__main__':
n = int(input())
print_rangoli(n)``````

Now let us move toward the possible solutions and explain each of them.

### Solution-1: Using the string module

Let us first solve the problem using the string module.

``````from string import ascii_lowercase as alc
def print_rangoli(size):
#exception case, normal method would give '-a-'
if size == 1:
print(alc)
return

alphaSlice = [*alc[:size]]
rangoli = []

#iterate through alphabet, get progressive smaller chunks, insert to list
for i in range(size):
# isolate center letter for easy padding
center = alphaSlice[i]
halfLine = '-'.join([y for y in alphaSlice[i+1:]])
line = halfLine[::-1]+f'-{center}-'+halfLine
rangoli.append(line)

# every line will have the same width as the longest line
#duplicate and mirror list
mirrorRangoli = [*rangoli[::-1],*rangoli[1:]]

for line in mirrorRangoli:
if __name__ == '__main__':
n = int(input())
print_rangoli(n)``````

The solution defines a function "`print_rangoli(size)`" which generates and prints a rangoli pattern of size "`size`". It starts by handling an exception case where size is `1`, in which it simply prints the first character of the lowercase ASCII alphabet. It then slices the lowercase ASCII alphabet to the specified size, creates an empty list to store the rangoli pattern, and iterates through the sliced alphabet to form lines for the pattern. Each line is constructed by concatenating the reverse of a progressive smaller chunk of the sliced alphabet, the center letter, and another progressive smaller chunk of the sliced alphabet. The padding for each line is set to the length of the longest line and the final rangoli pattern is mirrored and printed with each line centered by the specified padding.

### Solution-2: Using for loop

Let us now use the for loop to solve the problem:

``````def print_rangoli(size):
alphbets = "abcdefghijklmnopqrstuvwxyz"
# get char from alphabets, "-char-", next this in center and fill
total_size = ((4*size)-3)
for i in range(1, size+1):
# Form alphabets
alpha_string = ""
for j in range(i):
letter = alphbets[size-i+j]
alpha_string = alpha_string.center((2*j+1), letter)
alpha_string_hiphen = "-".join(alpha_string)
print(alpha_string_hiphen.center(total_size, "-"))
for i in range(size-1, 0, -1):
alpha_string = ""
for j in range(i):
letter = alphbets[size-i+j]
alpha_string = alpha_string.center((2*j+1), letter)
alpha_string_hiphen = "-".join(alpha_string)
print(alpha_string_hiphen.center(total_size, "-"))

if __name__ == '__main__':
n = int(input())
print_rangoli(n)``````

Similar to the previous one, this code also defines a function "`print_rangoli(size)`" which generates and prints a rangoli pattern of size "`size`". It starts by defining a string of lowercase ASCII alphabets and calculates the total size of the pattern. Then, it has two for loops to form the top half and bottom half of the pattern respectively. In each loop, it uses nested for loops to form a string of alphabets of the current row's size by using the center function to insert the current alphabet letter between its corresponding letters in the previous row. The formed alphabet string is then joined with hyphens to form a single string for the current row, which is then centered with hyphens to fit the total size of the pattern. Finally, the formed string for each row is printed.

### Solution-3: Using `join()` method

We will now use the `.join()` method to solve the given problem.

``````def print_rangoli(size: int):
pattern = '-'.join(chr(ch) for ch in reversed(range(ord('a'), ord('a') + size)))
layers = []
for limit in range(1, size * 2, 2):
layer = f'{pattern[:limit]}{pattern[:limit - 1][::-1]}'
layers.append(f'{layer:-^{size * 4 - 3}}')
print(layers[-1])
print('\n'.join(reversed(layers[:-1])))

if __name__ == '__main__':
n = int(input())
print_rangoli(n)``````

This code implements the printing of a Rangoli pattern of a given size '`n`'. The Rangoli pattern consists of characters arranged in a specific way, forming a symmetrical design. The pattern is created using a string, '`pattern`', which is built by joining characters in the reverse order of their ASCII values, starting from '`a`' and ending at the `nth` character in the alphabet. The pattern is printed in layers, each layer having one more character than the previous one, until the largest layer, which has twice the size of '`n`' characters. The layers are created by slicing the '`pattern`' string and concatenating it with its reverse, with the appropriate number of characters for each layer. The layers are then stored in a list, 'layers', and printed in reverse order, except for the last layer, which is printed directly. Finally, the layers are concatenated and printed, creating the complete Rangoli pattern.

## Summary

In this short article, we discussed how we can solve the Alphabet Rangoli using various methods. We covered three different methods to solve the problem and explained each one.

Problem on Hacker Rank: Python Alphabet Rangoli [Strings]

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation. For any other feedbacks or questions you can either use the comments section or contact me form.