Friday, June 28, 2019

Alphabet Rangoli - Hacker Rank 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).
Input Format
Only one line of input containing , 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--------
Alphabet Rangoli - Hacker Rank Solution
Problem Setter's code:
n = int(raw_input())
for i in range(n):
    s = "-".join(chr(ord('a')+n-j-1) for j in range(i+1))
    print((s+s[::-1][1:]).center(n*4-3, '-'))

for i in range(n-1):
    s = "-".join(chr(ord('a')+n-j-1) for j in range(n-i-1))
    print((s+s[::-1][1:]).center(n*4-3, '-'))

5 comments:

  1. import string
    def print_rangoli(size):
    # your code goes here
    alpha = string.ascii_lowercase
    a = []
    for i in range(n):
    s = "-".join(alpha[i:n])
    a.append((s[::-1] + s[1:]).center(4 * n - 3, "-"))
    print('\n'.join(a[::-1]))
    print('\n'.join(a[1:]))
    if __name__ == '__main__':
    n = int(input())
    print_rangoli(n)

    ReplyDelete
    Replies
    1. import string
      def print_rangoli(size):
      alpha = string.ascii_lowercase
      a = []
      for i in range(n):
      s = "-".join(alpha[i:n])
      a.append((s[::-1] + s[1:]).center(4 * n - 3, "-"))
      print('\n'.join(a[::-1]))
      print('\n'.join(a[1:]))
      if __name__ == '__main__':
      n = int(raw_input())
      print_rangoli(n)

      Delete
  2. def print_rangoli(size):
    # your code goes here
    alpha = chr(96+size)
    stri = alpha
    width = (size*4)-3
    inte = ord(alpha)
    if(inte <= 97):
    print(alpha)
    else:
    print(alpha.center(width,"-"))
    for i in range(1,size):
    inte = inte - 1
    stri = stri + "-" + chr(inte)
    revstri = stri[-2::-1]
    print((stri + revstri).center(width,"-"))
    ab = stri + revstri
    l = len(ab)
    l_stri = ab[0:int(l/2)]
    for i in range(size-1):
    l_stri = l_stri + l_stri[-4::-1]
    print(l_stri.center(width,"-"))
    leng = len(l_stri)
    l_stri = l_stri[0:int(leng/2)]
    if __name__ == '__main__':
    n = int(input())
    print_rangoli(n)

    ReplyDelete

Powered by Blogger.