
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
return room
-
return -1
Example
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))
Input
[20, 15, 35, 55, 30], 30
Output
35
Advertisements