Skip to content

Instantly share code, notes, and snippets.

@sanjarcode
Last active December 20, 2022 05:45
Show Gist options
  • Save sanjarcode/e9c5c76f1c2ce9b0f7611ef91e306281 to your computer and use it in GitHub Desktop.
Save sanjarcode/e9c5c76f1c2ce9b0f7611ef91e306281 to your computer and use it in GitHub Desktop.
Code files
import unittest
class PlaneSeating:
def __init__(self, seat_block_column_n_row, number_of_passsengers):
self.seat_block_column_n_row = seat_block_column_n_row
self.number_of_passsengers = number_of_passsengers
# calculate number of seats of each type
def seatTypeCount(self):
self.aisle_seats = 0
if len(self.seat_block_column_n_row) > 1:
for block_i in range(len(self.seat_block_column_n_row)):
if block_i == 0 or block_i == len(
self.seat_block_column_n_row) - 1:
self.aisle_seats += self.seat_block_column_n_row[block_i][
1]
else:
self.aisle_seats += 2 * self.seat_block_column_n_row[
block_i][1]
self.window_seats = self.seat_block_column_n_row[0][
1] + self.seat_block_column_n_row[-1][1]
self.center_seats = self.number_of_passsengers - self.aisle_seats - self.window_seats
# starting seat numbers
def startingSeatNumbers(self):
self.aisle_seat_number = 0
if self.aisle_seats:
self.aisle_seat_number = 1
self.window_seat_number = self.aisle_seats + 1
self.center_seat_number = self.aisle_seats + self.window_seats + 1
# calculate max height
def calculateMaxHeight(self):
self.max_height = -1
for _, row in self.seat_block_column_n_row:
if row > self.max_height:
self.max_height = row
# remove extra seats
def removeExtraneosSeats(self):
def removeExtraneous(seat_number):
if seat_number > self.number_of_passsengers:
return ' '
return seat_number
for plane_row_i in range(len(self.plane)):
self.plane[plane_row_i] = list(
map(removeExtraneous, self.plane[plane_row_i]))
return self.plane
def computeSeats(self):
self.seatTypeCount()
self.startingSeatNumbers()
self.calculateMaxHeight()
self.plane = []
for plane_row_i in range(self.max_height):
plane_row = []
for block_i in range(len(self.seat_block_column_n_row)):
if plane_row_i < self.seat_block_column_n_row[block_i][1]:
# block row exists at this height
for block_col_i in range(
self.seat_block_column_n_row[block_i][0]):
# check if seat is aisle, window or center
aisle_seat = False
window_seat = False
if self.aisle_seats and (
block_col_i == 0 or block_col_i
== self.seat_block_column_n_row[block_i][0] -
1):
# if not window seat
if not (
(block_col_i == 0 and block_i == 0) or
(block_col_i
== self.seat_block_column_n_row[block_i][0] -
1 and block_i
== len(self.seat_block_column_n_row) - 1)):
aisle_seat = True
# left aisle seat
if block_col_i == 0:
# print(self.aisle_seat_number, end=' ')
plane_row.append(self.aisle_seat_number)
self.aisle_seat_number += 1
# right aisle seat
if block_col_i == self.seat_block_column_n_row[
block_i][0] - 1:
# print(self.aisle_seat_number, end=' || ')
plane_row.append(self.aisle_seat_number)
self.aisle_seat_number += 1
# is window seat
if (block_col_i == 0 and block_i == 0) or (
block_col_i
== self.seat_block_column_n_row[block_i][0] - 1
and block_i
== len(self.seat_block_column_n_row) - 1):
window_seat = True
# right window, newline
if (block_col_i
== self.seat_block_column_n_row[block_i][0]
- 1 and block_i
== len(self.seat_block_column_n_row) - 1):
# print(self.window_seat_number)
pass
else:
# print(self.window_seat_number, end=' ')
pass
plane_row.append(self.window_seat_number)
self.window_seat_number += 1
# is neither window nor aisle - i.e. center seat
if aisle_seat == False and window_seat == False:
# print(self.center_seat_number, end=' ')
plane_row.append(self.center_seat_number)
self.center_seat_number += 1
self.plane.append(plane_row)
return self.removeExtraneosSeats()
def runTests(inputOutputs):
testCount = 0
passCount = 0
for seatMatrix, passengers, idealOutput in inputOutputs:
testCount += 1
try:
output = PlaneSeating(seatMatrix, passengers).computeSeats()
if output == idealOutput:
print(f'Test {testCount} passed.')
passCount += 1
else:
print(f'Test {testCount} failed.')
print(f'\tOutput: \t\b\b{output}')
print(f'\tIdeal Output: {idealOutput}')
except Exception as e:
print(f'Error occured in test {testCount}: {e}')
if testCount == passCount:
print('All tests passed!!')
if __name__ == '__main__':
# [[seatMatrix, passengers, output]]
inputOutputs = [
[
[[3, 2], [4, 3], [2, 3], [3, 4]],
30,
[[19, 25, 1, 2, 26, 27, 3, 4, 5, 6, 28, 20],
[21, 29, 7, 8, 30, ' ', 9, 10, 11, 12, ' ', 22],
[13, ' ', ' ', 14, 15, 16, 17, ' ', 23], [18, ' ', 24]]
],
[
[[1, 1]],
1,
[[1]]
],
[
[[1, 1]],
0,
[[' ']]
],
[
[[1, 1]],
12,
[[1]]
],
[
[[1, 6]],
12,
[[1], [2], [3], [4], [5], [6]]
],
[
[[3, 2]],
10,
[[1, 5, 2], [3, 6, 4]]
],
[
[[3, 2]],
1,
[[1, ' ', ' '], [' ', ' ', ' ']]
]
# Add [seatMatrix, passengers, idealOutput] here
]
runTests(inputOutputs)
@sanjarcode
Copy link
Author

sanjarcode commented Jun 28, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment