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

      • 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

Updated on: 11-Oct-2021

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements