/*
* coins[0] = number of 25 cent coins
* coins[1] = number of 10 cent coins
* coins[2] = number of 5 cent coins
* coins[3] = number of 1 cent coins
*/unsignedintchange(unsignedintn,unsignedintcoins[4]);
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
packagechangevarchangeTests=[]struct{descriptionstringinputintexpectedmap[int]int}{{"no change",0,map[int]int{25:0,10:0,5:0,1:0},},{"uses one coin",5,map[int]int{25:0,10:0,5:1,1:0},},{"uses two coins",27,map[int]int{25:1,10:0,5:0,1:2},},{"uses many coin",39,map[int]int{25:1,10:1,5:0,1:4},},}
funvoidchange(intcents){cents/25=>constintcoin25;coin25*25-=>cents;cents/10=>constintcoin10;coin10*10-=>cents;cents/5=>constintcoin5;coin5*5-=>cents;#! this is a comment
#! below is the debug print.
<<<"{ ","25 => ",coin25,", 10 => ",coin10,", 5 => ",coin5,", 1 => ",cents," }">>>;}31=>change;
prints
{25=>1,10=>0,5=>1,1=>1}
It probably could be written better, but looks like it gets the job done.
defmoduleCashMachinedo#🎵 `goto` a CashMachinedefchange(money)do#🎵 to get a ticket homeexchange(money,%{})#🎵 the message on the screenend#🎵 says "don't make plans, y'all broke"defpexchange(0,coins),do:coins#🎵 no, no, this can't be rightdefpexchange(coin,coins)whencoin>=25do#🎵 I know that times is tightquarters=div(coin,25)#🎵 I've only just been paidcoin=rem(coin,25)#🎵 three weeks, five days, do that seemexchange(coin,put_in(coins[25],quarters))#🎵 right? no.enddefpexchange(coin,coins)whencoin>=10do#🎵 I scratch a livin', it ain't easydimes=div(coin,10)#🎵 you know it's a dragcoin=rem(coin,10)#🎵 I'm always payin', never makin'exchange(coin,put_in(coins[10],dimes))#🎵 but you can't look backenddefpexchange(coin,coins)whencoin>=5do#🎵 I wonder if I'll ever get tonickels=div(coin,5)#🎵 where I want to becoin=rem(coin,5)#🎵 better believe it,exchange(coin,put_in(coins[5],nickels))#🎵 I'm workin' for the CashMachineenddefpexchange(coin,coins)whencoin>=1do# lyrics from "Cash Machine"exchange(0,put_in(coins[1],coin))# by Hard-Fiendend
Some people may say "are the comments really necessary?"
x86_64 assembly (System V ABI, GNU assembler), for those concerned about speed.
change.S:
change.h:
I am concerned about speed
we need to get that stuff off the streets smh
causin' good folk to think they need to write stuff in assembly, fcol >.>
ruby <3
Rust, iterative style:
Go - with tests:
change.go:
change_test.go
change_test_cases.go
My solution in js
JavaScript with reduce:
This is a nice solution in Java. But don't forget to remove the
number = 74;, otherwise the function will always write the same results.Just my two cents, in Gwion
prints
It probably could be written better, but looks like it gets the job done.
Python
that guard clause
Some people may say "are the comments really necessary?"
I say to them, "Is capitalism?"
Another javascript solution:
Greedy solution in JS
[gist.github.com/devparkk/e5a9ebbe8...]