DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #153 - Horse Race Gamble

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)

Collapse
 
rdandekarslb profile image
Rashmin Dandekar

Ruby

def horses(num)
  if !(num % 1 == 0) || !(num.is_a? Numeric)
    return "undefined"
  elsif num < 3
    return num
  else
    return num*(num-1)*(num-2)
  end
end

puts horses(12)
puts horses(2)
puts horses(11)
puts horses(2.5)
puts horses("a")


Collapse
 
nickholmesde profile image
Nick Holmes

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.

Collapse
 
strickolas profile image
Nicholas Saccente

Here's my submission while I'm learning Nim.

proc fac(n: int): int =
  if n <= 1: return 1
  result = n * fac(n - 1)

proc nPr(n: int, r: int): int =
  result = fac(n) div fac(n-r)

proc `%`(n: int, r: int): int =
  result = nPr(n, r)

proc horses(n: int): int =
  if n <= 3: return n
  result = n % 3

when isMainModule:
  echo horses(15)    #=> 2730
  echo horses(12)    #=> 1320
  echo horses(2)     #=> 2
  echo horses(11)    #=> 990
  # echo horses(a)   #=> compiler error

Collapse
 
idanarye profile image
Idan Arye

If the number of horses is 3 or less, return the input value.

Shouldn't that be "less than 3"? Shouldn't horses(3) return 6?

Collapse
 
rafaacioly profile image
Rafael Acioly

Python solution 🐍

from intertools import permutations

WINNERS_QUANTITY = 3


def winner_combination(horses_amount: int) -> int:
    if horses_amount <= WINNERS_QUANTITY:
        return horses_amount

    possibilities = list(permutations(
        range(horses_amount), WINNERS_QUANTITY
    ))
    return len(possibilities)

Collapse
 
centanomics profile image
Cent • Edited

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


// grabs the input, the number of horses, from the input tag
const calculate = (horses = Number(document.querySelector("#horses").value)) => {

  //puts the answer into an empty div
  document.querySelector("#answer").innerHTML = 
    horses !== parseInt(horses) ? undefined :
    horses < 4 ? horses :
    factorial(horses)/factorial(horses - 3);
}

const factorial = (n) => {
  return n ? n * factorial(n - 1) : 1;
}

Collapse
 
bamartindev profile image
Brett Martin • Edited

Shortest answer I could think of:

const horses = n => { 
    if(Number.isInteger(n)) return n < 3 ? n : n * (n-1) * (n-2)
};

And in Standard ML, since it has type checking I can skip the int check:

fun horses(n: int) : int = if n < 3 then n else n * (n - 1) * (n - 2)
Collapse
 
nickholmesde profile image
Nick Holmes

The code (F#)

let WinnerCombinations n =
    if (n <= 3) then None
    else Some (n * (n-1) * (n-2))

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)

Collapse
 
savagepixie profile image
SavagePixie • Edited

My JavaScript solution:

const winCombos = n => !Number.isInteger(n)
    ? undefined
    : n < 4
    ? n
    : n * (n - 1) * (n - 2)
Collapse
 
vaibhavyadav1998 profile image
Vaibhav Yadav • Edited

In Go.

func horses(num uint64) uint64 {
    if num <= 3 {
        return num
    }
    return num * (num - 1) * (num - 2)
}