多伦多大学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 列表,函数
更多可加微信讨论
微信号ITCSdaixie
"""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
def update_elevation(elevation_map: List[List[int]], start: List[int], stop: List[int], delta: int) -> None:
"""
Modify elevation map elevation_map so that the elevation of each cell between cells start and stop, inclusive, changes by amount delta.
Precondition:
elevation_map is a valid elevation map. start and stop are valid cells in elevation_map. start and stop are in the same row or column or both.
If start and stop are in the same row, start's column <= stop's column.
If start and stop are in the same column, start's row <= stop's row. elevation_map[i, j] + delta >= 1 for each cell [i, j] that will change.
>>> THREE_BY_THREE_COPY = [[1, 2, 1], ... [4, 6, 5], ... [7, 8, 9]]
>>> update_elevation(THREE_BY_THREE_COPY, [1, 0], [1, 1], -2)
>>> THREE_BY_THREE_COPY [[1, 2, 1], [2, 4, 5], [7, 8, 9]]
>>> FOUR_BY_FOUR_COPY = [[1, 2, 6, 5], ... [4, 5, 3, 2], ... [7, 9, 8, 1], ... [1, 2, 1, 4]]
>>> update_elevation(FOUR_BY_FOUR_COPY, [1, 2], [3, 2], 1)
>>> FOUR_BY_FOUR_COPY [[1, 2, 6, 5], [4, 5, 4, 2], [7, 9, 9, 1], [1, 2, 2, 4]]
"""
pass # remove this line when you implement this function
def get_average_elevation(elevation_map: List[List[int]]) -> float:
"""
Return the average elevation across all cells in the elevation map elevation_map.
Precondition:
elevation_map is a valid elevation map.
>>> get_average_elevation(UNIQUE_3X3) 5.0
>>> get_average_elevation(FOUR_BY_FOUR) 3.8125
"""
pass # remove this line when you implement this function
def find_peak(elevation_map: List[List[int]]) -> List[int]:
"""
Return the cell that is the highest point in the elevation map elevation_map.
Precondition:
elevation_map is a valid elevation map. Every elevation value in elevation_map is unique.
>>> find_peak(UNIQUE_3X3) [1, 0]
>>> find_peak(UNIQUE_4X4) [0, 3]
"""
pass # remove this line when you implement this function
def is_sink(elevation_map: List[List[int]], cell: List[int]) -> bool:
"""
Return True if and only if cell exists in the elevation map elevation_map and cell is a sink.
Precondition:
elevation_map is a valid elevation map. cell is a 2-element list.
>>> is_sink(THREE_BY_THREE, [0, 5])
False
>>> is_sink(THREE_BY_THREE, [0, 2])
True
>>> is_sink(THREE_BY_THREE, [1, 1])
False
>>> is_sink(FOUR_BY_FOUR, [2, 3])
True
>>> is_sink(FOUR_BY_FOUR, [3, 2])
True
>>> is_sink(FOUR_BY_FOUR, [1, 3])
False
"""
pass # remove this line when you implement this function
def find_local_sink(elevation_map: List[List[int]], cell: List[int]) -> List[int]:
"""
Return the local sink of cell cell in elevation map elevation_map.
Precondition:
elevation_map is a valid elevation map. elevation_map contains no duplicate elevation values. cell is a valid cell in elevation_map.
>>> find_local_sink(UNIQUE_3X3, [1, 1]) [0, 0]
>>> find_local_sink(UNIQUE_3X3, [2, 0]) [2, 0]
>>> find_local_sink(UNIQUE_4X4, [1, 3]) [0, 2]
>>> find_local_sink(UNIQUE_4X4, [2, 2]) [2, 1]
"""
pass # remove this line when you implement this function
def can_hike_to(elevation_map: List[List[int]], start: List[int], dest: List[int], supplies: int) -> bool:
"""
Return True if and only if a hiker can go from start to dest in elevation_map without running out of supplies.
Precondition:
elevation_map is a valid elevation map. start and dest are valid cells in elevation_map. dest is North-West of start. supplies >= 0
>>> map = [[1, 6, 5, 6], ... [2, 5, 6, 8], ... [7, 2, 8, 1], ... [4, 4, 7, 3]]
>>> can_hike_to(map, [3, 3], [2, 2], 10)
True
>>> can_hike_to(map, [3, 3], [2, 2], 8)
False
>>> can_hike_to(map, [3, 3], [3, 0], 7)
True
>>> can_hike_to(map, [3, 3], [3, 0], 6)
False
>>> can_hike_to(map, [3, 3], [0, 0], 18)
True
>>> can_hike_to(map, [3, 3], [0, 0], 17)
False
"""
pass # remove this line when you implement this function
def get_lower_resolution(elevation_map: List[List[int]]) -> List[List[int]]:
"""
Return a new elevation map, which is constructed from the values of elevation_map by decreasing the number of elevation points within it.
Precondition:
elevation_map is a valid elevation map.
>>> get_lower_resolution( ... [[1, 6, 5, 6], ... [2, 5, 6, 8], ... [7, 2, 8, 1], ... [4, 4, 7, 3]]) [[3, 6], [4, 4]]
>>> get_lower_resolution( ... [[7, 9, 1], ... [4, 2, 1], ... [3, 2, 3]]) [[5, 1], [2, 3]]
"""
pass # remove this line when you implement this function