Python Nested loops: printed seats
Question:
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.
Answers:
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):
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
""" 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"""
Why do I think first of
import itertools
print(' '.join(''.join(x) for x in itertools.product('12', 'ABC')))
?
s1 = 1
while s1 <= num_rows and num_cols != 0:
s2 = 'A'
while s2 <= 'C':
print('%s%s' % (s1,s2), end = ' ')
s2 = chr(ord(s2) + 1)
s1 += 1
print()
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.
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
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
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()
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()
This is what I got from taking a snippet from @vash_the_stampede and some code from @Martin Tovmassian over on a post about how to find letters in a range.
num_rows = int(input())
num_cols = int(input())
rows = num_rows
columns = num_cols
from string import ascii_letters
def range_alpha(start_letter, end_letter):
return ascii_letters[
ascii_letters.index(start_letter):ascii_letters.index(end_letter) + 1
]
l = (range_alpha('A', 'Z'))
for i in range(1, rows +1):
for j in range(1, columns + 1):
print('{}{} '.format(i, l[j-1]), end='')
columns = 0
print()
I hope this helps someone trying to figure it out. I know this could be cleaner but I’m still in the middle of learning the basics of programming in Python.
Can be done using for loops, this way in my opinion is easier
num_rows = int(input())
num_cols = int(input())
letter = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
l = letter.split(" ")
for i in range(num_rows):
for j in range(num_cols):
print(f'{i+1}{l[j]}', end = " ")
print()
num_rows = int(input())
num_cols = int(input())
rows = 1
while rows <= num_rows:
seat_letter = 'A'
for j in range(num_cols):
print("{}{}".format(rows, seat_letter),end=' ')
seat_letter = chr(ord(seat_letter) + 1)
rows += 1
print()
num_rows = int(input())
num_cols = int(input())
row = 1
cols = '?'
while row <= num_rows:
cols = 'A'
while cols < chr(ord('A') + num_cols):
print(f'{row}{cols}', end = ' ')
cols = chr(ord(cols) + 1)
row = row + 1
This has worked for me.
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.
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):
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
""" 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"""
Why do I think first of
import itertools
print(' '.join(''.join(x) for x in itertools.product('12', 'ABC')))
?
s1 = 1
while s1 <= num_rows and num_cols != 0:
s2 = 'A'
while s2 <= 'C':
print('%s%s' % (s1,s2), end = ' ')
s2 = chr(ord(s2) + 1)
s1 += 1
print()
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.
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
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
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()
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()
This is what I got from taking a snippet from @vash_the_stampede and some code from @Martin Tovmassian over on a post about how to find letters in a range.
num_rows = int(input())
num_cols = int(input())
rows = num_rows
columns = num_cols
from string import ascii_letters
def range_alpha(start_letter, end_letter):
return ascii_letters[
ascii_letters.index(start_letter):ascii_letters.index(end_letter) + 1
]
l = (range_alpha('A', 'Z'))
for i in range(1, rows +1):
for j in range(1, columns + 1):
print('{}{} '.format(i, l[j-1]), end='')
columns = 0
print()
I hope this helps someone trying to figure it out. I know this could be cleaner but I’m still in the middle of learning the basics of programming in Python.
Can be done using for loops, this way in my opinion is easier
num_rows = int(input())
num_cols = int(input())
letter = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
l = letter.split(" ")
for i in range(num_rows):
for j in range(num_cols):
print(f'{i+1}{l[j]}', end = " ")
print()
num_rows = int(input())
num_cols = int(input())
rows = 1
while rows <= num_rows:
seat_letter = 'A'
for j in range(num_cols):
print("{}{}".format(rows, seat_letter),end=' ')
seat_letter = chr(ord(seat_letter) + 1)
rows += 1
print()
num_rows = int(input())
num_cols = int(input())
row = 1
cols = '?'
while row <= num_rows:
cols = 'A'
while cols < chr(ord('A') + num_cols):
print(f'{row}{cols}', end = ' ')
cols = chr(ord(cols) + 1)
row = row + 1
This has worked for me.