d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > The Change Problem :( > All The Ways To Make Change
Prev13456Next
Add Reply New Topic New Poll
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Nov 30 2012 12:27pm
yes it's still brute force, but thats the reason i called it brute-force. you mad?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Nov 30 2012 02:29pm
Quote (Richter @ Nov 30 2012 11:27am)
yes it's still brute force, but thats the reason i called it brute-force. you mad?


more like, amazed that you would consider an inductive/recursive solution to be brute force. thought you were smarter than that.
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Dec 1 2012 09:50am
Well if you test the same possibilities as a method which is obviously a brute-force method, why should you even think that the other method could be called any different than a brute-force? I think my explanation is clear enough ^^. I don't see the reason to call it different, just because you use functions instead of a for loop... the functions would test the same possibilities...

I just don't want to discuss which the other guy. Because he imo didn't understood the problem. We don't want to just get a number of ways, we also want the ways. Well you two can discuss furthermore if you want, but i'm tired of discussing something which is imo useless as long as nobody has presented this "wonder algorithm" (which also actually reduces the possibilities).

If you really wan't to discuss further, calculate the complexity of the algorithms. If thats too hard, you could simplify the problem first a bit.
The complexity of the recursive function can be calculated by the "telescoping technique". Just found a pdf about it here: digital.cs.usu.edu/~allanv/cs5050/bigOh.pdf
I haven't read the pdf but on page 7 you can see the necessary calculations :)
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Dec 1 2012 07:14pm
the recursive algorithm comes up with solutions that are correct, and only solutions that are correct.

if you were talking about the OP's implementation, then I have no argument with you since I didn't bother reading his code.
Member
Posts: 8,564
Joined: Jun 13 2006
Gold: 4.75
Dec 3 2012 11:51am
Quote (Nom_Nomz @ 29 Nov 2012 17:47)
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?


Do you need to display every possibilites or is the numbers of way enough?
If you don't need the display here is an easy way of calculating the number of possibilites recursively

http://ideone.com/gPl19n


Have Fun!

Edit: HAd forgotten to cover the negative numbers... (if amount <0)

This post was edited by Fawar on Dec 3 2012 12:20pm
Member
Posts: 8,564
Joined: Jun 13 2006
Gold: 4.75
Dec 3 2012 12:24pm
Quote (Fawar @ 3 Dec 2012 12:51)
Do you need to display every possibilites or is the numbers of way enough?
If you don't need the display here is an easy way of calculating the number of possibilites recursively

http://ideone.com/gPl19n


Have Fun!

Edit: HAd forgotten to cover the negative numbers... (if amount <0)


I cant my previous post edit anymore, but there is the possibility in my code that you have 2 exact same possibilities for 26 Exemple : 25+1 and 1 + 25, which is wrong, i will look into it later

It gives out the number of permunation and we are looking for the number or Combinations.

This post was edited by Fawar on Dec 3 2012 12:36pm
Member
Posts: 8,564
Joined: Jun 13 2006
Gold: 4.75
Dec 3 2012 08:02pm
Quote (irimi @ 29 Nov 2012 17:50)
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.




Just try it out on paper...

Solution for 10 * Solution for 10 doesnt give out solution of 20.

Nb of possibilities of giving out 20 (non ordered) Total = 9
2x 10
1x10 + 2x5
1x10 + 1x5 + 5x1
1x10 + 10x1
4x5
3x5+5x1
2x5+10x1
1x5 + 15x1
20x1

Nb of possibilities of giving out 10 (non ordered) total =4
1x10
2x5
1x5 + 5x1
10x1

using your formulae : 4x4 + 1 = 17... which is 8 too much

Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Dec 4 2012 01:51pm
several things regarding that --

1) the inductive solution is correct; the code expressing it, not so much
2) the code in a later post is much closer to the real deal; it is in fact a correct solution if you add some minor logic to dedup solutions
3) the count is uninteresting -- it's simpler to calculate (you can formulaically dedup rather than have to do it programmatically).

last but not least, your solutions posted above are so conceptually far off the mark that I don't even want to start...

This post was edited by irimi on Dec 4 2012 01:52pm
Member
Posts: 8,564
Joined: Jun 13 2006
Gold: 4.75
Dec 4 2012 03:34pm
Quote (irimi @ 4 Dec 2012 14:51)
several things regarding that --

1) the inductive solution is correct; the code expressing it, not so much
2) the code in a later post is much closer to the real deal; it is in fact a correct solution if you add some minor logic to dedup solutions
3) the count is uninteresting -- it's simpler to calculate (you can formulaically dedup rather than have to do it programmatically).

last but not least, your solutions posted above are so conceptually far off the mark that I don't even want to start...


So thats how you are, proven wrong you attack.
I already said my solution was wrong and i even know why, i know how to correct it, but the solution is rather long and complicated because the number of pieces on each anwser is not static. yet my problem is the same as you, duplicates. So being so far i gues you are too.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Dec 4 2012 03:38pm
Quote (Fawar @ Dec 4 2012 02:34pm)
So thats how you are, proven wrong you attack.


let's clarify one thing: if someone comes up with a proof for something and starts with the premise that 1+1=3, then there's absolutely nothing offensive about saying "it's so conceptually far off the mark that it's not even worth considering".

there's no difference between that and what I'm doing here.

anyhow, this is a pointless exercise. the solution is fairly well-established, and the actual specifics of putting it into code that's 100% correct is pretty uninteresting (though probably a bit tedious). i was hoping that the fairly simple/elegant inductive proof could be expressed in equally simple/elegant code, but it seems to not be the case, so there's nothing left to be done here.

This post was edited by irimi on Dec 4 2012 03:41pm
Go Back To Programming & Development Topic List
Prev13456Next
Add Reply New Topic New Poll