Setup
Your friend likes to go to the horse races and gamble on which horses will finish first, second, and third place. Unfortunately, he doesn't know how many horses will be entering until he gets to the track.
Write a function that will take any number of horses as its only argument. This function must return the total number of different combinations of winners for the gold, silver, and bronze medals.
For example, if there are 15 horses, there are 2730 possible unique combinations of winners. If the number of horses is 3 or less, return the input value. If the number of horses is not an integer, return undefined
.
Examples
horses(15), 2730, true)
horses(2.5), undefined, false)
Tests
horses(12)
horses(2)
horses(11)
horses(a)
Good luck!
This challenge comes from tu6619 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (13)
Ruby
Yes, I said that in my post.
However, I misread the challenge, and thought that "undefined" should be returned when n <= 3, although it should actually return the input.
Here's my submission while I'm learning Nim.
Shouldn't that be "less than 3"? Shouldn't
horses(3)
return 6?Python solution 🐍
Good old factorials. I had fun with this one. Feel free to give advice if I can make my code better!
JS
CodePen
Edit: forgot to convert the input to a number making all of the numbers that go through the function a string
Shortest answer I could think of:
And in Standard ML, since it has type checking I can skip the int check:
The code (F#)
As F# is a strongly typed language, no need to check n is an integer - it is. Idiomatically, "undefined" in F# is handled with the Option monad, so we return either None or Some result.
The actual calculation is trivial; n * (n-1) * (n-2)
My JavaScript solution:
In Go.