CSC 110 Y1F Fall 2024 Quiz 5C/C++

Java Python Faculty of Arts & Science

Fall 2024 Quiz 5

CSC 110 Y1F

Question 1. Tabular Data [6 marks]

Here is a sample list similar to the data we saw in the lecture. Each column represents an ID, a civic centre name, the number of marriage licenses issued, and the month and year when they were issued (YYYY, MM, DD).

import datetime

marriage_data = [

[1657, ' ET ' , 80, datetime.date(2011, 1, 1)],

[1658, ' NY ' , 136, datetime.date(2011, 1, 1)],

[1659, ' SC ' , 159, datetime.date(2011, 1, 1)],

[1660, ' TO ' , 367, datetime.date(2011, 1, 1)],

[1662, ' NY ' , 150, datetime.date(2011, 2, 1)],

[1664, ' TO ' , 383, datetime.date(2011, 2, 1)]

]

a) What would the following expressions evaluate to? Write your answers in the space provided below each statement.

>>> len(marriage_data)

>>> marriage_data[5][2]

>>> len(marriage_data[-1])

>>> min([row[2] for row in marriage_data])

b) Fill in a plain English docstring description for the function below, based on the provided function body. The preconditions are provided for you. You do not need to provide any doctests or further preconditions.

def do_something(data: list[list], year: int) -> float:

"""

Preconditions:

- year is a positive integer

- data satisfies all of the properties described in the beginning of this question

"""

counts = [row[2] for row in data if row[3] .year == year]

num_months = len({row[3] .month for row in&n CSC 110 Y1F Fall 2024 Quiz 5C/C++ bsp;data if row[3] .year == year})

return sum(counts) / num_months

Question 2. Dataclasses [3 marks]

Recall this dataclass we discussed in lecture:

@dataclass

class MarriageData:

"""A record of the number of marriage licenses issued in a civic centre

in a given month .

Instance Attributes:

- id: a unique identifier for the record

- civic_centre: the name of the civic centre

- num_licenses: the number of licenses issued

- date_issued: the month and year these licenses were issued

"""

id: int

civic_centre: str

num_licenses: int

date_issued: datetime .date

Rewrite the function body from the do_something function from the previous question so that it now deals with a list of MarriageData instances. Fill this in below:

def do_something_v2(data: list[MarriageData], year: int) -> float:

"""

Docstring omitted . The code should do the same thing that it does in Question 1 . """

counts =

num_months =

return sum(counts) / num_months

Question 3. Debugging / Index-Based For Loops [3 marks]

The function below is an incorrect attempt to return True if a string s is a palindrome.  Your friend Bob believes the function is correct because he tried calling the function with some string arguments which the code did work correctly for. Answer the questions below.

def is_palindrome(s: str) -> bool:

"""Return whether s is a palindrome .

A palindrome is a string that reads the same backward as forward          

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值