Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Last active October 12, 2019 17:49
Show Gist options
  • Save jrjames83/94ca6767efba484ec350b9f8d992c0ee to your computer and use it in GitHub Desktop.
Save jrjames83/94ca6767efba484ec350b9f8d992c0ee to your computer and use it in GitHub Desktop.
Python Coin Change Problem (Recursive) - https://www.youtube.com/watch?v=8JwdenBGmEo&feature=youtu.be
# YouTube Walk Through: https://www.youtube.com/watch?v=8JwdenBGmEo&feature=youtu.be
def return_change(to_return, coins = [.01, .05, .10, .25, 1.0, 5.0]):
flag = None
for c in coins:
if c == to_return: return c
if c < to_return:
flag = c
temp_balance = round(to_return - flag, 2)
return [flag] + [return_change(temp_balance)]
result = return_change(4.33) # Highly nested iterable
print(result)
# Recursive function to flatten an iterable with arbitrary levels of nesting.
# https://stackoverflow.com/a/14491059/3182843
def flatten(L):
for item in L:
try:
yield from flatten(item)
except TypeError:
yield item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment