"""Самостоятельное упражнение Редактора; без внешних библиотек и ввода.""" import math assert list(range(3)) == [0, 1, 2] assert [0] * 3 == [0, 0, 0] assert list('ab') == ['a', 'b'] assert 'один два'.split() == ['один', 'два'] line = '1.5 2 3' assert [float(x) for x in line.split()] == [1.5, 2.0, 3.0] try: float('три') except ValueError: pass else: raise AssertionError('ValueError expected') rows = [[0] * 2 for _ in range(3)] rows[0][0] = 1 assert rows == [[1, 0], [0, 0], [0, 0]] shared = [[0] * 2] * 3 shared[0][0] = 1 assert shared == [[1, 0], [1, 0], [1, 0]] a = [1]; a.extend([2, 3]); assert a == [1, 2, 3] b = [1]; b.append([2, 3]); assert b == [1, [2, 3]] a.insert(0, 0); del a[1]; assert a == [0, 2, 3] a.clear(); assert a == [] items = ['a', 'b', 'a'] assert [x for x in items if x != 'a'] == ['b'] assert 'a' in items and items.index('a') == 0 and items.count('a') == 2 assert [i for i, x in enumerate(items) if x == 'a'] == [0, 2] assert len(items) == 3 try: items.index('z') except ValueError: pass else: raise AssertionError('ValueError expected') nums = [3, 1, 2] assert sum(nums) == 6 and max(nums) == 3 and math.prod(nums) == 6 assert nums.sort() is None and nums == [1, 2, 3] assert sorted(['B', 'a'], key=str.casefold) == ['a', 'B'] assert list(reversed(nums)) == [3, 2, 1] assert nums.reverse() is None and nums == [3, 2, 1] nums[0], nums[1] = nums[1], nums[0] assert nums == [2, 3, 1] def rotate_left(items, k): if not items: return [] k %= len(items) return items[k:] + items[:k] assert rotate_left([1, 2, 3], 4) == [2, 3, 1] assert rotate_left([], 4) == [] rows = [[1, 2], [3, 4]] assert len({len(row) for row in rows}) == 1 assert [list(col) for col in zip(*rows)] == [[1, 3], [2, 4]] assert ', '.join(['a', 'b']) == 'a, b' assert ', '.join(str(x) for x in [1, 2]) == '1, 2' assert str([1, 2]) == '[1, 2]' assert list(dict.fromkeys(['b', 'a', 'b'])) == ['b', 'a'] original = [[1]]; shallow = original.copy(); shallow[0].append(2) assert original == [[1, 2]] and shallow is not original class Bag: def __init__(self): self.items = [] first, second = Bag(), Bag(); first.items.append('a'); assert second.items == [] def collect(value, items=None): if items is None: items = [] items.append(value) return items assert collect(1) == [1] and collect(2) == [2] print('lists_extended: all examples passed')