''' Created on Jan 5, 2016 @author: Brett Paufler Copyright Brett Paufler CAN KILL Was playing with generators, inspired a weekly code snippet In theory a change maker, it works But the next step would be to optimize for length, which I'm guessing is a algorythm problem, so maybe not worth the effort to pursue ''' from collections import OrderedDict #coins = [100, 50, 25, 10, 5, 1] #coins = OrderedDict([(100, 0), (50, 2), (35, 0), (10, 10), (5, 5), (1, 5)]) #{100:20, 50:2, 25:4, 10:10, 5:5, 1:5}) def change(n, coins): '''Given int, return list of parts making whole.''' out = OrderedDict(((k, 0) for k, _ in coins.items())) start = OrderedDict(coins) finish = OrderedDict(coins) till = sum(k*v for k, v in coins.items()) print 'Till(%s) Covers(%s):? %s' % (till, n, till >= n) assert n <= till while any(k for k, v in finish.items() if v > 0 and k <= n): c = max(k for k, v in finish.items() if v > 0 and k <= n) n -= c finish[c] -= 1 out[c] += 1 print 'Start, ', start print 'Finish,', finish print 'OUT: ', out return out coins = OrderedDict([(100, 0), (50, 2), (35, 0), (10, 10), (5, 5), (1, 5)]) coins = OrderedDict([(19, 10), (7, 10), (3, 10), (1, 10)]) for n in [70, 71, 72]: #0, 34, 101, 432, 10000]: print print n print 'OUT: %s' % change(n, coins) lst = [x for x in []] gen = (x for x in []) print bool(lst), lst print bool(gen), gen print gen.__len() from itertools import islice slc = list(islice((x for x in []), 1)) print bool(slc), slc print slc.__len__()