Sinisterly
[challenge] change calculator - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: [challenge] change calculator (/Thread-challenge-change-calculator)



[challenge] change calculator - Inori - 12-22-2015

This is a pretty quick one. All you need to do is make a method that accepts a float and prints out the change values for it.

Example IO:
Code:
#In: calc(546.24) #Out: $100: 5 $50: 0 $20: 2 $10: 0 $5: 1 $2: 0 $1: 1 $0.25: 0 $0.10: 2 $0.05: 0 $0.01: 4

Spoiler: Example Solution (python)
Code:
def calc(c): for i in [100,50,20,10,5,2,1,0.25,0.10,0.05,0.01]: print '$%s: \t%s'%(i,int(c//i)) c=c%i



RE: [challenge] change calculator - Nil - 12-22-2015

Lmao! So I didn't know where to start, and I also didn't want to look for help. And thus, this masterpiece was born!!!

Edit: I'll do something better later. The next piece of code I was about to submit would probably be more readable in machine.


RE: [challenge] change calculator - m0dem - 12-22-2015

Python:
Code:
def calc(a): for i in (100,50,20,10,5,2,1,.25,.10,.05,.01): b,x=i,0 while b<=a: b,x=b+i,x+1 a-=x*i print "$"+str(i),"-",x
Wow, your solution is so simple...


RE: [challenge] change calculator - Inori - 12-22-2015

(12-22-2015, 07:21 AM)God Wrote: Lmao! So I didn't know where to start, and I also didn't want to look for help. And thus, this masterpiece was born!!!

Warning: Embarrassingly funny code ahead!

Spoiler: Python 3+
Code:
MONEY = 345 def main(): index = 0 list1 = [100, 50, 20, 10, 5, 1, .25, .10, .05, .01] list2 = [0]*10 list2 = change(MONEY, index, list1, list2) pretty_display(list1, list2, index) def change(MONEY, index, list1, list2): for x in list1: ch = MONEY // list1[index] list2[index] = int(ch) MONEY = MONEY - (ch * list1[index]) index+=1 return list2 def pretty_display(list1, list2, index): for x in list2: print('$', list1[index], '\t',': ', x, sep = '') index+=1 main()


So obviously you just change the money variable to get your desired results. And it just prints out a list in order of the change values. I didn't use a two dollar bill value.

But hey! It works.

Edit: No longer prints out list, modified for pretty display! Definitely now a industrial standard program! haha. Anyway, thought it was easy until I started typing and realized I couldn't find an elegant approach. Feel like I just brute-forced my way to a solution. If anyone has any criticism, such as seeing something that could easily be improved or changed then by all means point it out.

Edit2: Actually, If I add any amount of dollars with something under 5 cents (300.04) it prints out one less penny. I'll fix that in the morning.

Dude, that looks like some C shit Tongue