Airplane Seating Problem

Rules for seating

Isha Shaw
3 min readDec 26, 2022
  1. Always seat passengers starting from the front row to back, starting from the left to the right.
  2. Fill aisle seats first followed by window seats followed by center seats.

Solution

This is a Python script that assigns seats on a plane to passengers. The script begins by reading the input in a JSON-formatted string representing the columns and rows on the plane(A 2D array).

if __name__ == "__main__":
input_string = input('Enter the seats as arrays ')
array = json.loads(input_string)

row = max(int(e[0]) for e in array)
col = max(int(e[1]) for e in array)
seats = fill_with_m_a_and_w(array)
obj = {}

obj = replace_with_number('A', 1, seats, col, row)
obj = replace_with_number('W', obj['counter'], obj['seats'], col, row)
obj = replace_with_number('M', obj['counter'], obj['seats'], col, row)
print_values(seats, col, row)

The function fill_with_m_a_and_wbegin by creating an empty 2D array called seats and then using a nested loop to fill this array with 'M' values. Next, it uses another nested loop to replace the first and last elements in each subarray with 'A' values, which represents aisle seats. Finally, it uses another loop to replace the first element in the first subarray and the last element in the last subarray with 'W' values, which represents window seats.

def fill_with_m_a_and_w(array):
seats = []
for i in range(len(array)):
seats.append([["M"] * array[i][0] for _ in range(array[i][1])])
#print(seats)
for i in range(len(seats)):
for j in range(len(seats[i])):
seats[i][j][0] = "A"
seats[i][j][-1] = "A"
for i in range(len(seats[0])):
seats[0][i][0] = "W"
for i in range(len(seats[-1])):
seats[-1][i][-1] = "W"

return seats

The functionreplace_with_numberuses a nested loop to iterate through each element in the 2D array. If the element is equal to the seat preference passed to the function (e.g. ‘A’, ‘W’, or ‘M’), the function replaces the element with the current value of the counter and increments the counter.

This function is used to assign seats to passengers with a particular preference (aisle, window, or middle). It is called multiple times, with different seat preferences passed as arguments, to assign seats to all the passengers in the queue. The counter is used to keep track of which seat should be assigned next.

def replace_with_number(val, counter, seats, col_size, row_size):
for i in range(col_size):
for j in range(row_size):
try:
if seats[j] is None or seats[j][i] is None:
continue
except IndexError:
continue
try:
for k in range(len(seats[j][i])):
if seats[j][i][k] == val:
seats[j][i][k] = counter
counter += 1
except IndexError:
continue
return {'seats': seats, 'counter': counter}

The function called print_values use a nested loop to iterate through each element in the 2D array and append its value to a string called string_j. If the element is None, the function appends a hyphen ('-') to the string instead. After processing each element, the function appends a comma (',') to the string. When it finishes processing a row, the function appends a newline character ('\n') to the string. Finally, it prints the resulting string.

def print_values(seats, col_size, row_size):
string_j = ''
for i in range(col_size):
for j in range(row_size):
try:
if seats[j] is None or seats[j][i] is None:
string_j += "- "
continue
except IndexError:
continue

try:
for k in range(len(seats[j][i])):
string_j += f"{seats[j][i][k]} "
except IndexError:
continue
string_j += ","
string_j += "\n"
print(string_j)

This function is used to print out the final seat assignments for each passenger.

You can find the whole code here.

--

--