From 3d5d86d67cd9439db77fab45a6ec029e527612a7 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Mon, 9 Nov 2020 12:49:11 +0200 Subject: [PATCH 1/2] added code for second lab with binary search --- lalalal.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 lalalal.py create mode 100644 main.py diff --git a/lalalal.py b/lalalal.py new file mode 100644 index 0000000..11a3594 --- /dev/null +++ b/lalalal.py @@ -0,0 +1,75 @@ +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 + """ + all_bananas = max_speed = 0 + for count_of_pile in piles: + all_bananas += count_of_pile + if max_speed < count_of_pile: + max_speed = count_of_pile + min_speed = all_bananas // hours + + 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) diff --git a/main.py b/main.py new file mode 100644 index 0000000..25dc83c --- /dev/null +++ b/main.py @@ -0,0 +1,70 @@ +def find_speed_for_eat_bananas(piles, hours): + """ + + :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], 6) + 23 + >>> find_speed_for_eat_bananas([30, 11, 23, 4, 20], 5) + 30 + >>> find_speed_for_eat_bananas([4,9,28,43],7) + 15 + + """ + all_bananas = max_speed = 0 + for count_of_pile in piles: + all_bananas += count_of_pile + if max_speed < count_of_pile: + max_speed = count_of_pile + min_speed = all_bananas // hours + + 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: + :param max_bananas: + :param hours: + :param piles: + :return: + >>> search_by_binary_tree(1, 31, [31, 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_can_eat,middle): + """ + + :param middle: + :param piles: + :param hours_can_eat: + :return: + >>> 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_can_eat + + +if __name__ == '__main__': + import doctest + doctest.testmod(verbose=True) From f5d62a49a0e3f729a8c870179e32496d52618b13 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 10 Nov 2020 15:26:39 +0200 Subject: [PATCH 2/2] Delete main.py --- main.py | 70 --------------------------------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 main.py diff --git a/main.py b/main.py deleted file mode 100644 index 25dc83c..0000000 --- a/main.py +++ /dev/null @@ -1,70 +0,0 @@ -def find_speed_for_eat_bananas(piles, hours): - """ - - :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], 6) - 23 - >>> find_speed_for_eat_bananas([30, 11, 23, 4, 20], 5) - 30 - >>> find_speed_for_eat_bananas([4,9,28,43],7) - 15 - - """ - all_bananas = max_speed = 0 - for count_of_pile in piles: - all_bananas += count_of_pile - if max_speed < count_of_pile: - max_speed = count_of_pile - min_speed = all_bananas // hours - - 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: - :param max_bananas: - :param hours: - :param piles: - :return: - >>> search_by_binary_tree(1, 31, [31, 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_can_eat,middle): - """ - - :param middle: - :param piles: - :param hours_can_eat: - :return: - >>> 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_can_eat - - -if __name__ == '__main__': - import doctest - doctest.testmod(verbose=True)