联系方式

您当前位置:首页 >> Database作业Database作业

日期:2024-12-28 07:18

Faculty of Arts & Science

Fall 2024 Quiz 10 - V2

CSC 110 Y1F

Question 1. Classes    [6 marks] Part (a) [2 marks]

Complete the init method in the following Car class according to the provided description:

class Car:

"""A car that can be driven and refueled .

A car has a certain fuel level, measured as an integer,  where higher values

indicate more fuel .

Additionally, the car has a mileage count, which is the total number of miles

this car has driven .

Instance Attributes:

- model: the car ' s model name

- fuel: the car 's fuel level (starts at 10) .

- mileage: the car ' s mileage (starts at 0) .

Representation Invariants:

- 0 <= self .fuel <= 20

- 0 <= self .mileage """

model: str

fuel: int

mileage: int

def init (self, model: str) -> None:

"""Initialize a new car with the given model name, fuel level 10, and mileage 0 .

>>> c = Car( ' Honda ' )

>>> c .model

' Honda '

>>> c .fuel

10

>>> c .mileage

0

"""

# TODO: complete this method body

We want to add a few more methods to the Car class from the previous page.  Complete the methods below according to the provided descriptions:

Part (b) [4 marks]

def refuel(self) -> None:

"""Refuel this car, increasing its fuel level by 5 (to a maximum of 20) .

>>> c = Car( ' Toyota ' )

>>> c.refuel()

>>> c .fuel

15

"""

# TODO: complete this method body

def drive(self, distance: int) -> None:

"""

Drive this car, increasing its mileage by the given distance,

and decreasing its fuel by 1 for each mile driven .

If there is not enough fuel to cover the whole distance, only drive until fuel reaches 0

(adding the appropriate amount to mileage) .

>>> c = Car( ' Ford ' )

>>> c .fuel

10

>>> c.drive(45)

>>> c .fuel

0

>>> c .mileage

10

"""

# TODO: complete this method body

Question 2. Stacks   [4 marks]

Complete the following function that takes in a Stack object. You may only use the public Stack methods as provided on the reference sheet:  is_empty, push, and pop. Do not call any method besides these three. You may also create a new Stack if necessary.

def reverse_top_two(stack: Stack) -> None:

"""Reverse the top two elements on  .

Precondition: has at least two items .

>>> stack = Stack()

>>> stack.push(1)

>>> stack.push(2)

>>> reverse_top_two(stack)

>>> stack.pop()

1

>>> stack.pop() 2

>>> stack .is_empty()

True """

# TODO: Implement this function .

Question 3. Queues   [4 marks]

Complete the following function that takes in a Queue object.  You may only use the public Queue methods as provided on the reference sheet:  is_empty, enqueue, and dequeue. Do not call any method besides these three. You may also create a new Queue if necessary.

def remove_all_but_one(queue: Queue) -> None:

"""Remove all items from the given queue except the last one .

Preconditions:

- not queue .is_empty()

>>> queue = Queue()

>>> queue.enqueue(1)

>>> queue.enqueue(2)

>>> queue.enqueue(3)

>>> remove_all_but_one(queue)

>>> queue .is_empty()

False

>>> queue.dequeue() 3

>>> queue .is_empty()

True """



相关文章

版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:821613408 微信:horysk8 电子信箱:[email protected]
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:horysk8