Skip to content

Instantly share code, notes, and snippets.

@jcla1
Created April 20, 2013 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcla1/5426864 to your computer and use it in GitHub Desktop.
Save jcla1/5426864 to your computer and use it in GitHub Desktop.
Solution to checkio.org ATM problem. Functional Python FTW!
import math
def total_cost(amount):
return int(math.ceil(amount + 0.5 + 0.01 * amount))
def reduce_balance(balance, amount):
if amount < balance:
balance -= total_cost(amount)
return balance
def checkio(data):
balance, amounts = data
amounts = filter(lambda x: x % 5 == 0 and x > 0, amounts)
return reduce(reduce_balance, amounts, balance)
if __name__ == '__main__':
assert checkio([120, [10 , 20, 30]]) == 57, 'First'
# With one Insufficient Funds, and then withdraw 10 $
assert checkio([120, [200 , 10]]) == 109, 'Second'
#with one incorrect amount
assert checkio([120, [3, 10]]) == 109, 'Third'
assert checkio([120, [200, 119]]) == 120 , 'Fourth'
assert checkio([120, [120, 10, 122, 2, 10, 10, 30, 1]]) == 56, "It's mixed all base tests"
print('All Ok')
@Sandyy8
Copy link

Sandyy8 commented Apr 22, 2013

Hi, was trying to run the program..says global name reduce is not defined..

@vedgar
Copy link

vedgar commented Jul 12, 2016

@Sandyy8 You have to import it from functools in Py3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment