55.1k views
4 votes
Given num_rows and num_cols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat. Sample output with inputs: 2 3

User Bbnm
by
5.0k points

1 Answer

4 votes

Answer:

The solution code is written in Python:

  1. alphabets = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
  2. user_input = input("Enter row and columns: ")
  3. myArr = user_input.split(" ")
  4. num_rows = int(myArr[0])
  5. num_cols = int(myArr[1])
  6. seats = []
  7. for i in range(num_rows):
  8. row = []
  9. for j in range(num_cols):
  10. row.append(alphabets[j])
  11. seats.append(row)
  12. output = ""
  13. for i in range(len(seats)):
  14. for j in range(len(seats[i])):
  15. output += str(i + 1) + seats[i][j] + " "
  16. print(output)

Step-by-step explanation:

Firstly, we define a short alphabets list from A - J (Line 1).

Next, we prompt user to input row and columns (Line 3). Since the input is a string (e.g. "2 3"), we need to use split() method to separate the individual row and column number as individual element in a list (Line 4). We assign the first element (row number) to variable num_rows and second element (column number) to variable num_cols.

Next, we generate the seats list by using a two-layers for-loop (Line 10 -15). After the seat list is ready, we use another two-layers for-loop to generate output string as required by the question (Line 19-21).

At last, we print the output (Line 23). A sample input of 2 3 will generate the output as follows:

1A 1B 1C 2A 2B 2C

User Mato
by
4.6k points