Python Forum
Help with list homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with list homework
#1
I have a list that compared from 3 lists that compered from 0,1,2
like that:
[
    [0, 0, 2, 2, 0, 1, 1],
    [1, 0, 0, 0, 1, 0, 0],
    [0, 1, 0, 0, 0, 1, 0]
]
0 can be next to all numbers
1 cant be next to another number
2 can be next to another 2

I tried to do it with the first line unsuccsefully
def verify_seating_arranngment(cinema):
    line_1 = cinema[0]
    line_2 = cinema[1]
    line_3 = cinema[2]
    for i in line_1:
        if i ==1 and i+1== 1:
            return False
    
print(verify_seating_arranngment([
    [0, 0, 2, 2, 0, 1, 1],
    [1, 0, 0, 0, 1, 0, 0],
    [0, 1, 0, 0, 0, 1, 0]
]))

someone can please tall me whats wrong with it?
Yoriz write Nov-17-2022, 08:45 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
I have added a print statement that shows what is happening in your if statement and that it is always False
def verify_seating_arranngment(cinema):
    line_1 = cinema[0]
    line_2 = cinema[1]
    line_3 = cinema[2]
    for i in line_1:
        print(f"{i} == 1 = {i ==1} and {i+1} == 1 = {i+1== 1} = {i ==1 and i+1== 1}")
        if i ==1 and i+1== 1:
            return False
     
print(verify_seating_arranngment([
    [0, 0, 2, 2, 0, 1, 1],
    [1, 0, 0, 0, 1, 0, 0],
    [0, 1, 0, 0, 0, 1, 0]
]))
Output:
0 == 1 = False and 1 == 1 = True = False 0 == 1 = False and 1 == 1 = True = False 2 == 1 = False and 3 == 1 = False = False 2 == 1 = False and 3 == 1 = False = False 0 == 1 = False and 1 == 1 = True = False 1 == 1 = True and 2 == 1 = False = False 1 == 1 = True and 2 == 1 = False = False None
Reply
#3
It is difficult for i to equal 1 (i == 1) and 0 (i + 1 == 1) at the same time.
Reply
#4
(Nov-17-2022, 08:59 PM)Yoriz Wrote: I have added a print statement that shows what is happening in your if statement and that it is always False
def verify_seating_arranngment(cinema):
    line_1 = cinema[0]
    line_2 = cinema[1]
    line_3 = cinema[2]
    for i in line_1:
        print(f"{i} == 1 = {i ==1} and {i+1} == 1 = {i+1== 1} = {i ==1 and i+1== 1}")
        if i ==1 and i+1== 1:
            return False
     
print(verify_seating_arranngment([
    [0, 0, 2, 2, 0, 1, 1],
    [1, 0, 0, 0, 1, 0, 0],
    [0, 1, 0, 0, 0, 1, 0]
]))
Output:
0 == 1 = False and 1 == 1 = True = False 0 == 1 = False and 1 == 1 = True = False 2 == 1 = False and 3 == 1 = False = False 2 == 1 = False and 3 == 1 = False = False 0 == 1 = False and 1 == 1 = True = False 1 == 1 = True and 2 == 1 = False = False 1 == 1 = True and 2 == 1 = False = False None

ok got it, thank you,What do you think is the best way to do it?
Reply
#5
You could start by breaking the problem down.

Quote:0 can be next to all numbers
1 cant be next to another number
2 can be next to another 2
Based on your rules figure out what are valid pairs of seating.
(Note I assume the rule 1 cant be next to another number is considering 0 to not be a number?)

If you have learned about sets you could use them to create valid pairs without needing both 0, 1 & 1, 0
print({0, 1} == {1, 0})
Output:
True
Then create a function valid_seating_pair(seat1, seat2) that returns True or False by checking if the pair are valid

This will get you started.
Reply
#6
The rules, as stated in the original post, are wrong. I think these are the rules
0 is an empty seat
You can only sit in an empty seat
1 cannot sit next to 1 or 2
2 cannot sit next to 1

I don't understand the problem. Are you trying to add occupants to an existing seating chart (find where 2 can sit in the partially filled cinema) or are you trying to create a valid seating chart given a list of attendees (find seats for [1,1,1,1,1,1,2,2])? If the latter, I think there are 9715 unique solutions for your guest list. If you are trying to seat 11 1's there is only one solution. Do you need to find all solutions or just one?
Can A sit in seat[x][y]?

To determine if A can sit in seat[x][y], gather the occupants of all neighboring seats.

neighbors = (seat[x-1][y], seat[x+1][y], seat[x][y-1], seat[x][y+1])

If the seat[x][y] is occupied (!= 0), it is not available
If any neighbor == 1, the seat is not available.
If A == 1 and any neighbor != 0 the seat is not available.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework - List containing tuples containing dicti Men 4 2,025 Dec-28-2021, 12:37 AM
Last Post: Men
  Sorting list - Homework assigment ranbarr 1 2,238 May-16-2021, 04:45 PM
Last Post: Yoriz
  have homework to add a list to a list using append. celtickodiak 2 2,022 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Dictionary/List Homework ImLearningPython 22 10,579 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020