Last active
March 16, 2017 22:42
-
-
Save StandoffVenus/90ac3289a3291817b352fe7117a62f1f to your computer and use it in GitHub Desktop.
Given the Fizz Buzz challenge, create code to handle large integer input (= no recursion) that avoids the use of "i" and dynamic code functions (e.g. eval, exec, etc.)
This file contains hidden or 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
# gets our global methods (such as reduce or eval - though, eval's not used here) | |
gb = vars(globals()['__bu' + chr(105) + 'lt' + chr(105) + 'ns__']) | |
nput = gb[chr(105) + 'nput'] | |
prnt = gb['pr' + chr(105) + 'nt'] | |
nt = gb[chr(105) + 'nt'] | |
# _mport functools -> make to d_ct -> get reduce() from module | |
reduce = vars(gb['__' + chr(105) + 'mport__']('functools'))['reduce'] | |
# behaves as ternary operator | |
def tern(con, a, b): | |
(b, a)[bool(con)]() | |
# emulates for loop by reduce method | |
def loop(n, func): | |
# sorta bad for memory, but works for large values | |
reduce(func, [1] * (n + 1)) | |
# handles output (parameters n and x to handle reduce calls) | |
def oStr(n, x): | |
_s = '' | |
# modulo 3 passed | |
def _setF(): | |
nonlocal _s | |
_s += 'F' + chr(105) + 'zz' | |
# modulo 5 passed | |
def _setB(): | |
nonlocal _s | |
_s += 'Buzz' | |
# neither modulo test passed | |
def _setN(): | |
nonlocal _s | |
nonlocal n | |
_s = n | |
tern(n % 3 == 0, _setF, lambda: None) | |
tern(n % 5 == 0, _setB, lambda: None) | |
# last two tests may have made no change to _s, so we set the value to the current number | |
tern(_s == '', _setN, lambda: None) | |
prnt(_s) | |
return n + x | |
loop(nt(nput()), oStr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment