r/dailyprogrammer • u/oskar_s • Jun 15 '12
[6/15/2012] Challenge #65 [easy]
Write a program that given a floating point number, gives the number of American dollar coins and bills needed to represent that number (rounded to the nearest 1/100, i.e. the nearest penny). For instance, if the float is 12.33, the result would be 1 ten-dollar bill, 2 one-dollar bills, 1 quarter, 1 nickel and 3 pennies.
For the purposes of this problem, these are the different denominations of the currency and their values:
- Penny: 1 cent
- Nickel: 5 cent
- Dime: 10 cent
- Quarter: 25 cent
- One-dollar bill
- Five-dollar bill
- Ten-dollar bill
- Fifty-dollar bill
- Hundred-dollar bill
Sorry Thomas Jefferson, JFK and Sacagawea, but no two-dollar bills, half-dollars or dollar coins!
Your program can return the result in whatever format it wants, but I recommend just returning a list giving the number each coin or bill needed to make up the change. So, for instance, 12.33 could return [0,0,1,0,2,1,0,1,3] (here the denominations are ordered from most valuable, the hundred-dollar bill, to least valuable, the penny)
- Thanks to Medicalizawhat for submitting this problem in /r/dailyprogrammer_ideas! And on behalf of the moderators, I'd like to thank everyone who submitted problems the last couple of days, it's been really helpful, and there are some great problems there! Keep it up, it really helps us out a lot!
5
u/emcoffey3 0 0 Jun 15 '12
So, I went really overboard on this, but I did so because I intend on using it in another application. The real meat of the class are in the private methods RecalculateDistribution() and CalcStep(). Also, I included twenties even though they weren't in the problem description.
The following code is in my Main()...
...which yields the following output:
5
u/[deleted] Jun 15 '12 edited Jun 15 '12
J magic time!
1
u/SwimmingPastaDevil 0 0 Jun 15 '12
Output:
I think i am missing something here, for 112.33 it gives:
What am i doing wrong here?
3
u/ashashwat Jun 16 '12
In Haskell, (awesome pattern matching here :) )
2
u/_Daimon_ 1 1 Jun 15 '12
Python
Using a greedy strategy with arbitrary coinage.
1
u/ixid 0 0 Jun 15 '12 edited Jun 15 '12
D language, a flexible version that will deal with any set of notes and coins but defaults to the listed set.
module main; import std.stdio;
1
u/[deleted] Jun 15 '12 edited Jun 16 '12
1
u/pbl24 Jun 15 '12
Python. Stupid floating point issues. Probably would've been easier using an iterative solution, but I did it using recursion anyways.
Output:
1
u/xjtian Jun 15 '12
Recursive solution that allows you to specify denominations.
Python 2.7:
1
u/bengarrr Jun 15 '12
Python 2.7
Here's my output for 12.33: [0, 0, 1.0, 0, 2.0, 1.0, 0, 1.0, 3.0]
Here's my output for 112.33: [1.0, 0, 1.0, 0, 2.0, 1.0, 0, 1.0, 2.0]
I understand not :(
1
u/kohjingyu 0 0 Jun 16 '12
Python:
1
u/loonybean 0 0 Jun 16 '12
Python:
1
u/Xadoo 0 0 Jun 16 '12
Dat Ruby
1
u/[deleted] Jun 16 '12
Perl!
1
u/cdelahousse Jun 18 '12
Javascript:
1
u/weedpatch2 Jun 18 '12
Powershell:
If anyone has a better way to do this in Powershell, please let me know.
1
u/Okashu Jun 24 '12 edited Jun 24 '12
I managed to do this, but used polish currency and language. Linking to pastebin because it's pretty long: http://pastebin.com/kLPjM7zz
Also, I'd be happy to recieve feedback on what could I improve in, it's my second program bigger than 10 lines, and the first one completed.
1
u/Jamdod Jun 26 '12
Actionscript 3:
1
u/Arthree Jun 15 '12 edited Jun 16 '12
autohotkey:
1
u/[deleted] Jun 29 '12
[removed] — view removed comment