多伦多大学CSC108Assignment2课业解析
多伦多大学CSC108Assignment2课业解析
题意:
按照功能描述完成8个函数,对矩阵进行一定操作
解析:
3*3矩阵定义如下: THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7, 8, 9]] 第一个函数compare_elevations_within_row(elevation_map: List[List[int]], map_row: int,level: int) -> List[int],接收三个参数分别是矩阵、要比较的行、要比较的值,compare_elevations_within_row(THREE_BY_THREE, 1, 5)即找到上述矩阵第一行,比5小的数、等于5的数和大于5的数,并以列表形式返回,即[1, 1, 1];第二个函数把起点到终点元素之间(含起点和终点)所有元素的值加上delta,若结果大于1保留修改否则取消;第三个函数和第四个函数都需要遍历所有元素,前者计算平均值,后者返回最大值的行列坐标。
涉及知识点:
python 列表,函数
更多可+薇❤️讨论 : qing1119X
"""Assignment 2 functions."""
from typing import List
THREE_BY_THREE = [[1, 2, 1], [4, 6, 5], [7, 8, 9]]
FOUR_BY_FOUR = [[1, 2, 6, 5], [4, 5, 3, 2], [7, 9, 8, 1], [1, 2, 1, 4]]
UNIQUE_3X3 = [[1, 2, 3], [9, 8, 7], [4, 5, 6]]
UNIQUE_4X4 = [[10, 2, 3, 30], [9, 8, 7, 11], [4, 5, 6, 12], [13, 14, 15, 16]]
def compare_elevations_within_row(elevation_map: List[List[int]], map_row: int, level: int) -> List[int]:
"""
Return a new list containing the three counts:
the number of elevations from row number map_row of elevation map elevation_map that are less than, equal to, and greater than elevation level.
Precondition:
elevation_map is a valid elevation map. 0 <= map_row < len(elevation_map).
>>> compare_elevations_within_row(THREE_BY_THREE, 1, 5) [1, 1, 1]
>>> compare_elevations_within_row(FOUR_BY_FOUR, 1, 2) [0, 1, 3]
"""
pass # remove this line when you implement this function