Skip to content

Commit 63d31ed

Browse files
Merge pull request #629 from Samclouder/master
Created a python file for the Egg Dropping Puzzle
2 parents b14613e + 5f83295 commit 63d31ed

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#A Dynamic Programming based Python Program for the Egg Dropping Puzzle
2+
import sys
3+
4+
# Function to get minimum number of trials
5+
# needed in worst case with n eggs and k floors
6+
def eggDrop(n, k):
7+
8+
# If there are no floors, then no trials needed. If there is one floor, one trial needed.
9+
if (k == 0 or k == 1):
10+
return k
11+
12+
# We need k trials for one egg and k floors
13+
if (n == 1):
14+
return k
15+
16+
min = sys.maxsize
17+
18+
# Consider all droppings from 1st floor to kth floor and return the minimum of these values plus 1.
19+
for x in range(1, k + 1):
20+
21+
res = max(eggDrop(n - 1, x - 1), eggDrop(n, k - x))
22+
if (res < min):
23+
min = res
24+
25+
return min + 1
26+
27+
28+
if __name__ == "__main__":
29+
30+
n = 2
31+
k = 36
32+
#or
33+
#n=int(input("Enter number of eggs : "))
34+
#k=int(input("Enter number of floors : "))
35+
print("Minimum number of trials in worst case with", n, "eggs and", k, "floors is", eggDrop(n, k))

0 commit comments

Comments
 (0)