'Python Nested loops: printed seats

Given rows and 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, including after the last. Ex: num_rows = 2 and num_cols = 3 prints: 1A 1B 1C 2A 2B 2C

num_rows = 2
num_cols = 3
c1 = 1
while c1 <= num_rows:
    c2 = 'A'
    while c2 <= 'C':
        print('%s%s' % (c1, c2), end=' ')
        c2 = chr(ord(c2) + 1)
    c1 += 1
print()

Output returns correctly when I test with 2 rows and 3 columns or with 5 rows and 3 columns. However, when I test with 0 columns, it returns "1A 1B 1C 2A 2B 2C 3A 3B 3C 4A 4B 4C 5A 5B 5C" when it should return nothing. I tried changing c2 = 'A' into c2 = num_cols, but didn't change anything.



Solution 1:[1]

"""    c2 = 'A'
while c2 <= 'C':"""

I think the problem is here. Counting the alphabet would need an assigned value like you have 'C' but if you wanted to iterate through the alphabet with more or less columns you would need to assign a number to the maximum instead of the character. (The uppercase alphabetical assignment should only work if columns are between range(1,27))Try the following:

num_rows = 2
num_cols = 3

rows = 1                       #create a number to start counting from for rows
while rows <= num_rows:        #start iterating through number of rows
    cols = 1                   #create a number to start counting from for columns
    alpha = 'A'                #starting point for alphabet
    while cols <= num_cols:                    #iterates through number of columns
        print('%s%s' % (rows, alpha), end=' ')
        cols +=1                               #number of columns needs to increase
        alpha = chr(ord(alpha) + 1)            #alphabet needs to increase
    rows += 1                                  #number of rows needs to increase
print()                                         

"""This is my very first attempt. Please let me know if my code can be cleaner and more readable"""

Solution 2:[2]

You aren't making use of num_cols but instead hard-coding column C. You should make the upper bound of the column based on num_cols.

Change:

while c2 <= 'C':

to:

while c2 < chr(ord('A') + num_cols):

Solution 3:[3]

Why do I think first of

import itertools
print(' '.join(''.join(x) for x in itertools.product('12', 'ABC')))

?

Solution 4:[4]

Using a for loop instead of a while loop here could help ease the solution and optimize ouputs

rows = 2
columns = 3
l = ['a','b','c']

for i in range(1, rows +1):
    for j in range(1, columns + 1):
        print('{}{} '.format(i, l[j-1]), end='')
# 1a 1b 1c 2a 2b 2c 

columns = 0
# No output

Solution 5:[5]

I don't know if it's the cleanest, but it makes the most sense to me:

num_rows = 2
num_cols = 3

letter1 = '1'
letter2 = 'A'

for row in range(num_rows): # sets maximum changes to letter1.
   letter2 = 'A' # makes sure letter2 starts over after the inner loop first finishes.
   for col in range(num_cols): # sets maximum changes to letter2.
      print(letter1 + letter2, end=' ') # Print before incrementing letter2.
      letter2 = chr(ord(letter2) + 1) # converts 'A' to its unicode value, increments it by one, then converts it back to a letter.
   letter1 = chr(ord(letter1) + 1) # only runs after the inner loop is done to increment letter1 for the next loop.

Solution 6:[6]

num_row = 2
num_col = 3


i = 1
if num_rows and num_cols > 0:
    while i <= num_rows:    
        seat = 'A'
        while seat <= 'C':
            print('%s%s' %(i,seat), end=" ")
            seat = chr(ord(seat)+1)
        i +=1
print()

"Output returns correctly when I test with 2 rows and 3 columns or with 5 rows and 3 columns. However, when I test with 0 columns, it returns "1A 1B 1C 2A 2B 2C 3A 3B 3C 4A 4B 4C 5A 5B 5C" when it should return nothing. I tried changing c2 = 'A' into c2 = num_cols, but didn't change anything." Add an if statement to make sure there are existing chairs. If there isn't a column or row than their isn't a chair

Solution 7:[7]

for i in range(num_rows):
    letter = "A"
    num = i+1 #initially 0, so making it 0 + 1 .. and so on
    
    for j in range(num_cols):
        
        print('{}{}'.format(num,letter), end= ' ')
        letter =chr(ord(letter)+1)  #getting the unicode of the letter and incrementing it and changing back to ASCII character

Solution 8:[8]

num_rows = int(input())
num_cols = int(input())

row = 1    # establish start point to count rows
for i in range(num_rows):  # iterate num_rows 
    seat = 'A'  # establish start point to count cols
    for j in range(num_cols): # iterate num_cols
        print('{}{}'.format(row, seat), end=' ')  # format outputs with space 
        seat = chr(ord(seat) + 1)   # convert character to integer and increment 
    row = (row +1)   # increment
print()

Solution 9:[9]

num_rows = int(input())
num_cols = int(input())


i = 1
for row in range(num_rows):
    j = 'A'
    for col in range(num_cols):
    print(f'{i}{j}', end=' ') # Print before incrementing letter2.
    j = chr(ord(j) + 1)
i = i + 1 # only runs after the inner loop is done to increment letter1 for the next loop.
print()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Crunchy Leaves
Solution 2 blhsing
Solution 3 Dan D.
Solution 4 vash_the_stampede
Solution 5
Solution 6 user16865459
Solution 7 ???? ????
Solution 8 Smallfry
Solution 9 egwong