Login Register






[challenge] change calculator filter_list
Author
Message
[challenge] change calculator #1
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
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

[+] 1 user Likes Inori's post
Reply

RE: [challenge] change calculator #2
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.
"If you look for the light, you can often find it. But if you look for the dark, that is all you will ever see.”


Reply

RE: [challenge] change calculator #3
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...

Reply

RE: [challenge] change calculator #4
(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
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

[+] 1 user Likes Inori's post
Reply