Skip to content

Commit b194632

Browse files
committed
feat (day 146): implement left-handed seat availability check with orientation-aware neighbor rules
1 parent 590d7c7 commit b194632

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Left-Handed Seat at the Table
3+
Given a 4x2 matrix (array of arrays) representing the seating arrangement for a meal, determine how many seats a left-handed person can sit at.
4+
5+
A left-handed person cannot sit where a right-handed person would be in the seat to the immediate left of them.
6+
In the given matrix:
7+
8+
An "R" is a seat occupied by a right-handed person.
9+
An "L" is a seat occupied by a left-handed person.
10+
An "U" is an unoccupied seat.
11+
Only unoccupied seats are available to sit at.
12+
The seats in the top row are facing "down", and the seats in the bottom row are facing "up" (like a table), so left and right are relative to the seat's orientation.
13+
Corner seats only have one seat next to them.
14+
For example, in the given matrix:
15+
16+
[
17+
["U", "R", "U", "L"],
18+
["U", "R", "R", "R"]
19+
]
20+
The top-left seat is cannot be sat in because there's a right-handed person to the left. The other two open seats can be sat in because there isn't a right-handed person to the left.
21+
"""
22+
23+
import unittest
24+
25+
class LeftHandedSeatTest(unittest.TestCase):
26+
27+
def test1(self):
28+
self.assertEqual(find_left_handed_seats([["U", "R", "U", "L"], ["U", "R", "R", "R"]]), 2)
29+
30+
def test2(self):
31+
self.assertEqual(find_left_handed_seats([["U", "U", "U", "U"], ["U", "U", "U", "U"]]), 8)
32+
33+
def test3(self):
34+
self.assertEqual(find_left_handed_seats([["U", "R", "U", "R"], ["L", "R", "R", "U"]]), 0)
35+
36+
def test4(self):
37+
self.assertEqual(find_left_handed_seats([["L", "U", "R", "R"], ["L", "U", "R", "R"]]), 1)
38+
39+
def test5(self):
40+
self.assertEqual(find_left_handed_seats([["U", "R", "U", "U"], ["U", "U", "L", "U"]]), 5)
41+
42+
43+
def find_left_handed_seats(table):
44+
45+
rows, cols = len(table), len(table[0])
46+
47+
count = 0
48+
49+
for r in range(rows):
50+
for c in range(cols):
51+
if table[r][c] != 'U':
52+
continue
53+
# Determine immediate-left neighbor based on orientation
54+
left_c = c + 1 if r == 0 else c - 1
55+
if(0 <= left_c < cols):
56+
if table[r][left_c] == 'R':
57+
continue
58+
59+
count += 1
60+
61+
return count
62+
63+
64+
65+
if __name__ == "__main__":
66+
unittest.main()

0 commit comments

Comments
 (0)