Alphabet Rangoli in Python - HackerRank Solution

Alphabet Rangoli in Python - HackerRank Solution
Alphabet Rangoli in Python - HackerRank Solution



Problem :


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



Input Format :

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

Constraints :

  • 0 < N < 27

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 :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Alphabet Rangoli in Python - HackerRank Solution
def print_rangoli(size):
    # your code goes here
    # Alphabet Rangoli in Python - HackerRank Solution START
    width  = size*4-3
    string = ''

    for i in range(1,size+1):
        for j in range(0,i):
            string += chr(96+size-j)
            if len(string) < width :
                string += '-'
        for k in range(i-1,0,-1):    
            string += chr(97+size-k)
            if len(string) < width :
                string += '-'
        print(string.center(width,'-'))
        string = ''

    for i in range(size-1,0,-1):
        string = ''
        for j in range(0,i):
            string += chr(96+size-j)
            if len(string) < width :
                string += '-'
        for k in range(i-1,0,-1):
            string += chr(97+size-k)
            if len(string) < width :
                string += '-'
        print(string.center(width,'-'))
        
    # Alphabet Rangoli in Python - HackerRank Solution END

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





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
11 Comments
  • Unknown
    Unknown Tuesday, January 04, 2022

    def print_rangoli(size):
    n = size
    b=''
    letters=''
    c= []
    for i in range(n):
    letters+=chr(97+i)

    for i in letters[::-1]:
    b+="-"+i
    c.append(b+b[:-1][::-1])
    c[-1]=c[-1].strip('-')

    for i in c:
    print(i.center(len(c[-1]),'-'))

    for i in c[::-1][1:]:
    print(i.center(len(c[-1]),'-'))


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

    • Anonymous
      Anonymous Friday, April 29, 2022

      your implementation is beautiful..... i broke it down and was able to understand every line of code....thanks alot...i have been schooled

  • Unknown
    Unknown Friday, January 28, 2022

    def print_rangoli(n):
    A="abcdefghijklmnopqrstuvwxyz"
    a=A[:n]
    b=""
    c=[]
    k=1
    o=n+(n-1)
    p=o+(o-1)
    for i in range(0,n):
    if (i==0):
    b=a[i]
    c.append(b.center(p,"-"))
    else:
    x=b[:k]
    y=x[::-1]
    k+=2
    b=x+"-"+a[i]+"-"+y
    if (len(b)<=p):
    c.append(b.center(p,"-"))
    for i in range(len(c)-2,-1,-1):
    c.append(c[i])
    for i in range(0,len(c)):
    print(c[i])
    if __name__ == '__main__':
    n = int(input())
    print_rangoli(n)

  • Unknown
    Unknown Tuesday, February 01, 2022

    def print_rangoli(size):
    # your code goes here
    n = size
    c = 'a'
    m = 3*n + (n-3)
    w1 = m//2 + 1
    w2 = m//2
    #Creating a list of characters
    ch = []
    for _ in range(n):
    ch.append(c)
    c = chr(ord(c)+1)
    #Top part
    rev = ch[::-1]
    for i in range(1,n):
    print(('-{}'*i).format(*rev).rjust(w1, '-')+('-{}'*(i-1)).format(*ch[n-i+1:]).ljust(w2, '-'))
    #Middle belt
    if n == 1:
    print(ch[0])
    else:
    mid = (2*n)-3
    print(ch[len(ch)-1]+('-{}'*(mid)).format(*rev[1:], *ch[1:len(ch)-1])+'-'+ch[len(ch)-1])
    #Bottom part
    for i in range(n-1,0,-1):
    print(('-{}'*i).format(*rev).rjust(w1, '-')+('-{}'*(i-1)).format(*ch[n-i+1:]).ljust(w2, '-'))
    if __name__ == '__main__':
    n = int(input())
    print_rangoli(n)

  • Nguyen Khanh Toan
    Nguyen Khanh Toan Saturday, April 23, 2022

    def chu(a, k):
    s=''
    for i in range(1,k):
    s += chr(97-i+a) +'-'
    for i in range(k,2*k):
    s += chr(97+i+a-2*k) +'-'
    return s[:-1]
    #khoi tao
    n = 27
    while n not in range(1,27):
    n=int(input())
    #print India carpet
    w=len(chu(n,n))
    for j in range(1,n*2):
    print(chu(n,abs(abs(n-j)-n)).center(w,'-'))

  • Anonymous
    Anonymous Tuesday, May 03, 2022

    def print_rangoli(size):
    abh = [chr(x) for x in range(97,97+size)]
    width = (size-1)*4+1

    string = []
    for i in range(size-1,-1,-1):
    lst = []
    string.append(abh[i])
    lst = string[::-1]
    final = string+lst[1:]
    print(("-".join(final)).center(width,"-"))
    #print(("-".join(string)+"-"+"-".join(reversed(string[0:-1]))).center(width,"-"))

    new = abh[::-1]
    for j in range(size,1,-1):
    string = new[0:j-1]
    print(("-".join(string)+"-"+"-".join(reversed(string[0:-1]))).center(width,"-"))

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

    • Anonymous
      Anonymous Friday, May 27, 2022

      Give me for that 10 lines code for that program

    • Anonymous
      Anonymous Friday, May 27, 2022

  • HACKER@CRACKER
    HACKER@CRACKER Saturday, June 18, 2022

    def print_rangoli(size):
    # your code goes here
    import string
    alpha=string.ascii_lowercase
    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))


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

  • Anonymous
    Anonymous Friday, July 22, 2022

    why is there 4*n-3

    • Anonymous
      Anonymous Wednesday, August 10, 2022

      It is his way of calculating the width of the rangoli // in other words the number of columns in the rangoli

Add Comment
comment url