Question: from __future__ import annotations import random from typing import TYPE_CHECKING, List, Any from course import sort_students if TYPE_CHECKING: from survey import Survey from course import
from __future__ import annotations import random from typing import TYPE_CHECKING, List, Any from course import sort_students if TYPE_CHECKING: from survey import Survey from course import Course, Student
def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of
The last slice may contain fewer than
=== Precondition === n <= len(lst)
>>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [False]] True """ ret = [] tmp = [] for i in range(len(lst)): if i % n == 0 and i != 0: tmp.append(lst[i]) if len(tmp) != 0: ret.append(tmp) tmp = [] return ret
def windows(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing windows of
=== Precondition === n <= len(lst)
>>> windows([3, 4, 6, 2, 3], 2) == [[3, 4], [4, 6], [6, 2], [2, 3]] True >>> windows(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [1, 6.0, False]] True """ ret = [] tmp = [] for i in range(n): tmp.append(lst[i]) ret.append(tmp) for i in range(len(lst)-n): tmp = tmp[1:] tmp.append(lst[i+n]) ret.append(tmp) return ret
I don't think the codes of these two methods are correct? Can you help me check these two methods and tell me the correct codes using python3.8? thankyou.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
