CS61A Fall 2021 Lab 6: Mutability and Iterators

Q1: WWPD: List-Mutation

>>> lst = [5, 6, 7, 8]
>>> lst.append(6)
Nothing

>>> lst
[5,6,7,8,6]

>>> lst.insert(0, 9)
>>> lst
[9,5,6,7,8,6]

>>> x = lst.pop(2)
>>> lst
[9,5,7,8,6]

>>> lst.remove(x)
>>> lst
[9,5,7,8]

>>> a, b = lst, lst[:]
>>> a is lst
True

>>> b == lst
True

>>> b is lst
True

>>> lst = [1, 2, 3]
>>> lst.extend([4,5])
>>> lst
[1, 2, 3, 4, 5]

>>> lst.extend([lst.append(9), lst.append(10)])
>>> lst
[1,2,3,4,5,9,10,None, None]

Q2: Insert Items

def insert_items(lst, entry, elem):
    """Inserts elem into lst after each occurence of entry and then returns lst.

    >>> test_lst = [1, 5, 8, 5, 2, 3]
    >>> new_lst = insert_items(test_lst, 5, 7)
    >>> new_lst
    [1, 5, 7, 8, 5, 7, 2, 3]
    >>> double_lst = [1, 2, 1, 2, 3, 3]
    >>> double_lst = insert_items(double_lst, 3, 4)
    >>> double_lst
    [1, 2, 1, 2, 3, 4, 3, 4]
    >>> large_lst = [1, 4, 8]
    >>> large_lst2 = insert_items(large_lst, 4, 4)
    >>> large_lst2
    [1, 4, 4, 8]
    >>> large_lst3 = insert_items(large_lst2, 4, 6)
    >>> large_lst3
    [1, 4, 6, 4, 6, 8]
    >>> large_lst3 is large_lst
    True
    >>> # Ban creating new lists
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'insert_items',
    ...       ['List', 'ListComp', 'Slice'])
    True
    """
    time = 0
    for _ in range(len(lst)):
        if lst[_+time] == entry:
            lst.insert(_+time+1, elem)
            time += 1
    return lst

Q3: WWPD: Iterators

>>> s = [1, 2, 3, 4]
>>> t = iter(s)
>>> next(s)
Error

>>> next(t)
1

>>> next(t)
2

>>> iter(s)
Iterator

>>> next(iter(s))
1

>>> next(iter(t))
3

>>> next(iter(s))
1

>>> next(iter(t))
4

>>> next(t)
StopIteration
>>> r = range(6)
>>> r_iter = iter(r)
>>> next(r_iter)
0

>>> [x + 1 for x in r]
[1,2,3,4,5,6]

>>> [x + 1 for x in r_iter]
[2,3,4,5,6]

>>> next(r_iter)
StopIteration

>>> list(range(-2, 4))   # Converts an iterable into a list
[-2,-1,0,1,2,3]
>>> map_iter = map(lambda x : x + 10, range(5))
>>> next(map_iter)
10

>>> next(map_iter)
11

>>> list(map_iter)
[12,13,14]

>>> for e in filter(lambda x : x % 2 == 0, range(1000, 1008)):
...     print(e)
(line 1)? 1000
(line 2)? 1002
(line 3)? 1004
(line 4)? 1006

>>> [x + y for x, y in zip([1, 2, 3], [4, 5, 6])]
[5,7,9]

>>> for e in zip([10, 9, 8], range(3)):
...   print(tuple(map(lambda x: x + 2, e)))
(line 1)? (12,2)
(line 2)? (11,3)
(line 3)? (10,4)

 Q4: Count Occurrences

def count_occurrences(t, n, x):
    """Return the number of times that x appears in the first n elements of iterator t.

    >>> s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
    >>> count_occurrences(s, 10, 9)
    3
    >>> s2 = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
    >>> count_occurrences(s2, 3, 10)
    2
    >>> s = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])
    >>> count_occurrences(s, 1, 3)
    1
    >>> count_occurrences(s, 4, 2)
    3
    >>> next(s)
    2
    >>> s2 = iter([4, 1, 6, 6, 7, 7, 8, 8, 2, 2, 2, 5])
    >>> count_occurrences(s2, 6, 6)
    2
    """
    assert n > 0, "n must be greater than 0, asshole!"
    count = 0
    for _ in t:
        if _ == x:
            count += 1
        n = n - 1
        if n == 0:
            return count

Q5: Repeated

def repeated(t, k):
    """Return the first value in iterator T that appears K times in a row.
    Iterate through the items such that if the same iterator is passed into
    the function twice, it continues in the second call at the point it left
    off in the first.

    >>> s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
    >>> repeated(s, 2)
    9
    >>> s2 = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
    >>> repeated(s2, 3)
    8
    >>> s = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])
    >>> repeated(s, 3)
    2
    >>> repeated(s, 3)
    5
    >>> s2 = iter([4, 1, 6, 6, 7, 7, 8, 8, 2, 2, 2, 5])
    >>> repeated(s2, 3)
    2
    """
    assert k > 1, "k>1, pls"
    last = None
    count = 0
    for _ in t:
        if last == None or _ !=  last:
            count = 1
            last = _
        else:
            count += 1
            if count == k:
                return _ 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值