12
1
Task
A theatre has 10 rows, labelled A
to J
from front to back, and 15 seats in each row,
numbered 1 to 15 from left to right.
The program uses the following rules to choose the best seats.
- Rule 1: All seats in one booking must be in the same row, next to each other.
- Rule 2: The seats must be as close to the front as possible, then as close to the left as possible (lowest letter, then lowest number)
Write a function which takes the number of tickets wanted as an integer input (n
), and outputs the best seats available in a list of length n
.
Your program should:
- Output
-1
if 1 > Input or Input > 15* - Output
-1
if the seats aren't available* - Have a function
B(n)
that the user can use to input the desired number of seats.
*You can output the -1 in a list if it makes it easier
Examples
I/O
Calling B(5)
on a new array should return [A1, A2, A3, A4, A5]
Calling B(2)
after that should then return [A6, A7]
Calling B(10)
after that should then return [B1, B2, ... B9, B10]
Calling B(-1)
should always return -1
Un-golfed Solution Python
Theatre = [ [False] * 16 ] * 11
def B(n):
if 0 <= n <= 15:
for i in range(10):
for j in range(15-n+1):
try:
if not Theatre[i][j]:
if not Theatre[i][j + n]:
row = i
start = j
List = []
for q in range(n):
List.append(chr(row + 65) + str(start + q + 1))
Theatre[row][start + q] = True
return List
except:
break
return -1
1Is "Have hardcoded a list of seats in a two dimensional array" necessary? There are numerous ways to do this without that; the requirement really restricts solutions. – Justin – 2014-05-23T01:42:34.990
2You say the 2-D array must be hard-coded, but your Python example doesn't even hard-code it, it uses a comprehension to create a new list at runtime. – Tony Ellis – 2014-05-23T01:53:06.570
Can I return ['-1'] instead of -1? – Οurous – 2014-05-23T01:55:33.520
You guys are right, amending now... – Harry Beadle – 2014-05-23T02:01:16.963
@Ourous Sure, that sounds reasonable :) – Harry Beadle – 2014-05-23T02:02:02.960
6Why even mention "a list of seats in a two dimensional array"? That sounds like an implementation detail and if somebody creates a program that satisfies the required output without using an array, there should be no problem with that. – Greg Hewgill – 2014-05-23T02:19:30.430
@GregHewgill Ammended – Harry Beadle – 2014-05-23T02:21:43.837
2what if input is 0? – edc65 – 2014-05-23T15:17:47.183
1@edc65 I always make my nonexistent movie theater patrons sit in the best spot of the theater, on another patron's lap if need be. They never notice. – Adam Davis – 2014-05-23T15:56:53.297
@edc65 As the rules say, output
-1
– Harry Beadle – 2014-05-24T23:09:43.560OK, I'll change my answer according to this. But that's not what the rules say. "-1 if 0 > Input ..." and 0 is not > 0. I suggest changing to if "-1: if input < 1 or input > 15." – edc65 – 2014-05-24T23:20:15.020
Does the returned list need to be sorted or is it okay to just have the correct seats? – nderscore – 2014-05-25T17:18:08.750
@nderscore Just a list, sorting doesn't matter – Harry Beadle – 2014-05-25T17:23:15.620