Skip to content

Instantly share code, notes, and snippets.

@snydergd
Created July 22, 2018 23:42
Show Gist options
  • Save snydergd/4b9f405cb5f2d6682519b15c0fe74891 to your computer and use it in GitHub Desktop.
Save snydergd/4b9f405cb5f2d6682519b15c0fe74891 to your computer and use it in GitHub Desktop.
Just some experimentation that I wanted to save. No point to make it private.
"""
Looking for a way to reduce a list of 9 digits into a sum that is unique if the digits are those between 1 and 9 inclusively.
This is for a solution to a Sudoku problem, but it was fun to mess around with functional programming.
"""
import itertools, functools, inspect
def findAllSimilar(individualOperation, aggregationOperation):
# Note: doing range inverse to make sure operation is commutative
expected = functools.reduce(aggregationOperation, map(individualOperation, range(9, 0, -1)))
return map(lambda x: str(x[1]),
filter(lambda x: x[0] == expected,
map(lambda x: (
functools.reduce(aggregationOperation,
map(individualOperation, x)
),
x
),
itertools.combinations_with_replacement(range(1,10), 9)
)
)
)
print(
str.join('\n',
map(lambda x: str(inspect.getsource(x[1])),
filter(lambda x: x[0] == 1,
map(lambda x: (sum(1 for everything in findAllSimilar(x[0],x[1])), x[0], x[1]),
[
((lambda x: x**5), (lambda x,y: x+y)),
((lambda x: x), (lambda x,y: x/y)),
]
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment