d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [python] Giving Out Change Money
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Nov 4 2013 09:04am
Hey :)
I'd like some some critique on an algorithm I was tasked to write. It does yield the right answer, but due to inexperience I have no clue whether the implementation is any good beyond that.
Here's the exercise:

Quote
Write a function f which given the change amount and a list of all existing coins in a curreny, returns all possible ways to give out the change money. Example: with change == 6 and coins == [1, 5, 10, 25, 27], f(change, coins) should return [[5, 1], [1, 1, 1, 1, 1, 1]].

Here's my code:

Code

def changeCombs(change, currencyCoins, memo = None, firstCall = True):
if not memo:
memo = {0:[[]]}

if change in memo:
return memo[change]

allcombs = []
for coin in currencyCoins:
if change - coin >= 0:
for subcomb in changeCombs(change - coin, currencyCoins, memo, False):
comb = [coin] + subcomb
comb.sort()
comb.reverse()
if comb not in allcombs:
allcombs.append(comb)

memo[change] = allcombs
if firstCall:
return sorted(allcombs, key = lambda lst: len(lst))
return allcombs


Thanks!
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Nov 4 2013 09:35am
forgot to mention: change is supposed to be an integer, coins a list of integers. otherwise you can get into serious floating point trouble.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 4 2013 01:14pm
why dont you start by providing the big O analysis?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 4 2013 02:52pm
edit: nevermind, misread the original question

This post was edited by irimi on Nov 4 2013 02:53pm
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Nov 5 2013 06:26am
Quote (carteblanche @ Nov 4 2013 08:14pm)
why dont you start by providing the big O analysis?

I honestly don't know how to do a big O analysis for a multi parameter function utilizing memoization.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 5 2013 12:10pm
Quote (tt_toby @ Nov 5 2013 08:26am)
I honestly don't know how to do a big O analysis for a multi parameter function utilizing memoization.


you really need to use big O analysis if you're trying to figure out efficiency. from there, you can look at each component and see if you can do it faster. or compare it to another known algorithm for an upper/lower bound if applicable. just use a separate variable for each parameter. i'm not sure what memoization is.

eg:
Code
for int i = 0 to param1{
for int j = 0 to param2{
// blah blah
}
}


is O(param1) * O(param2)

similarly

Code
for int i = 0 to param 1{
// blah blah
}

for int j = 0 to param 2{
// blah
}


is O(param1) + O(param2)

make sure you don't count these as constant time unless they really are:

Code
if comb not in allcombs:


Code
comb.sort()
comb.reverse()


This post was edited by carteblanche on Nov 5 2013 12:16pm
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Nov 5 2013 12:49pm
Okay, I'll have a look at it. By memoization I meant dynamic programming, that is storing already computed sub-solutions to the problem in a dictionary for easy lookup instead of computing this sub-solutions over and over in the process of computing the global solution. Looking up a sub-solution will take linear time, and I don't know how to work this fact into the analysis.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll