Home > Software design >  Flight Exercise using file
Flight Exercise using file

Time:04-10

i need a little help if you don't mind me with this assignment

so considering we have a file that has flight information as the following:

  • name of the airline
  • flight destination
  • number of passengers

while the airline name is in ascending order, text file example:

Airarabia Paris 185

Airarabia Barcelona 85

Gulfair Brussels 95

Gulfair Barcelona 165

Nokair Oslo 205

Westjet Edinburgh 185 
    
Westjet Amsterdam 85 
    
Westjet Madrid 195

Tasks :

Task 1: Print how many flights are going to "Barcelona"?

Task 2: Which flight have the most passengers in the text file?

Task 3: Find the first flight with passengers that has less than 100 passengers . if there's no print that there are no flight with less than 100 travellers.

Task 4: Print the airline in the list that has the most total number of passengers also print the total number of passengers by that specific airline as well eg. Westjet 465 .

example total output:

2
Nokair Oslo 205
Airarabia Barcelona 85
Westjet 465

So far, I could only make Task 1 and Task 2 Properly then i got stuck as i didn't know how to gather sum of specific airline.

Thats the code i made

from collections import Counter


BarcelonaFlights = 0
Max = 0
Min = 99

with open('input.txt', 'r') as searchfile:

    if searchfile.read(1):

        for line in searchfile:

            #Task 1 - Barcelona Flights
            if 'Barcelona' in line:
                BarcelonaFlights  = 1

            #Task 2 - The Highest Passengers
            Num = int(line.split()[-1])

            if(Num > Max):
                Max = Num
                MaxFlight = line

            #Task 3 -  Passengers Less Than a hunder - TBF
            if(Num < Min):
                Min = Num
                LowFlight = line
            elif(Num > Min or Num == Min):
                LowFlight = "There is no flight with passengers less than 100."

            #Task 4 - Total Airline

                

 

        print(BarcelonaFlights)
        print(MaxFlight)
        print(LowFlight)
        

    else:
        print("0")
        print("File is empty!")
        print("There is no flight with passengers less than 100.")
        print("File is empty!")

CodePudding user response:

The way to approach this is to gather all the information from the input file into something that you can apply various operations to after you've you've consumed all of the input.

Each line in the file is comprised of 3 pieces of information so have a class that represents that.

class Flight:
    def __init__(self, airline, destination, pax):
        self.airline = airline
        self.destination = destination
        self.pax = int(pax)
    def __repr__(self):
        return f'{self.airline} {self.destination} {self.pax}'

As you consume the input, build a list of class instances:

flights = list()

with open('input.txt') as flight_data:
    for line in map(str.strip, flight_data):
        try:
            flights.append(Flight(*line.split()))
        except TypeError: # in case the input file doesn't match the expected format
            pass

Now write discrete functions to support the required functionality:

def count_destinations(flights, destination):
    return sum(1 for flight in flights if flight.destination == destination)

def max_pax(flights):
    return max(flights, key=lambda e: e.pax)

def low_pax(flights):
    for flight in flights:
        if flight.pax < 100:
            return flight

def most_pax(flights):
    d = dict()
    max_pax, max_airline = 0, None
    for flight in flights:
        if flight.airline in d:
            d[flight.airline]  = flight.pax
        else:
            d[flight.airline] = flight.pax
        if d[flight.airline] > max_pax:
            max_pax, max_airline = d[flight.airline], flight.airline
    return f'{max_airline} {max_pax}'

Then call these functions and print their return values:

print(count_destinations(flights, 'Barcelona'))
print(max_pax(flights))
print(low_pax(flights))
print(most_pax(flights))

Output:

2
Nokair Oslo 205
Airarabia Barcelona 85
Westjet 465
  • Related