Check if it is possible to survive on Island in Python

PythonServer Side ProgrammingProgramming

Suppose there is an island. There is only one store in that location, this store remains open always except Sunday. We have following values as input −

  • N (Maximum number of food someone can buy each day).
  • S (Number of days someone is required to survive).
  • M (Number of food required each day to survive).

If it is Monday, and we need to survive for the next S number of days. We have to check whether we can survive or not, if we can find the minimum number of days on which we need to buy food, so that we can survive the next S number of days.

So, if the input is like S = 12, N = 24, M = 3, then the output will be True and minimum number of days on which we need to buy food is 2, as we can survive 8 days (from current Monday to next Monday) using 24 units of food, then buy again 12 units for next 4 days.

To solve this, we will follow these steps −

  • if (N * 6 < M * 7 and S > 6) or M > N, then
    • return False
  • otherwise,
    • count := quotient of (M * S) / N
    • if (M * S) is divisible by N, then
      • count := count + 1
  • return True and count

Example

Let us see the following implementation to get better understanding −

 Live Demo

def solve(S, N, M):
   if ((N * 6) < (M * 7) and S > 6) or M > N:
      return False
   else:
      count = (M * S) // N
      if ((M * S) % N) != 0:
         count += 1
      return (True, count)
S = 12
N = 24
M = 3
print(solve(S, N, M))

Input

12, 24, 3

Output

(True, 2)
raja
Published on 19-Jan-2021 04:31:11
Advertisements