Last active
April 14, 2020 11:44
-
-
Save gideon-maina/bc71112dd95217e608faf521ddfcb705 to your computer and use it in GitHub Desktop.
make a door mat , challenge from hackerrank
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 re | |
def get_next_multiple_greater_than_n_for_x(n, x): | |
return n + (x-n %x) | |
def get_next_multiple_less_than_n_for_x(n, x): | |
for i in range(n-1, 0, -1): | |
if i % x == 0: | |
return i | |
def make_mat(length, width): | |
h_length = length//2 | |
h_width = width//2 | |
n_multiple = get_next_multiple_less_than_n_for_x(h_length, 3) | |
n_multiple_incrementing = get_next_multiple_greater_than_n_for_x(0,3) | |
parts = [] | |
for i in range(width): | |
if i < h_width: | |
ss = '-'*length | |
part_1 = ss[:n_multiple] | |
part_2 = '' | |
part_3 = ss[-n_multiple:] | |
to_replace = ss[n_multiple:len(ss)-n_multiple] | |
for i in range(0, len(to_replace), 3): | |
part_2 += '.|.' | |
n_multiple = get_next_multiple_less_than_n_for_x(n_multiple, 3) | |
total = part_1 + part_2 + part_3 | |
parts.append(total) | |
elif i == h_width: | |
# Since C will always be at the center get left side and replace | |
# last three chars with WEL and with the right part first 3 chars with OME | |
ss = '-'*length | |
part_1 = ss[:h_length] | |
part_1 = re.sub("---$", "WEL", part_1) | |
part_2 = 'C' | |
part_3 = ss[-h_length:] | |
part_3 = re.sub("^---", "OME", part_3) | |
total = part_1 + part_2 + part_3 | |
parts.append(total) | |
elif i > h_width: | |
part_1 = ss[:n_multiple_incrementing] | |
part_2 = '' | |
part_3 = ss[-n_multiple_incrementing:] | |
to_replace = ss[n_multiple_incrementing:len(ss)-n_multiple_incrementing] | |
for i in range(0, len(to_replace), 3): | |
part_2 += '.|.' | |
total = part_1 + part_2 + part_3 | |
parts.append(total) | |
n_multiple_incrementing = get_next_multiple_greater_than_n_for_x(n_multiple_incrementing, 3) | |
print('\n'.join(p for p in parts)) | |
if __name__ == "__main__": | |
print("Enter width and length space delimitered, length is width * 3 e.g 9 27 | |
width, length = map(int, input().split()) | |
make_mat(length, width) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample