HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank Alphabet Rangoli problem solution in python | python problem solution

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

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

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

Problem solution in Python 2 programming.

from copy import deepcopy
N = int(raw_input())
row = 2 * (N - 1) + 1
col = (N - 1) * 4 + 1
arr = ['-'] * col
res = []
for _ in xrange(row): res.append(deepcopy(arr))
mr = row / 2
mc = col / 2
for i in xrange(mr + 1):
ch = ord('a') + i
for j in xrange(N - i):
res[mr - i][mc - 2 * j] = chr(ch + j)
res[mr - i][mc + 2 * j] = chr(ch + j)
res[mr + i][mc - 2 * j] = chr(ch + j)
res[mr + i][mc + 2 * j] = chr(ch + j)
for r in res:
print ''.join(r)

Problem solution in Python 3 programming.

def print_rangoli(size):
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, "-"))

Problem solution in pypy programming.

import string
def print_rangoli(size):
alpha = string.ascii_lowercase
n = size
L = []
for i in range(n):
s = "-".join(alpha[i:n])
L.append((s[::-1]+s[1:]).center(4*n-3, "-"))
print('n'.join(L[:0:-1]+L))

Problem solution in pypy3 programming.

import string
alpha = string.ascii_lowercase

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


Leave a Reply

10 Best Artificial Intelligence Software|artificial intelligence tools 5 nft games to earn money | Best NFT games for earn crypto Earn Money From Minting NFTs| How to mint NFT for free Top 10 Things You Need To Know About Python List | python lists functions 10 Popular PHP frameworks for web developers| best php frameworks 12 Tips On How To Become a Python Developer | python For beginner 12 Best Nodejs Frameworks for App Development in 2022 how to create google web stories, Steps to create web stories Top 10 Features in Angular 13 Every Developer Should Know | Angular 13 Features 10 Best Angular UI Libraries | angular ui components | angular Project 10 Best Web Development Frameworks in 2022 Frontend & Backend 18 Best Open-Source and Free Database Software | best database software for beginners Top 10+ Best Java IDEs & Online Java Compilers | best java ide for beginners top 10 besic to andvance java books |java books for beginer Top 5 Themes For Blogger, professional blogger theme download BEST Python Courses Online,Top 10 Courses to Learn Python in 2022 Top 13 python libraries for data science