3
\$\begingroup\$

Question: You're in a hotel and you forgot what room number you were in but remember that the sum of it's divisors is greater than the number, yet there is no subset of those divisors that add up to the number itself. There are 100 rooms in the Hotel, what's your room number?

My Answer in CoffeeScript: Looking for constructive criticism of my solution, as well as styling suggestions specific to CoffeeScript.

# hotelRoom.coffee
# splashinn

findRoom = (totalRooms) ->
  findDivisors = (number) ->
    divisors = []
    iterator = number
    divisors.push iterator  if number % iterator is 0  while iterator--
    divisors

  # Returns true or false based on whether the number is found in the sum of array subsets
  isSubsetSum = (number, array) ->
    hasSubset = false
    (findSubset = (total, numbers) ->
      not hasSubset and (hasSubset = total is number)
      return  if hasSubset or total > number
      numbers.forEach (num, index) ->
        findSubset total + num, numbers.slice(0, index).concat(numbers.slice(index + 1))
    ) 0, array
    hasSubset

  # Need a simple helper method that returns the sum of an array
  sumArray = (array) ->
    array.reduce ((memo, num) ->
      memo + num
    ), 0

  # Find the room using the provided functions
  divisors = undefined
  room = 0

  while room <= totalRooms
    divisors = findDivisors(room)

    # The sum of all the divisors must be greater than the number
    return room  if sumArray(divisors) > room and not isSubsetSum(room, divisors)
    room++
  0 # No room number found
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.