'Python return occurence number of a specific item in the box
My problem sound like that, and I try to figure out how to implement in Python by basic steps.
We have a box which has capacity of 3 items. These 3 items are: Headphones,monitor,mouse
Every box will fill in the same order Order:mouse->headphones->monitor.
So If we specify the number of items from keyboard (ex input 29) how many monitors we will have in boxes and how many box we use?
Solution 1:[1]
First, you will need to get the number of boxes used. Assuming positive integers (not zero)
If input <= 3, boxes used = 1
If input % 3 == 0 (remainder of dividing input by 3 equals 0), and input >=3, boxes used = input / 3.
If input % 3 != 0 (remainder of dividing input by 3 is not 0) and input > 3, boxes used = (input / 3) + 1
Now to get the number of monitors. If input % 3 == 0, the number of monitors is equal to the number of boxes used. Else, the number of monitors will be one less than the number of boxes used.
Implementing the above in python3:
#!/usr/bin/env python3
import sys
def no_monitors(input):
if input < 0:
raise ValueError()
if input == 0:
boxes_used = 0
elif input <= 3:
boxes_used = 1
elif input % 3 == 0:
boxes_used = input // 3
else:
boxes_used = (input // 3) + 1
if input % 3 == 0:
monitors = boxes_used
else:
monitors = boxes_used - 1
return monitors, boxes_used
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <number_of_items>")
else:
try:
mon, boxes = no_monitors(int(sys.argv[1]))
print(f"Number of boxes = {boxes}\nNumber of monitors = {mon}")
except ValueError:
print("Please enter a valid positive integer!")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
