Issue
This was the task in Python
middle exam at my university(first course) and I could not figure it out how to complete it.
So, there was a dictionary of wagons, seat names and availability of them(P.S this dictionary was much bigger, this is just short example).
d = {
'one': [{"Seat_name" : "A1", "isTaken" : False},
{ "Seat_name" : "A2", "isTaken" : True}],
'two': [{ "Seat_name" : "B1", "isTaken" : True},
{ "Seat_name" : "B2", "isTaken" : False}]
}
I had to offer the customer the wagon and the seat that was available and if the customer chose manually and the seat was taken or wagon was full, I had to offer the ones that was available. After the exam, I still could not find a way to solve it. So, any help, would be much appreciated.
Solution
basically this requires just a bit of dict
handling. what you could do is extract the 'available seats' part of the input dictionary. 'not avialable' is then given by exclusion. The rest is a bunch of if
and print
statements; I will leave the details and "prettification" to you since detailled requirements are not provided.
I took the freedom to convert your example dict to valid Python syntax and modified it a bit so we can test the behavior of the evaluation. In principle, you'd also have to check for validity of the choice, e.g. does wagon/seat exist. I skipped that for now...
def check_availability(data, choice):
# get the available seats, let each wagon be a key here as well.
availability = {}
for k, v in data.items():
# k: wagon no., v: dicts specifying seat/availability in the wagon
# check if all seats are occupied in a wagon
if not all(i['isTaken'] for i in v):
# if seats are available, append them to availability dict:
availability[k] = [i['Seat_name'] for i in v if not i['isTaken']]
# short version, actually bad style since line too long...
# availability = {k: [i['Seat_name'] for i in v if not i['isTaken']] for k, v in data.items() if not all(i['isTaken'] for i in v)}
# now there are three options we can walk through:
if choice['wagon'] not in availability.keys():
print(f"wagon {choice['wagon']} is full. available are:\n{availability}")
elif choice['seat'] not in availability[choice['wagon']]:
print(f"seat {choice['seat']} is taken. available in wagon {choice['wagon']} are:\n{availability[choice['wagon']]}")
else:
print(f"seat {choice['seat']} in wagon {choice['wagon']} is available!")
# testing
d = {1: [{"Seat_name": "A1", "isTaken": True},
{"Seat_name": "A2", "isTaken": True}],
2: [{"Seat_name": "B1", "isTaken": True},
{"Seat_name": "B2", "isTaken": False},
{"Seat_name": "B3", "isTaken": False}]}
choice = {'wagon': 1, 'seat': "A1"}
check_availability(d, choice)
# wagon 1 is full. available are:
# {2: ['B2', 'B3']}
choice = {'wagon': 2, 'seat': "B1"}
check_availability(d, choice)
# seat B1 is taken. available in wagon 2 are:
# ['B2', 'B3']
choice = {'wagon': 2, 'seat': "B2"}
check_availability(d, choice)
# seat B2 in wagon 2 is available!
Answered By - FObersteiner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.