Help with list homework - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Help with list homework (/thread-38731.html) |
Help with list homework - eyal123 - Nov-17-2022 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? RE: Help with list homework - Yoriz - Nov-17-2022 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] ]))
RE: Help with list homework - deanhystad - Nov-17-2022 It is difficult for i to equal 1 (i == 1) and 0 (i + 1 == 1) at the same time. RE: Help with list homework - eyal123 - Nov-18-2022 (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 ok got it, thank you,What do you think is the best way to do it? RE: Help with list homework - Yoriz - Nov-18-2022 You could start by breaking the problem down. Quote:0 can be next to all numbersBased 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, 0print({0, 1} == {1, 0}) Then create a function valid_seating_pair(seat1, seat2) that returns True or False by checking if the pair are validThis will get you started. RE: Help with list homework - deanhystad - Nov-18-2022 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. |