Last active
June 29, 2020 04:00
-
-
Save StefanRudvin/efdf70ad2b6d3beccd9a88e889bcabc7 to your computer and use it in GitHub Desktop.
Simplify
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, math | |
def solve(n): | |
""" | |
Begin by creating base case, then programatically generate result rows by slicing the first x elements | |
from the base case, add repeated terminal value and add the reverse of the previous. | |
""" | |
abc_chars = string.ascii_uppercase[1:n][::-1] | |
base = abc_chars + string.ascii_uppercase[0] + abc_chars[::-1] | |
ret = [] | |
for i in range(0, n - 1): | |
str = base[0:i] + base[i] * (n-1-i) | |
ret.append(str + base[i] + str[::-1]) | |
print(*[x for x in ret], sep='\n') | |
print(base) | |
print(*[x for x in ret[::-1]], sep='\n') | |
solve(3) | |
print("") | |
solve(4) | |
print("") | |
solve(5) | |
print("") | |
solve(6) | |
print("") | |
solve(10) | |
print("") | |
solve(26) | |
def solve_lambda(n): | |
abc_chars = string.ascii_uppercase[1:n][::-1] | |
base = abc_chars + string.ascii_uppercase[0] + abc_chars[::-1] | |
t = lambda i: base[0:i] + base[i] * (n-1-i) | |
f = lambda x: t(x) + base[x] + t(x)[::-1] | |
ret = [f(x) for x in range(0, n-1)] | |
print(*[x for x in ret], sep='\n') | |
print(base) | |
print(*[x for x in ret[::-1]], sep='\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment