
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if it is possible to survive on Island in Python
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 −
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)
- Related Articles
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to transform one string to another in Python
- Check if it is possible to serve customer queue with different notes in Python
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Check if it is possible to convert one string into another with given constraints in Python
- Check if it possible to partition in k subarrays with equal sum in Python
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python
- Check if it is possible to move from (0, 0) to (x, y) in N steps in Python
- Check if it is possible to draw a straight line with the given direction cosines in Python
- Check if it is possible to form string B from A under the given constraint in Python
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python
- Is it possible in Android to check if a notification is visible or canceled?
