Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Exam 2 (2021-01-09)."""
- def range_with_count(start: int, stop: int, count: int) -> list:
- """
- Return list from start to stop with number total values in the list.
- The step between the elements in the list should be the same for all the elements.
- In case there is only one element, use the start value.
- Check the examples.
- #1
- count >= 1
- range_with_count(1, 5, 1) -> [1.0]
- range_with_count(1, 5, 2) -> [1.0, 5.0]
- range_with_count(1, 5, 3) -> [1.0, 3.0, 5.0]
- range_with_count(1, 5, 4) -> [1.0, 2.333333333333333, 3.6666666666666665, 5.0]
- range_with_count(1, 5, 5) -> [1.0, 2.0, 3.0, 4.0, 5.0]
- range_with_count(5, 1, 5) -> [5.0, 4.0, 3.0, 2.0, 1.0]
- """
- pass
- def add_symbols(string: str, symbols: str) -> str:
- """
- Return given string with added symbols where needed.
- #2
- If letter in string exists in symbols, double it in result.
- If number in string exists in symbols, triple it in result.
- It does not change the result when any symbol appears more than once in symbols.
- add_symbols("ab12", "b12a") -> "aabb111222"
- add_symbols("xyz", "xxxxxx") -> "xxyz"
- add_symbols("aaaa", "b") -> "aaaa"
- add_symbols("aab", "a") -> "aaaab"
- """
- pass
- def h_index(articles: list) -> int:
- """
- Given a list where each value represents the times of citations of one article.
- Return an h index of the person.
- H-index is the largest number such that a number of publications
- have at least the same number of citations.
- Examples:
- [4, 2, 4] => 2
- [1, 2, 2] => 2
- [] => 0
- [1, 1, 1, 1] => 1
- [3, 5, 7] => 3
- [2, 5, 7] => 2
- [5, 4, 7, 3, 6] => 4
- :param articles:
- :return:
- """
- pass
- def fuel_calculator(fuel: int) -> int:
- """
- Find needed amount of fuel for a given mass.
- #4
- Amount of fuel needed = mass divided by three, rounded down, subtract two
- + fuel needed for the fuel itself
- + fuel needed for the fuel's fuel + etc.
- Negative fuel rounds to zero.
- Examples:
- fuel_calculator(10) -> 1 + 0 = 1
- fuel_calculator(151) -> 48 + 14 + 2 + 0 = 64
- """
- pass
- def valid_parentheses(sequence: str) -> bool:
- """
- Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
- #5
- An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Example 1:
- Input: sequence = "()"
- Output: true
- Example 2:
- Input: sequence = "()[]{}"
- Output: true
- Example 3:
- Input: sequence = "(]"
- Output: false
- Example 4:
- Input: sequence = "([)]"
- Output: false
- Example 5:
- Input: sequence = "{[]}"
- Output: true
- :return boolean whether sequence is valid or not
- """
- pass
- class Donut:
- """Donut class."""
- def __init__(self, filling: str, icing: str):
- """
- Donut class constructor.
- :param icing: donut icing
- :param filling: donut filling
- """
- self.filling = filling
- self.icing = icing
- class DonutFactory:
- """DonutFactory class."""
- def __init__(self):
- """DonutFactory class constructor."""
- pass
- def add_donuts(self, donuts: list):
- """
- Add list of fresh donuts to already existing ones.
- :param donuts: list of donuts to add
- :return:
- """
- pass
- def get_donuts(self) -> list:
- """
- Return list of all donuts present on the line at the moment.
- :return: list of all donuts
- """
- pass
- def pack_donuts_by_filling_and_icing(self) -> dict:
- """
- Method should return dict with donuts divided by filling and icing.
- Dict key must be represented as tuple of filling and icing and value as list of donuts with
- given filling and icing.
- {(filling, icing): [donut1, donut2]}
- After packing, the production line for donuts should be empty (everything is packed).
- :return: dict
- """
- pass
- def sort_donuts_by_icing_and_filling(self) -> list:
- """
- Method should return list of donuts sorted by icing in alphabetical order and then by filling in alphabetical order.
- :return: sorted list of donuts
- """
- pass
- def get_most_popular_donut(self) -> dict:
- """
- Method should return dict with icing and filling of the most popular donut.
- {icing: most_pop_donut_icing, filling: most_pop_donut_filling}
- If there are several icing-filling combinations with the same amount of donuts,
- use the one which icing is alphabetically lower (a comes before b).
- :return: dict with icing and filling of most pop donut
- """
- pass
- def get_least_popular_donut(self) -> dict:
- """
- Method should return dict with icing and filling of the least popular donut.
- {icing: least_pop_donut_icing, filling: least_pop_donut_filling}
- If there are several icing-filling combinations with the same amount of donuts,
- use the one which filling is alphabetically higher (b comes before a).
- :return: dict with icing and filling of least pop donut
- """
- pass
- def get_donuts_by_flavour(self, flavour: str) -> list:
- """
- Get list of donuts that have the same icing or filling as given in method parameter.
- :return: list of donuts with the given flavour.
- """
- pass
- TICKET_MARKUP = 1.2
- class Aircraft:
- """Aircraft that will be flying your passengers around."""
- def __init__(self, operating_cost_per_distance: float, seats: int):
- """Constructor."""
- self.operating_cost_per_distance = operating_cost_per_distance
- self.seats = seats
- class Passenger:
- """Passenger interested in flying with your airline."""
- def __init__(self, money):
- """Constructor."""
- self.money = money
- class Airport:
- """Airport where flights are passing by."""
- def __init__(self, lat: float, lon: float, airport_fee: float):
- """Initialize airport with its coordinates in latitude and longitude, and airport fee for airlines."""
- self.lat = lat
- self.lon = lon
- self.airport_fee = airport_fee
- def distance_to_airport(self, other_airport) -> float:
- """
- Calculate the distance between this airport and the other airport in the input, using the Pythagorean theorem.
- Formula: distance = sqrt((Airport_A.lat - Airport_B.lat) ** 2 + (Airport_A.lon - Airport_B.lon) ** 2)
- :param other_airport: airport we are flying to.
- :return: distance between this airport and other_airport
- """
- pass
- class Flight:
- """Flight through two or more airports."""
- def __init__(self, aircraft: Aircraft, airports: list, passengers: list):
- """
- Constructor.
- NB! If you change the aircraft later on and the new aircraft has less seats than you have passengers,
- you must remove and refund any extra passengers by the amount they originally paid for the flight.
- :param aircraft: aircraft
- :param airports: list of Airports
- :param passengers: list of Passengers
- """
- pass
- def change_aircraft(self, aircraft):
- """
- Change aircraft of the flight.
- If the aircraft has less seats then currently there are passengers,
- you must remove any extra passengers and refund the amount they originally paid for the flight.
- """
- pass
- def add_passenger(self, passenger: Passenger):
- """
- Add a passenger to this flight, but only if they have enough money for the ticket and there are available seats.
- Don't forget to charge them for the flight!
- :param passenger: potential passenger who is supposed to be added and money taken from
- """
- pass
- def remove_passenger(self, passenger: Passenger):
- """
- Remove a passenger from the flight and give them a full refund for what they originally paid for the ticket.
- :param passenger: the passenger to be removed
- """
- pass
- def get_total_distance(self) -> float:
- """
- Calculate the entire distance of the flight through each airport.
- :return: total distance of the flight
- """
- pass
- def get_operating_cost(self) -> float:
- """
- Calculate the total operating cost.
- Calculate the total operating cost for a flight for this flight, by multiplying the distance
- and the airplane's operating_cost_per_distance and finally adding the airport fee of each airport
- visited for this flight.
- The resulting float is rounded to two decimal places.
- :return: fuel cost for flying the provided distance
- """
- pass
- def get_ticket_cost(self) -> float:
- """
- Calculate the cost of a single ticket.
- Calculate the cost of a single ticket, for which the formula is:
- ticket_cost = (operating costs / (available seats / 2)) * TICKET_MARKUP
- Ticket price is rounded to two decimal places.
- :return: price per ticket
- """
- pass
- def get_flight_profit(self) -> float:
- """
- Calculate and return the total profit of the flight, which is all ticket sales - operating costs.
- Profit is rounded to two decimal places.
- :return: flight profit
- """
- pass
- if __name__ == '__main__':
- assert range_with_count(1, 5, 2) == [1.0, 5.0]
- assert range_with_count(5, 1, 5) == [5.0, 4.0, 3.0, 2.0, 1.0]
- assert add_symbols("aab", "a") == "aaaab"
- assert add_symbols("aab1", "a12") == "aaaab111"
- assert h_index([4, 2, 4]) == 2
- assert h_index([5, 4, 7, 3, 6]) == 4
- assert fuel_calculator(151) == 64
- assert valid_parentheses("()") is True
- assert valid_parentheses("[[") is False
- assert valid_parentheses("[(])") is False
- # donut examples
- donut_factory = DonutFactory()
- donut1 = Donut('chocolate', 'sugar')
- donut2 = Donut('caramel', 'chocolate')
- donut3 = Donut('cherry', 'marshmallow')
- donut4 = Donut('chocolate', 'sugar')
- donut5 = Donut('vanilla', 'cream')
- donut6 = Donut('vanilla', 'cream')
- donut7 = Donut('cherry', 'marshmallow')
- donut8 = Donut('chocolate', 'sugar')
- donuts = [donut1, donut2, donut3, donut4, donut5, donut6, donut7, donut8]
- donut_factory.add_donuts(donuts)
- print(donut_factory.get_donuts_by_flavour('marshmallow')) # [donut3, donut7]
- print(donut_factory.get_least_popular_donut()) # {icing: chocolate, filling: caramel}
- print(donut_factory.get_most_popular_donut()) # {icing: sugar, filling: chocolate}
- print(donut_factory.sort_donuts_by_icing_and_filling()) # [donut2, donut5, donut6, donut3, donut7, donut1,
- # donut4, donut8]
- print(donut_factory.pack_donuts_by_filling_and_icing()) # {(chocolate, sugar): [donut1, donut4, donut8],
- # (caramel, chocolate): [donut2],
- # (cherry, marshmallow): [donut3, donut7],
- # (vanilla, cream): [donut5, donut6]}
- # airport examples
- passenger_1 = Passenger(2000.0)
- passenger_2 = Passenger(350.0)
- airport_1 = Airport(3, 2, 25.0)
- airport_2 = Airport(9, 7, 20.0)
- print(f'distance from airport_1 to airport_2: {airport_1.distance_to_airport(airport_2)}') # 7.81...
- flight = Flight(Aircraft(200.0, 10), [airport_1, airport_2, airport_1], [passenger_1])
- print(f'total distance: {flight.get_total_distance()}') # 15.62...
- print(f'operating cost: {flight.get_operating_cost()}') # 3194.1
- print(f'ticket cost: {flight.get_ticket_cost()}') # 766.58
- print(f'flight profit: {flight.get_flight_profit()}') # -2427.52
- flight.add_passenger(passenger_2)
- print(f'passenger count: {len(flight.passengers)}') # 1
- passenger_3 = Passenger(800.0)
- flight.add_passenger(passenger_3)
- print(f'passenger count: {len(flight.passengers)}') # 2
- print(f'flight profit: {flight.get_flight_profit()}') # -1660.94
- flight.remove_passenger(passenger_3)
- print(f'passenger count: {len(flight.passengers)}') # 1
- print(f'flight profit: {flight.get_flight_profit()}') # -2427.52
- print(f'passenger money: {passenger_3.money}') # 800.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement