/FiveProb.py Secret
Last active
August 25, 2017 04:28
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
""" | |
http://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
""" | |
from functools import reduce | |
from operator import add | |
def sol1(seq): | |
ans = [0 , 0 , 0] | |
for it in seq: | |
ans[0] += it | |
i = 0 | |
while i < len(seq): | |
ans[1] += seq[i] | |
i += 1 | |
ans[2] = (lambda f , *cond: f(f , *cond)) \ | |
(lambda f , l , acc: f(f , l[1::] , acc + l[0]) if l else acc , seq , 0) | |
return (all([e == sum(seq) for e in ans]) , ans) | |
def sol2(a , b): | |
return reduce(add , zip(a , b)) | |
def sol3(l): | |
return reduce(lambda acc , i: acc + [acc[i - 2] + acc[i - 1]] , range(2 , l) , [0 , 1]) | |
def sol4(ls): | |
return ''.join(reversed(sorted([str(e) for e in ls]))) | |
def sol5(exp , po): | |
inserted = lambda exp , po , elt: exp[:po:] + elt + exp[po::] | |
solved = lambda op: sol5(inserted(exp , po , op) , po + len(op) + 1) | |
return reduce(add , map(solved , ('+' , '-' , ''))) \ | |
if po < len(exp) else ([exp] if eval(exp) == 100 else []) | |
print(sol1([1 , 3 , 5 , 7 , 9])) | |
print(sol2([1 , 2 , 3] , ['a' , 'b' , 'c'])) | |
print(sol3(100)) | |
print(sol4([50 , 2 , 1 , 9])) | |
print(sol5("123456789" , 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment