/*
* 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 :)
I am curious about the performance in those kind of challenge. I am totally aware that JavaScript is not the fastest to do this kind of thing and the idea is not to bash any language of implementation. I'm just a guy curious about that. If some of you are willing to take the habit to post the performance for one milion iterations that would be cool to compare with others!
// one milion iterationsfor(letindex=0;index<1000000;index++){change(31);}console.timeEnd("time");// time: 21670.730ms
Don’t get me wrong, I like your version, but in the strict sense I’d argue it isn’t more “pure” than the original version.
We might think that the COINS variable declared with a const operator is indeed a constant but it is not. It is not possible to assign another array, but you can still update its values by targeting its indexes. This means that the function is inpure because its result is not predictable as it relies on an out-scope variable that can be updated outside of the function call, but used inside the function. The oustide world can update the array like so:
COINS[0]="Hello world!";
The solution would be to declare the array as a freezed array in JavaScript:
constCOINS=Object.freeze([1,2,3,...]);
That way, no side-effects possible. This would also allow the function to be pure again. Another solution would be to declare the COINS variable inside the function, or even better, declare another parameter if the coins variable is something to be changed later.
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:
JavaScript
Pure variant of what has already been made in JavaScript.
Performance
I am curious about the performance in those kind of challenge. I am totally aware that JavaScript is not the fastest to do this kind of thing and the idea is not to bash any language of implementation. I'm just a guy curious about that. If some of you are willing to take the habit to post the performance for one milion iterations that would be cool to compare with others!
Source-Code
Available online.
Hey there, thank you for your comment!
We might think that the
COINSvariable declared with aconstoperator is indeed a constant but it is not. It is not possible to assign another array, but you can still update its values by targeting its indexes. This means that the function is inpure because its result is not predictable as it relies on an out-scope variable that can be updated outside of the function call, but used inside the function. The oustide world can update the array like so:The solution would be to declare the array as a freezed array in JavaScript:
That way, no side-effects possible. This would also allow the function to be pure again. Another solution would be to declare the
COINSvariable inside the function, or even better, declare another parameter if the coins variable is something to be changed later.JavaScript with reduce:
Go - with tests:
change.go:
change_test.go
change_test_cases.go
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.
My solution in js
Greedy solution in JS
Python
Another javascript solution:
that guard clause
Some people may say "are the comments really necessary?"
I say to them, "Is capitalism?"
[gist.github.com/devparkk/e5a9ebbe8...]