From 1867830aea001d876acfb6f9bcd10610bf404db4 Mon Sep 17 00:00:00 2001 From: Neeramitra Reddy <46085683+neeru1207@users.noreply.github.com> Date: Sun, 14 Apr 2019 16:46:51 +0530 Subject: [PATCH] Added Egg Drop.py (#972) --- Egg_Drop/Egg_Drop.py | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Egg_Drop/Egg_Drop.py diff --git a/Egg_Drop/Egg_Drop.py b/Egg_Drop/Egg_Drop.py new file mode 100644 index 0000000000..cdbaf53d20 --- /dev/null +++ b/Egg_Drop/Egg_Drop.py @@ -0,0 +1,57 @@ +# Egg Drop problem + +# Function for recursive approach +def eggDropRecursive(egg, floor): + if floor == 0: + return 0 + if egg == 1: + return floor + min = 999999 + for i in range(1, floor+1): + sol = max(eggDropRecursive(egg-1, i-1), eggDropRecursive(egg, Floor-i)) + 1 + if sol < min: + min = sol + return min + +# Function for recursive dynamic programming approach +def eggDropRecursiveDP(egg, floor, mem): + if floor == 0: + return 0 + if egg == 1: + return floor + if mem[egg][floor] != 0: + return mem[egg][floor] + min = 999999 + for i in range(1, floor + 1): + sol = max(eggDropRecursiveDP(egg-1, i-1, mem), eggDropRecursiveDP(egg, floor-i, mem)) + 1 + if sol < min: + min = sol + mem[egg][floor] = min + return min + +# Function for iterative dynamic programming approach +def eggDropIterativeDP(eggs, floors, mem): + for e in range(1, eggs+1): + for f in range(1, floors+1): + if f == 0: + mem[e][f] = 0 + elif e == 1: + mem[e][f] = f + else: + min = 999999 + for i in range(1, f+1): + sol = max(mem[e-1][i-1], mem[e][f-i]) + 1 + if sol < min: + min = sol + mem[e][f] = min; + return mem[eggs][floors] + +# Taking the user input for the number of eggs and floors +egg = int(input("Enter the number of eggs: ")) +floor = int(input("Enter the number of floors: ")) +mem = [[0 for i in range(0, floor+2)] for j in range(0, egg+2)] +print(eggDropIterativeDP(egg, floor, mem)) + +#Sample Output +#Enter the number of eggs: 5 +#Enter the number of floors: 100