- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Egg Dropping Puzzle
In this article, we will learn about the solution to the problem statement given below.
Problem statement − Suppose that we want to know which stories in a 40-story building are safe to drop the eggs from, and which of those will cause the eggs to get damaged on landing with the help of eggs. We need to display a minimum number of trails to check the stories.
Now let’s observe the solution in the implementation below −
# dynamic programming INT_MAX = 32767 # to get minimum trials def eggDrop(n, k): # intialization eggFloor = [[0 for x in range(k + 1)] for x in range(n + 1)] # base case for i in range(1, n + 1): eggFloor[i] = 1 eggFloor[i] = 0 # We always need j trials for j in range(1, k + 1): eggFloor[j] = j # Fill rest of the entries for i in range(2, n + 1): for j in range(2, k + 1): eggFloor[i][j] = INT_MAX for x in range(1, j + 1): res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x]) if res < eggFloor[i][j]: eggFloor[i][j] = res return eggFloor[n][k] # main n = 4 k = 40 print("Minimum number of trials in worst case scenario with " + str(n) + " eggs and "+ str(k) + " floors is " + str(eggDrop(n, k)))
Minimum number of trials in worst case scenario with 4 eggs and 40 floors is 6
All the variables are declared in the local scope and their references are seen in the figure above.
In this article, we have learned about how we can make a Python Program for Egg Dropping Puzzle.
- Egg Dropping Puzzle
- C Program for Egg Dropping Puzzle - DP-11?
- A Puzzle using C Program
- Super Egg Drop in C++
- A Boolean Array Puzzle In C Program?
- Program to find number of steps to solve 8-puzzle in python
- Number of Valid Words for Each Puzzle in C++
- Dropping a SAP HANA database view
- Dropping Unique constraint from MySQL table?
- Dropping a MySQL Table using NodeJS
- Packet Queuing and Dropping in Routers
- A nested loop puzzle?
- C++ Sum Array Puzzle
- C/C++ Pointer Puzzle?
- Sliding Puzzle in C++