Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ cd projects/algorithm-lab

3. if you do not have Phyton 3+ installed, you must use the console:
```
python main.py
python lalalal.py
```

***

this program shows you the results of insertion and merge sorts for each algorithm, it also shows some additional information, such as:
* time,
* swap counter,
* comparison counter.
this program will show you the solution to the problem of Jackie and Bananas using binary search.

counters represent the numbers of corresponding operations that occurred during the sorting process.
there are also doctests that can be used to check whether the methods work correctly.

***
# The condition of the problem
Горила Джекі із зоопарку Мюнхена любить їсти банани. На складі зоопарку є N кошиків (piles) з бананами, у і-тому кошику є певна кількість бананів Х. Кошики знаходяться під охороною, але охорона здійснює обхід зоопарку на Н годин, протягом якого Джекі може поласувати своєю улюбленою стравою. Джекі може з'їсти за годину К бананів. Кожну годину вона вибирає кошик з бананами і їсть К бананів звідти. Якщо кошик має менше, ніж К бананів, вона з'їдає всі банани з нього і більше не буде їсти бананів протягом цієї години. Джекі любить їсти повільно, але все ж хочеться закінчити споживання всіх бананів, перш ніж охоронці повернуться.

Визначіть мінімальне ціле число К таким чином, щоб Джекі могла з'їсти всі банани на складі протягом Н годин, поки повернеться охорона.

the list of objects is taken from *basin.csv* file. Each row there represents a new object as a sequence of its properties' values separated by commas. The order is:
```
address, volume_of_water, max_number_of_visitors
```
74 changes: 74 additions & 0 deletions lalalal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
def find_speed_for_eat_bananas(piles, hours):
"""
solves task from README.md

:param piles: array of bananas
:param hours: the time during which the monkey must eat all the bananas
:return: a value that satisfies the condition

>>> find_speed_for_eat_bananas([3, 6, 7, 11], 8)
4
>>> find_speed_for_eat_bananas([30, 11, 23, 4, 20], 5)
30
>>> find_speed_for_eat_bananas([30, 11, 23, 4, 20], 6)
23
>>> find_speed_for_eat_bananas([4,9,28,43],7)
15
"""
max_speed = 0
for count_of_pile in piles:
if max_speed < count_of_pile:
max_speed = count_of_pile
min_speed = 1

if len(piles) == hours:
return max_speed
return search_by_binary_tree(min_speed, max_speed, piles, hours)


def search_by_binary_tree(min_bananas, max_bananas, piles, hours):
"""
:param min_bananas: minimal value to start search
:param max_bananas: maximal value of bananas in piles
:param piles: array of piles with bananas
:param hours: hours monkey has to eat all bananas
:return: minimal most suitable value to pass task condition

>>> search_by_binary_tree(1, 30, [30, 11, 23, 4, 20], 6)
23
"""
while min_bananas < max_bananas:
middle = (min_bananas + max_bananas) // 2

if jackie_can_eat(piles, hours, middle):
max_bananas = middle
else:
min_bananas = middle + 1
return min_bananas


def jackie_can_eat(piles, hours_to_eat, middle):
"""
checks condition if monkey can eat all bananas with eating speed of probable_speed in hours_to_eat

:param piles: list of piles with bananas
:param hours_to_eat: hours monkey has to eat all bananas
:param middle: speed to check
:return: true if pass condition else false

>>> jackie_can_eat([3, 6, 7, 11], 8, 6)
True
>>> jackie_can_eat([3, 6, 7, 11], 8, 3)
False
"""
all_hours = 0
for count_of_pile in piles:
all_hours += count_of_pile // middle
if count_of_pile % middle != 0:
all_hours += 1
return all_hours <= hours_to_eat


if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)