From 3f989fb9e5870485e1721c85955e66b0c18baa74 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Mon, 9 Nov 2020 21:25:38 +0200 Subject: [PATCH 1/4] added file with binary search --- lalalal.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lalalal.py diff --git a/lalalal.py b/lalalal.py new file mode 100644 index 0000000..c6f9906 --- /dev/null +++ b/lalalal.py @@ -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) From c7c783a9cea2f2817de4a4bde435b160b20c46d7 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 10 Nov 2020 14:13:24 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6a4f722..1eab712 100644 --- a/README.md +++ b/README.md @@ -21,19 +21,11 @@ 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. - -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 -``` +there are also doctests that can be used to check whether the methods work correctly. From 4bedc3f1d156c50422421c1562d54bbd534898de Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 10 Nov 2020 14:18:11 +0200 Subject: [PATCH 3/4] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 1eab712..c80b895 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,10 @@ python lalalal.py this program will show you the solution to the problem of Jackie and Bananas using binary search. there are also doctests that can be used to check whether the methods work correctly. + +*** +#The condition of the problem +Горила Джекі із зоопарку Мюнхена любить їсти банани. На складі зоопарку є N кошиків (piles) з бананами, у і-тому кошику є певна кількість бананів Х. Кошики знаходяться під охороною, але охорона здійснює обхід зоопарку на Н годин, протягом якого Джекі може поласувати своєю улюбленою стравою. Джекі може з'їсти за годину К бананів. Кожну годину вона вибирає кошик з бананами і їсть К бананів звідти. Якщо кошик має менше, ніж К бананів, вона з'їдає всі банани з нього і більше не буде їсти бананів протягом цієї години. Джекі любить їсти повільно, але все ж хочеться закінчити споживання всіх бананів, перш ніж охоронці повернуться. + +Визначіть мінімальне ціле число К таким чином, щоб Джекі могла з'їсти всі банани на складі протягом Н годин, поки повернеться охорона. + From 0f86baf17a5885a19ec227ac914f877d2f64ea7d Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 10 Nov 2020 14:18:36 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c80b895..3e0b5dd 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ this program will show you the solution to the problem of Jackie and Bananas usi there are also doctests that can be used to check whether the methods work correctly. *** -#The condition of the problem +# The condition of the problem Горила Джекі із зоопарку Мюнхена любить їсти банани. На складі зоопарку є N кошиків (piles) з бананами, у і-тому кошику є певна кількість бананів Х. Кошики знаходяться під охороною, але охорона здійснює обхід зоопарку на Н годин, протягом якого Джекі може поласувати своєю улюбленою стравою. Джекі може з'їсти за годину К бананів. Кожну годину вона вибирає кошик з бананами і їсть К бананів звідти. Якщо кошик має менше, ніж К бананів, вона з'їдає всі банани з нього і більше не буде їсти бананів протягом цієї години. Джекі любить їсти повільно, але все ж хочеться закінчити споживання всіх бананів, перш ніж охоронці повернуться. Визначіть мінімальне ціле число К таким чином, щоб Джекі могла з'їсти всі банани на складі протягом Н годин, поки повернеться охорона.