constgetEvolutionRateMessage=(before,after)=>{constevolution=before==0?after*100:after==0?-before*100:Math.round((after-before)/before*100)returnevolution==0?"No evolution.":evolution>0?`A positive evolution of ${evolution}℅.`:`A negative evolution of ${Math.abs(evolution)}%.`}
usePHPUnit\Framework\TestCase;classGetEvolutionRateMessageTestextendsTestCase{publicfunctiontestShouldReturnPositiveRateMessage(){$this->assertEquals(getEvolutionRateMessage(11.29,45.79),"A positive evolution of 306%");}publicfunctiontestShouldReturnOtherPositiveRateMessage(){$this->assertEquals(getEvolutionRateMessage(0,27.35),"A positive evolution of 2735%");}publicfunctiontestShouldReturnNegativeRateMessage(){$this->assertEquals(getEvolutionRateMessage(95.12,66.84),"A negative evolution of 30%");}publicfunctiontestShouldReturnOtherNegativeRateMessage(){$this->assertEquals(getEvolutionRateMessage(41.26,0),"A negative evolution of 4126%");}publicfunctiontestShouldReturnZeroEvolutionMessage(){$this->assertEquals(getEvolutionRateMessage(1.26,1.26),"No evolution");}}
All this talk of evolution makes me want to Go play Pokemon again. Holy crap I haven't played that in a long time!
rate.go
packageevolutionimport"fmt"const(negativestring="A negative evolution of %.f%%."positivestring="A positive evolution of %.f%%."neutralstring="No evolution.")// Rate indicates the evolution percentage based on the before and after valuesfuncRate(beforefloat64,afterfloat64)string{ifbefore==0{returnfmt.Sprintf(positive,after*100)}ifafter==0{returnfmt.Sprintf(negative,before*100)}rate:=(after-before)/before*100ifrate>0{returnfmt.Sprintf(positive,rate)}ifrate<0{returnfmt.Sprintf(negative,rate*-1)}returnneutral}
rate_test.go
packageevolutionimport"testing"typetestCasestruct{descriptionstringinput[]float64expectedstring}funcTestRate(t*testing.T){testCases:=[]testCase{{"a positive evolution rate",[]float64{11.29,45.79},"A positive evolution of 306%.",},{"a negative evolution rate",[]float64{95.12,66.84},"A negative evolution of 30%.",},{"zero before value",[]float64{0,27.35},"A positive evolution of 2735%.",},{"zero after value",[]float64{41.26,0},"A negative evolution of 4126%.",},{"same before and after values",[]float64{1.26,1.26},"No evolution.",},}for_,test:=rangetestCases{ifresult:=Rate(test.input[0],test.input[1]);result!=test.expected{t.Fatalf("FAIL: %s - Rate(%+v): %s - expected: %s",test.description,test.input,result,test.expected)}t.Logf("PASS: %s",test.description)}}
defgetEvolutionratemessage(before,after):try:evolution=(after-before)/before*100exceptZeroDivisionError:return"can't divide by zero "ifevolution>0:statement=f"A Positive evolution of {round(evolution)}%"elifevolution<0:statement=f"A Negative evolution of {round(evolution)}%"else:statement=f"No evolution"returnstatementprint(getEvolutionratemessage(95.12,66.84))
getEvolutionRateMessage::Double->Double->StringgetEvolutionRateMessagebeforeafter|evol>0="A positive evolution of "++showevol++"%."|evol==0="No evolution."|evol<0="A negative evolution of "++show(0-evol)++"%."whereevol=(after-before)/before*100
Here is a Javascript arrow function, that uses nested ternary operators. Someone probably already posted this answer, but hey main point is that I took the challenge and solved by myself.
constgetEvolutionRateMessage=(before,after)=>{constrate=Math.round((((after==0?-before*before+before:after)-before)/(before==0?1:before))*100);returnrate==0?"No evolution.":rate>0?`A positive evolution of ${rate}%.`:`A negative evolution of ${-1*rate}%.`;};
Perl solution. I still don't understand how the values are computed for zeroes, but it passes the tests :-)
#!/usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };sub get_evolution_rate_message{my($before,$after)=@_;return'No evolution.'if$before==$after;returnsprintf'A %s evolution of %.0f%%.',qw(_ negative positive)[$before<=>$after],abs(($before-($after||($before+$before**2)))*100/($before||1))}useTest::Moretests=>5;isget_evolution_rate_message(11.29,45.79),'A positive evolution of 306%.';isget_evolution_rate_message(95.12,66.84),'A negative evolution of 30%.';isget_evolution_rate_message(0,27.35),'A positive evolution of 2735%.';isget_evolution_rate_message(41.26,0),'A negative evolution of 4126%.';isget_evolution_rate_message(1.26,1.26),'No evolution.';
My knowledge of statistics is a bit rusty (yet I didn't use rust for this challenge), but I think that an increase from 0 to anything is basically an infinite rate and a decrease from anything to 0 is a negative 100% rate, so I disagree with the examples 3 and 4 and have written my solution accordingly.
Haskell, of course
get_rate::Double->Double->Stringget_ratebeforeafter|before==after="No evolution."|before==0="Infinite evolution."|evolution<0="A negative evolution of "++(show'.abs)evolution++"%."|otherwise="A positive evolution of "++show'evolution++"%."whereevolution=(after-before)*100/beforeshow'=show.round-- show_abs = show' . abs -- Hmm, maybe not the best name ever, let's leave it out.
Output
get_rate11.2945.79-- "A positive evolution of 306%."get_rate95.1266.84-- "A negative evolution of 30%."get_rate027.35-- "Infinite evolution."get_rate41.260-- "A negative evolution of 100%."get_rate1.261.26-- "No evolution."
Oyeeeeee
Thanks a lot , man
I appreciate it ...
Can u help me with how to publish my codes here in comment section ...
I don't really understand this markdown and all ...
I wrote it in Python, kept it as short as possible while maintaining clarity. 😎
Neat! I like what they did with this new string interpolation thing in Python.
I Didn't know it was called a polarity. Learning new things every day. Yay!
Some JavaScript.
Hi there, nice and short solution! I believe the case for the values
0&27.35wont give what you expected. But you are in the right path for sure.Fixed!
Since no one proposed a PHP solution, here I am!
getEvolutionRate()unit tests:getEvolutionRateMessage()unit tests:I love those guards you are puting in your code. Really makes me want to go back and do PHP with you like the good ol'times!
Let's do a side project together
All this talk of evolution makes me want to Go play Pokemon again. Holy crap I haven't played that in a long time!
rate.go
rate_test.go
I always love your take at these challenges in Go. I should get started soon. Looking sharp as always!
And yes, Pokemon is just timelessly good. I could play it all my life haha!
Ah shucks, I am humbled by the praise.
You should, Go is a very easy language to pick up and use.
Only part that may require a little more effort to understand is the concurrency related features of it but just pick those up as you need them.
My javascript solution :
Haskell:
I long awaited this one. Haha!
Here is a Javascript arrow function, that uses nested ternary operators. Someone probably already posted this answer, but hey main point is that I took the challenge and solved by myself.
Thanks for showing us your solution. What's important is what you achieve at the end of the day!
Perl solution. I still don't understand how the values are computed for zeroes, but it passes the tests :-)
Imagine that instead of going from 0 to 27.35, you are going from 0.0000000001 to 27.35. I think you'll have your answer here. 😉
My knowledge of statistics is a bit rusty (yet I didn't use rust for this challenge), but I think that an increase from 0 to anything is basically an infinite rate and a decrease from anything to 0 is a negative 100% rate, so I disagree with the examples 3 and 4 and have written my solution accordingly.
Haskell, of course
Output
python
{ % gist gist.github.com/devparkk/092171076... %}
The code will throw an error if you pass in the following parameters
getEvolutionRateMessage 0 27.35as you're dividing by 0.Oyeeeeee
Thanks a lot , man
I appreciate it ...
Can u help me with how to publish my codes here in comment section ...
I don't really understand this markdown and all ...
Hi Prakash, you can refer to this link to see how code syntax highlighting works in markdown.
Thank you