Lab Q1 Short Circuiting >>> True and 13 13 >>> False or 0 0 >>> not 10 False >>> print(3) or "" # 首先会打印3,再打印“” 3 "" Q2 High-order Function ...

1 min

Lab Q1 WWPD: Lists & Ranges python3 ok -q lists-wwpd -u --local >>> list(range(3, 6)) [3,4,5,6,7,8] >>> range(3, 6) range(3,6) >>> range(4)[-1] 3 Q4 WWPD: List Comprehensions >>> [[1] + s for s in [[4], [5, 6]]] [[1, 4], [1, 5, 6]] Q8 Making Onions HW Q1 Num Eights 计算 n 中 8 出现的次数。 def num_eights(n): """Returns the number of times 8 appears as a digit of n. >>> num_eights(3) 0 >>> num_eights(8) 1 >>> num_eights(88888888) 8 >>> num_eights(2638) 1 >>> num_eights(86380) 2 >>> num_eights(12345) 0 >>> num_eights(8782089) 3 """ if n == 0: return 0 return num_eights(n // 10) + (1 if n % 10 == 8 else 0) Q2 Digit Distance 递归计算 n 各位差的总和。...

4 min

Lab 检查所有得分: python3 ok --score --local Q1: WWPD: List-Mutation python3 ok -q list-mutation -u --local >>> s = [9, 7, 8] >>> a, b = s, s[:] # s[:] 是 s 的浅拷贝 >>> s = [3] >>> s.extend([4, 5]) >>> a [9, 7, 8] 注意在 s 重新赋值后, a 和 s 不指向同一位置了. 初始状态: s —-> [9,7,8] a —-> [9,7,8] (和 s 指向同一个对象) b —-> [9,7,8] (独立的副本) 执行 s = [3] 后: s —-> [3] a —-> [9,7,8] (保持不变) b —-> [9,7,8] (保持不变)...

6 min

0 min

VSCode 配置 python Lab00 python3 ok --help: 查看 ok 提示。 python3 ok -q <file> -u: 运行单个 tests 目录下的测试。 python3 ok -q <function>: 运行单个函数的测试。 python3 ok: 运行全部测试。 python3 ok --score:查看分数。 python3 ok --local:本地运行测试。 python3 -m doctest lab00.py: 运行 doctest。 python3 -i xxx.py: 进入 python 交互模式 python3 -m doctest xxx.py: 运行函数的 doctest,如下,会运行»>后的函数,并与 2024 对比。如果 pass 的话不会有任何输出。 def twenty_twenty_four(): """Come up with the most creative expression that evaluates to 2024 using only numbers and the +, *, and - operators....

1 min

Building Abstractions with Data 2.1 Introduction 2.1.1 Native Data Types Python includes three native numeric types: integers (int), real numbers (float), and complex numbers (complex). >>> type(2) <class 'int'> >>> type(1.5) <class 'float'> >>> type(1+1j) <class 'complex'> 2.2 Data Abstraction 2.3 Sequences 2.3.1 Lists >>> digits = [1, 8, 2, 7] >>> len(digits) # 返回list长度 4 >>> digits[3] 8 >>> digits[-1] # 最后一个元素 7 # 这样也可以取list中的值 >>> digits = [1, 2, 3] >>> x, y, z = digits # 注意这边必须和list中元素的数量对应上才能unpack >>> x 1 >>> y 2 >>> z 3 List 的加法和乘法:...

7 min

0 min

Chapter 4 Data Processing 4.2 Implicit Sequences Sequence 可以在使用时才分配内存, 比如下面的 range(), 只有在使用时才分配内存, 而不是在定义时分配内存. >>> r = range(10000, 1000000000) >>> r[45006230] 45016230 4.2.1 Iterators 迭代器. >>> primes = [2, 3, 5, 7] >>> type(primes) <class 'list'> >>> iterator = iter(primes) >>> type(iterator) <class 'list_iterator'> >>> next(iterator) 2 >>> next(iterator) 3 >>> next(iterator) 5 当 next()到序列的最后一个元素之后, 会抛出 StopIteration 异常. 可以通过 try 来 catch 这个异常. >>> next(iterator) 7 >>> next(iterator) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> try: next(iterator) except StopIteration: print('No more values') No more values 每次调用 next(), 迭代器都会维护一个内部状态, 这个状态会记录当前迭代器的位置....

3 min

disc3 Q3 hw03 count_dollars Q5.6 hw04 berry_finder 和 max_path_sum hw04 和 lab04 的解析

1 min

Inheritance II: Extends, Casting, Higher Order Functions 10.1 Implementation Inheritance: Extends extend 语法: public class RotatingSLList<Blorp> extends SLList<Blorp>{ public void rotateRight() { Blorp oldBack = removeLast(); addFirst(oldBack); } } Constructor Behavior extend 的子类需要在构造函数中调用父类的构造函数,通过super(). 如果省略了 java 会帮忙自动调用。 public VengefulSLList() { deletedItems = new SLList<Item>(); } public VengefulSLList() { super(); deletedItems = new SLList<Item>(); } 如果子类构造函数需要调用父类带参数的构造函数,那么这时候必须主动调用super(...), 否则 java 只会自动调用不带参数的super(). public VengefulSLList(Item x) { super(x); deletedItems = new SLList<Item>(); } public VengefulSLList(Item x) { deletedItems = new SLList<Item>(); } 10....

2 min