Time flies while you're having fun, but then one day your bio says something about being a 30+ year veteran in software engineering. Still, I've not seen it all, let alone done it all (yet).
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)
PHP enthusiast, passionate about quality tools, AI exploration, and sharing knowledge through articles. Automation is my playground; I’m always learning, innovating, and pushing boundaries.
The word "combinations" being used here provides us with a good jumping-off point to figure out the solution - combinatorics! We can calculate the n choose k/binomial coefficient for the horses, where n is the total number of horses, and k is equal to 3, since we're looking for 3 horses (gold, silver, and bronze winners). From there, we need to take into account that ordering - which horse won which position - matters to us; mathematical combinations are sets in which order doesn't normally matter. We'll need permutations for that part.
So, knowing that the number of permutations for a set of n elements is equal to n!, we can multiply the result of our n choose k by 6 (which is just a shorthand for multiplying by 3!, as 3! = 6). Putting that all together with a little bit of error checking, we have the following short Python solution:
#!/usr/bin/env python3
importmathdefnchoosek(n,k):n_fac=math.factorial(n)denominator=math.factorial(k)*math.factorial(n-k)returnn_fac/denominatordefhorses(num_horses):try:# check for non-integer but still numerical input
ifint(num_horses)!=num_horses:returnNoneexceptValueError:# check for non-numerical input
returnNoneifnum_horses<=3:returnnum_horsesreturnint(nchoosek(num_horses,3)*6)if__name__=="__main__":print(horses(15))print(horses(4))print(horses(1))print(horses("whoops"))print(horses(4.5))
edit: forgot to add code to check for floating-point numbers as input, whoops
Edit: forgot to convert the input to a number making all of the numbers that go through the function a string
// grabs the input, the number of horses, from the input tagconstcalculate=(horses=Number(document.querySelector("#horses").value))=>{//puts the answer into an empty divdocument.querySelector("#answer").innerHTML=horses!==parseInt(horses)?undefined:horses<4?horses:factorial(horses)/factorial(horses-3);}constfactorial=(n)=>{returnn?n*factorial(n-1):1;}
Time flies while you're having fun, but then one day your bio says something about being a 30+ year veteran in software engineering. Still, I've not seen it all, let alone done it all (yet).
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)
The word "combinations" being used here provides us with a good jumping-off point to figure out the solution - combinatorics! We can calculate the
nchoosek/binomial coefficient for the horses, wherenis the total number of horses, andkis equal to 3, since we're looking for 3 horses (gold, silver, and bronze winners). From there, we need to take into account that ordering - which horse won which position - matters to us; mathematical combinations are sets in which order doesn't normally matter. We'll need permutations for that part.So, knowing that the number of permutations for a set of
nelements is equal ton!, we can multiply the result of ournchoosekby 6 (which is just a shorthand for multiplying by3!, as3! = 6). Putting that all together with a little bit of error checking, we have the following short Python solution:edit: forgot to add code to check for floating-point numbers as input, whoops
Nice solution, you could also use the
itertoolspackage:dev.to/rafaacioly/comment/jkno
Shortest answer I could think of:
And in Standard ML, since it has type checking I can skip the int check:
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
Python solution 🐍
Here's my submission while I'm learning Nim.
My JavaScript solution:
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.
In Go.
Shouldn't that be "less than 3"? Shouldn't
horses(3)return 6?