Throwing cats out of windows

前端 未结 8 971
别那么骄傲
别那么骄傲 2020-12-04 04:32

Imagine you\'re in a tall building with a cat. The cat can survive a fall out of a low story window, but will die if thrown from a high floor. How can you figure out the lon

相关标签:
8条回答
  • 2020-12-04 05:27

    I took a slightly different method to produce a solution.

    I started by working out the maximum floor that could be covered using x cats and y guesses using the following method.

    Start with 1 floor and keep increasing the number of guesses while keeping track of floors checked, which guess they were checked on and how many cats were remaining for each floor.
    Repeat this up to y times.

    This very inefficient code to compute the given answer but nonetheless useful for small number of cats / floors.

    Python code:

    def next_step(x, guess):
      next_x = []
      for y in x:
        if y[0] == guess:
          if y[1] != 1:
            next_x.append((guess+1, y[1] - 1))
        next_x.append(y)
        if y[0] == guess:
          next_x.append((guess+1, y[1]))
      return next_x
    
    x = [(1, TOTAL_NUM_CATS)]
    current_floor = 1
    while len(x) <= TOTAL_NUM_FLOORS:
      x = next_step(x, current_floor)
      current_floor += 1
      print len(x)
    

    For 2 cats the maximum floors that can be identified in x guesses is:
    1, 3, 6, 10, 15, 21, 28...

    For 3 cats:
    1, 3, 7, 14, 25, 41, 63...

    For 4 cats:
    1, 3, 7, 15, 30, 56, 98...

    After extensive research (mostly involving typing numbers sequences into OEIS) I noticed that the maximum floors for x follows a combination piecewise pattern.

    For 2 cats:
    n < 2 : 2^n - 1
    n >= 2 : C(n, 1) + C(n, 2)

    For 3 cats:
    n < 3 : 2^n - 1
    n >= 3 : C(n, 1) + C(n, 2) + C(n, 3)

    For 4 cats:
    n < 4 : 2^n - 1
    n >= 4 : C(n, 1) + C(n, 2) + C(n, 3) + C(n, 4)

    From here I took the easy approach of simple incrementing n until I pass the required number of floors.

    Python code:

    def find_smallest(floors, eggs):
      maximum_floors = 0
      n = 0
      while maximum_floors < floors:
        maximum_floors = 0
        n += 1
        if n < eggs:
          maximum_floors = 2**n - 1
        else:
          count = 0
          for x in xrange(1, eggs+1):
            maximum_floors += combination(n, x)
      print n
    

    This gives the correct solution for (100, 2) = 14.
    For anyone that wishes to check something less trivial, it gives (1 000 000, 5) = 43.

    This runs in O(n) where n is the answer to the problem (the more cats the better).
    However I'm sure a somebody with a higher level of mathematics could simplify the piecewise formulas to compute in O(1).

    0 讨论(0)
  • 2020-12-04 05:28

    According to a recent episode of Radiolab (about "Falling"), a cat reaches terminal velocity by the 9th floor. After that, it relaxes and is less likely to be hurt. There are completely uninjured cats after a fall from above the 30th. The riskiest floors are 5th to 9th.

    0 讨论(0)
提交回复
热议问题