- 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
Program to find first fit room from a list of rooms in Python
Suppose we have a list of numbers called rooms and another target value t. We have to find the first value in rooms whose value is at least t. If there is no such room, return -1.
So, if the input is like rooms = [20, 15, 35, 55, 30] t = 30, then the output will be 35. Because 30 is smaller than 35 and previous rooms are not sufficient for target 30.
To solve this, we will follow these steps −
for each room in rooms, do
if room >= t, then
Let us see the following implementation to get better understanding
def solve(rooms, t): for room in rooms: if room >= t: return room return -1 rooms = [20, 15, 35, 55, 30] t = 30 print(solve(rooms, t))
[20, 15, 35, 55, 30], 30
- Program to find closest room from queries in Python
- Program to find duplicate item from a list of elements in Python
- Program to find folded list from a given linked list in Python
- Python Program to find out the number of rooms in which a prize can be hidden
- Python program to find word score from list of words
- Python program to find N largest elements from a list
- Program to find total unique duration from a list of intervals in Python
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find length of longest interval from a list of intervals in Python
- Program to find sum of odd elements from list in Python
- Program to find mutual followers from a relations list in Python
- Python program to Find the first non-repeating character from a stream of characters?
- Python program to find all close matches of input string from a list
- Program to find number of restricted paths from first to last node in Python