Created
April 16, 2020 14:59
-
-
Save gideon-maina/c1c1621b61bd8a945d5d24280242c38d to your computer and use it in GitHub Desktop.
Create an alphabet rangoli hacker rank challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
def next_less_even_number(n): | |
for i in range(n-1, 0, -1): | |
if i % 2 == 0: | |
return i | |
def next_greater_even_number(n, x=2): | |
return n + (x-n %x) | |
def print_rangoli(size): | |
n = size | |
first_n_chars = string.ascii_lowercase[:n] | |
s_parts = [] | |
width = (n * 2) -1 | |
next_less_even = next_less_even_number(n*2) | |
next_greater_even = next_greater_even_number(0) | |
for i in range(1, width+1): | |
if i < n: | |
part_1 = '-'*next_less_even | |
part_3 = '-'*next_less_even | |
chars_to_use = first_n_chars[n - i :] | |
mid_char = chars_to_use[len(chars_to_use)//2] | |
part_2 = mid_char | |
if len(chars_to_use) > 1: | |
sub_part_2b = '-'.join(i for i in chars_to_use) | |
sub_part_2a = '-'.join(i for i in reversed(chars_to_use)) | |
sub_part_2a = sub_part_2a[:-1] # Remove last char | |
part_2 = sub_part_2a + sub_part_2b | |
next_less_even = next_less_even_number(next_less_even) | |
elif i == n: | |
chars_to_use = first_n_chars | |
part_1 = '-'.join(i for i in reversed(chars_to_use)) | |
part_1 = part_1[:-1] # Remove lats char since it starts part 2 | |
part_2 = '-'.join(i for i in chars_to_use) | |
part_3 = '' | |
elif i > n: | |
part_1 = '-'*next_greater_even | |
part_3 = '-'*next_greater_even | |
chars_to_use = first_n_chars[i-n:] | |
mid_char = chars_to_use[len(chars_to_use)-1:] | |
part_2 = mid_char | |
if len(chars_to_use) < n: | |
sub_part_2a = '-'.join(i for i in reversed(chars_to_use)) | |
sub_part_2a = sub_part_2a[:-1] # Remove last char | |
sub_part_2b = '-'.join(i for i in chars_to_use) | |
part_2 = sub_part_2a + sub_part_2b | |
next_greater_even = next_greater_even_number(next_greater_even) | |
s = part_1 + part_2 + part_3 | |
s_parts.append(s) | |
print('\n'.join(s for s in s_parts)) | |
if __name__ == '__main__': | |
n = int(input()) | |
print_rangoli(n) |
Author
gideon-maina
commented
Apr 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment