Created
December 5, 2020 04:08
-
-
Save rawinng/2b5eee64391709af13375b425930bd9f to your computer and use it in GitHub Desktop.
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
## Diamon | |
def main(): | |
n = int(input(f"Input any number : ")) | |
a = build_diamond(n) | |
result = [] | |
for i in a: | |
result.append([ str(n) if n > 0 else " " for n in i ]) | |
for i in result: | |
print(''.join(i)) | |
def build_diamond(num): | |
""" | |
Build a diamond stack | |
""" | |
mat = [] | |
line_pattern = build_revert_array_apply(num, lambda i: num - i) | |
for n in line_pattern: | |
mat.append(build_revert_array_apply(num, lambda i: i - n)) | |
return mat | |
def build_revert_array(n): | |
""" | |
Build revert array like 5 = 123454321 | |
""" | |
a = [] | |
for i in range(1, n + 1): | |
a.append(i) | |
for i in range(n - 1, 0, -1): | |
a.append(i) | |
return a | |
def build_revert_array_apply(n, op): | |
""" | |
Build revert array and apply some function | |
""" | |
a = build_revert_array(n) | |
b = [] | |
for i in a: | |
b.append(op(i)) | |
return b | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment