d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > The Change Problem :( > All The Ways To Make Change
Prev123456Next
Add Reply New Topic New Poll
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 04:13pm
Quote (irimi @ 29 Nov 2012 18:02)
yeah. so i read your post a few times, and you're not making a lot of sense in what you just said.

in any case, those don't look like corner cases to me.  they look like inherent flaws in your logic.


That's most likely the case but I dont know how to implement this any other way unfortunately

an example of what I was saying is this

my code runs cases in that order 4 3 2 1

when I input 17 cents to be processed I get every result except the one 1 dime + 7 pennies

If I remove case 4 and case 2 (the cases that process quarters and nickels) I get the missing result of 1 dime and 7 pennies

So what I wanted to do was run 4 3 2 1 and some how implement a way to run 3 and 1 alone 4 and 1 alone 4 3 1 alone and lastly 3 2 1 alone and then add them up this way and subtracting overlap?

I dont know I am pretty burnt out, probably overthinking the hell out o this... I appreciate the help though.

This post was edited by Nom_Nomz on Nov 29 2012 04:15pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 29 2012 04:39pm
oftentimes the best way when you get stuck is to throw out everything and start from scratch. don't try to fix something that's broken -- just rewrite it.

look at the recursive solution that i most recently posted. it's mostly correct, though the case for making change for 25 cents is still a work in progress. once you get the base cases correct, implementing the recursion is absolutely trivial.

the code structure would look something like this:

Code
def waysToMakeChange(cents):
 if (cents == 25):
    return [solution for 25 cents]
 if (cents == 10):
    return [solution for 10 cents]
 if (cents == 5):
    return [solution for 5 cents]
 if (cents < 5):
    return [cents * pennies]

 else:
   if (cents % 25 == 0):
     returnVal = []
     changeLast = waysToMakeChange(cents - 25)
     for (x in changeLast):
        for (y in [solution for 25 cents])
          returnVal.append( x + y )
     return returnVal
   if (cents % 10 == 0):
     returnVal = []
     for (x in waysToMakeChange(cents - 10)):
        for (y in [solution for 10 cents])
           returnVal.append( x + y )
     return returnVal
  if (cents % 5 == 0):
     returnVal = []
     for (x in waysToMakeChange(cents - 5)):
        for (y in [solution for 5 cents])
          returnVal.append( x + y )
     return returnVal
   else:
     returnVal = []
     for (x in waysToMakeChange(cents - 1)):
       returnVal.append(x + penny)
     return returnVal


This post was edited by irimi on Nov 29 2012 04:48pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 04:43pm
"...to find all the ways..."
so if you enter the number 17.... what should be the output? think about it... you'll see that 17 already has a very huge amount of possibilities...
here are just a few:
Code
17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1-0
17-12-11-10-9-8-7-6-5-4-3-2-1-0
17-16-11-10-9-8-7-6-5-4-3-2-1-0
17-16-15-10-9-8-7-6-5-4-3-2-1-0
...
17-16-15-14-13-12-11-10-9-8-7-6-5-0
...
17-12-11-10-5-0
17-16-11-10-5-0
17-7-6-5-4-3-2-1-0
17-16-6-5-4-3-2-1-0
17-7-2-1-0
17-16-11-1-0
17-12-2-1-0
17-12-11-6-5-0
...


think about the possibilities... are you really so crazy that you need all those numbers in the RIGHT ORDER? I at least still don't know what you want... I understand Irmi and I also understand you... but your way won't work until you know what recursive means :/

I guess that this output is what you want for the number 17:
17:
Code
1 1 5 5 5
1 1 5 10
1 1 1 1 1 1 1 10
1 1 1 1 1 1 1 5 5
1 1 1 1 1 1 1 1 1 1 1 1 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


or is it this version? (number of lines says how many possibilities, first row says how many cents, second one how many 5-cent pieces... and so on)
Code
2 3 0 0
2 1 1 0
7 0 1 0
7 2 0 0
12 1 0 0
17 0 0 0


or is the output just a "6 possibilities for the change are possible" ?
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 04:43pm
Quote (irimi @ 29 Nov 2012 18:39)
oftentimes the best way when you get stuck is to throw out everything and start from scratch.  don't try to fix something that's broken -- just rewrite it.

look at the recursive solution that i most recently posted.  it's mostly correct, though the case for making change for 25 cents is still a work in progress.  once you get the base cases correct, implementing the recursion is absolutely trivial.


YUP!
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 04:47pm
Quote (Richter @ 29 Nov 2012 18:43)
"...to find all the ways..."
so if you enter the number 17.... what should be the output? think about it... you'll see that 17 already has a very huge amount of possibilities...
here are just a few:
Code
17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1-0
17-12-11-10-9-8-7-6-5-4-3-2-1-0
17-16-11-10-9-8-7-6-5-4-3-2-1-0
17-16-15-10-9-8-7-6-5-4-3-2-1-0
...
17-16-15-14-13-12-11-10-9-8-7-6-5-0
...
17-12-11-10-5-0
17-16-11-10-5-0
17-7-6-5-4-3-2-1-0
17-16-6-5-4-3-2-1-0
17-7-2-1-0
17-16-11-1-0
17-12-2-1-0
17-12-11-6-5-0
...


think about the possibilities... are you really so crazy that you need all those numbers in the RIGHT ORDER? I at least still don't know what you want... I understand Irmi and I also understand you... but your way won't work until you know what recursive means :/

I guess that this output is what you want for the number 17:
17:
Code
1 1 5 5 5
1 1 5 10
1 1 1 1 1 1 1 10
1 1 1 1 1 1 1 5 5
1 1 1 1 1 1 1 1 1 1 1 1 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


or is it this version? (number of lines says how many possibilities, first row says how many cents, second one how many 5-cent pieces... and so on)
Code
2 3 0 0
2 1 1 0
7 0 1 0
7 2 0 0
12 1 0 0
17 0 0 0


or is the output just a "6 possibilities for the change are possible" ?


Order doesn't matter (thank god)

Also I'm not sure if this was obvious or not but I am coding in C at the moment if that makes a difference.

your short code block with all the 1's etc is what I am trying to get to at the moment.

I think my biggest problem right now is the act that my instructor gave me some starting hints and they just aren't working for me...let me know i you can explain them better?

Hints (not mandatory):
Here is the function description:
// Precondition: denomination = 1 (for penny), 2 (for nickel), 3 (for dime),
// or 4 (for quarter).
// Postcondition: If amount < 0, then 0 has been returned. Otherwise,
// the value returned is the number of ways that amount
// can be changed into coins whose denomination is no
// larger than denomination.
int ways (int amount, int denomination);
For the sake of simplifying the ways function, develop a coins function that returns the value of each
denomination. Thus, coins(1) returns 1, coins(2) returns 5, coins(3) returns 10, and coins(4) returns 25.

I understand recursion takes an input performs a reduction step and then calls itself using the result of that step ( on a very basic level) to some ending condition... at least that is what I think it means?

This post was edited by Nom_Nomz on Nov 29 2012 04:53pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 29 2012 04:50pm
see the code i posted above.

as I mentioned before, a fairly simple relationship can be established from 1 cent to 5 cents to 10 cents.

1 cent = 1 penny
5 cents = 5 * [solution for 1 cent] + 1 nickel
10 cents = [solution for 5 cents] * [solution for 5 cents] + 1 dime

so I think by extension:
25 cents = [solution for 10 cents] * [solution for 10 cents] * [solution for 5 cents] + 1 quarter

but perhaps, it's easier to think of it this way:

25 cents = [solution for 15 cents] * [solution for 10 cents] + 1 quarter
which *then becomes*
25 cents = [solution for 10 cents] * [solution for 10 cents] * [solution for 5 cents] + 1 quarter

so in theory, the only thing you need in your base case... is to add the additional coin. the rest is just a matter of doing an i-j for loop to combine two subcases you get from subtracting the largest possible coin.

second attempt:
Code
def change(n):
 ret = []

 if (n < 5):
   return [pennies * n]
 if (n > 25)
   for (x in change(n-25)):
   ret.append(x+ 1 quarter)
     for (y in change(25)):
        ret.append(x+y)        
     return ret
 if (n > 10):
     for (x in change(n-10)):
       ret.append(x + 1 dime)
       for (y in change (10)):
          ret.append(x+y)
     return ret
 if (n > 5):
    for (x in change(n-5)):
       ret.append(x + 1 nickel)
      for(y in change(5)):
         ret.append(x+y)
     return ret


or something like that.

This post was edited by irimi on Nov 29 2012 05:11pm
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 04:58pm
yes, thats it. but you need a function, in which one you call the function itself...

if you would call in every function the function itself, the program would crash as soon as so many function are called that there is no more space available (wherever that space is doesn't matter)

so the function aborts at a certain time. when? as soon as there are no more possibilities for the sub-problem

so if i start with such a recursive function, i must give it the number for the remaining cash. the remaining cash is at the beginning 17

well, when the function gets 17, then it knows that it can't give a 25'er piece back... that would be too much.

so what does the function now? it opens the function itself for all the remaining possibilities...
those are:
Code
int function(remainingCash) {
possibilities += function(17-5)
possibilities += function(17-1)
possibilities += function(17-7)
//possibilities += function(17-25)
return possibilities;}

the last line (17-25) isn't a possibility, because it gets negative... doesn't matter if you check it before you call the function or after that...

Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 29 2012 05:10pm
actually this is kind of annoying. this should be pretty easy to solve lol - kind of embarrassed that i haven't come up with a stupidly simple solution to this yet.

fucking combinatorics.
Member
Posts: 3,210
Joined: Aug 21 2010
Gold: 152.00
Nov 29 2012 05:18pm
Quote (irimi @ 29 Nov 2012 19:10)
actually this is kind of annoying.  this should be pretty easy to solve lol - kind of embarrassed that i haven't come up with a stupidly simple solution to this yet.

fucking combinatorics.


Yeah every time I think I've got it...I don't. tbh I'd rather take a kick to the nuts right now...
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 29 2012 05:21pm
it's just too late :S

This post was edited by Richter on Nov 29 2012 05:34pm
Go Back To Programming & Development Topic List
Prev123456Next
Add Reply New Topic New Poll