-
Notifications
You must be signed in to change notification settings - Fork 343
Empty seats
There is a restaurant that can accommodate 1000 people. They number the seats in order and record them in a database table. After each guest sits down, the waiter will mark it in the seat table. When the guest leaves, this mark will be deleted. At a certain moment, the table is as follows:
Due to the frequent occurrence of multiple guests sitting together in consecutive seats, the restaurant requires a synchronized "free seat area table" to facilitate the search for such consecutive seats. This table is used to record which sections of available seats are currently available and the location of each section.
Please assist the restaurant administrator in generating a table of available seating areas from the seating chart above.
Merging the same content can be solved by the grouping function in esProc. While for only merging adjacent identical content, the group function has an option @o to group without sorting, which can perfectly solve this problem.
A | |
---|---|
1 | =T("Seatings.txt") |
2 | =A1.group@o(Availability; |
3 | =A2.select(Availability) |
https://try.esproc.com/splx?4nr
A1 reads the seating chart.
A2 groups by whether there are guests or not, and count the number of consecutive seats with or without guests in each segment, as well as the starting seat number:
A3 selects the empty seats result from it:
The problem of empty seats in practical situations is much more complex than the example above. There will be many rows of seats, and there will not be a situation of 1000 consecutive seats. Let's take a look at a more complex problem of empty seats.
The seating chart for a certain screening room in a cinema is as follows:
The seats in the screening hall are divided into three areas by two passages, with green indicating empty seats and red indicating occupied seats. Now we need to count the consecutive number and starting position of each empty seat segment, represented by row column numbers, such as 3-12 representing the 12th seat in the third row.
First, use a sequence to represent the seating situation, with each row of seats represented by a string of characters. Empty seats are represented by O, occupied seats are represented by X, and passages are represented by P. Then loop through each row of seats, similar to the previous example, group without sorting to count the number of consecutive seats and the starting seat number for each segment. Due to the presence of passage occupancy, when calculating the starting seat number, it is necessary to first obtain the corresponding seat number for each position. Finally, select the empty seats and merge the results from each row together.
A | |
---|---|
1 | OOOOOOPOOOOOOOOOOOOPOOOOOO |
2 | OOOOOOPOOOOOXXOOOOOPOOOOOO |
3 | OOOOOOOPOOOXXXXXXXOOPOOOOOOO |
4 | OXXOOOOOPOOOOOOOOOOOOPOXXOOOOO |
5 | OOOOOXXOOPOOOOXXXXXXXOPOOOOOXXOO |
6 | OOOXXXXXXXPOOOOOXOOOOOOPOOOXXXXXXX |
7 | OOOOOXXOOOPOOOXXXXOXXOOPOOOOOOXXOO |
8 | OOOOOOOOOOPOOOOOOOXXXXOPOOOOOOOOOO |
9 | OOOOOOOOOOPOOOOOXXXXXOOPOOOOOOOOOO |
10 | OOOOOOOOOOPOOOOOOOOOOOOPOOOOOOOOOO |
11 | OOOOOXXXXXPOOOXXOOOOOOOPOOOOOOXXXX |
12 | OOOOOOOXXOPOOOOOOOOOOOOPOOOOOOOOOX |
13 | =[A1:A12].(~.split()) |
14 | =A13.(a=0,~.(if(~=="P",0,a+=1))) |
15 | =A13.(r=#,rn=A14(#),~.groups@o(~:Status;count(~):NumberOfSeats, r/"-"/rn(#):StartingSeatNo)) |
16 | =A15.(~.select(Status=="O")).conj() |
https://try.esproc.com/splx?4DR
The first 12 rows record seat occupancy based on the seating chart, where O represents empty seats, X represents occupied seats, and P represents passages.
A13 splits each row of seats into a sequence by character as follows. A14 calculates the seat numbers for each position in each row, with the passage position set to 0:
A15 loops each row, grouping by seat status without sorting, and counting the number of consecutive seats with or without people in each segment, as well as the starting seat number. The starting seat number consists of the row number and the seat number obtained from A14:
A16 selects the empty seat records of each row and merge the results of each row using conj:
SPL Resource: SPL Official Website | SPL Blog | Download esProc SPL | SPL Source Code