Task from python mid exam, list in dictionary, dictionary in list

Question:

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.

Asked By: dante

||

Answers:

Your input is not a valid dictionary. For me d should be like this(correct me if i am wrong)

d={1: [{'Seat_name': 'A1', 'isTaken': False},
        {'Seat_name': 'A2', 'isTaken': True}],
   2: [{'Seat_name': 'B2', 'isTaken': False}]
   }   

Create a dictionary that will store all seats_name available for each wagon

a=dict() 
for item in d: 
    a[item]=list() 
    for seat in d[item]: 
        if not seat.get('isTaken'): 
            a[item].append(seat['Seat_name'])

now if you want to check available wagon you can directly access from a dict and if value is empty in a then you can suggest some other wagon number

Answered By: Sudhir Tiwari

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

⦁ Suppose that there is a list which contains the id number, mid and final marks of four students. The program uses a dictionary, where the key is the ID of the student and value is a list containing the mid and final result of student, like this { “R/1212/09”: [ 35, 40] ,”R/1213/09”: [ 25, 45] ,…} .
Id Quiz (10% Mid exam (30%) Final exam (60%) Total
R/1212/09 8 35 40
R/1213/09 8 25 45
R/1214/09 9 34 54

   A.)  Calculate the total marks of each student and append them to your original lists inside the dictionary:   

Similar to {“R/1212/09”: [8, 35, 40, 75], ”R/1213/09”: [8,25, 45, 65],…}
B.) Write a python program that print top 2 students (id and total) based on total
C.) Print the dictionary in the following format
Grade distribution
Id quiz mid exam final exam total

⦁ Write a python program count all words in word.txt longer than 18 letters print these words and the number of such words.
⦁ Explain about default and named parameter with example.
⦁ Explain the main difference local and global variable with example
⦁ with Explain the main difference between list, tuple and dictionary example.
⦁ What is comment? Explain types of problem of comment with example.
⦁ What are computations? explain the problem solving steps with computer
⦁ What is cloning in graphics

Answered By: Abi Desta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.