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

Inheritance III: Subtype Polymorphism, Comparators, Comparable 11.1 A Review of Dynamic Method Selection Static Type vs. Dynamic Type static type: 变量声明时指定的类型。 dynamic type: 变量运行时的类型,根据 new 关键字。 11.2 ComparablesSubtype Polymorphism vs Explicit Higher Order Functions Explicit Higher Order Functions: def print_larger(x, y, compare, stringify): if compare(x, y): return stringify(x) return stringify(y) Subtype Polymorphism: def print_larger(x, y): if x.largerThan(y): return x.str() return y.str() 11.3 Comparables java 有一个 comparable interface: public interface Comparable<T> { /* Return negative number if o1 < o2....

2 min

12. Inheritance IV: Iterators, Object Methods 12.1 Lists and Sets in Java list java 提供了 built-in 的 List interface 和一些实现,比如 ArrayList. import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<Integer> L = new ArrayList<>(); L.add(5); L.add(10); System.out.println(L); } } set java 提供了 built-in 的 Set interface 和一些实现,比如 HashSet. import java.util.Set; import java.util.HashSet; Set<String> s = new HashSet<>(); s.add("Tokyo"); s.add("Lagos"); System.out.println(s.contains("Tokyo")); // true 12.2 Exceptions java 抛出异常语法:...

3 min

Inheritance I: Interface and Implementation Inheritance 9.2 Hypernyms, Hyponyms, and the Implements Keyword interface 类: public interface List61B<Item> { public void addFirst(Item x); public void add Last(Item y); public Item getFirst(); public Item getLast(); public Item removeLast(); public Item get(int i); public void insert(Item x, int position); public int size(); } 实现类: public class AList<Item> implements List61B<Item>{...} 9.3 Overriding, Interface Inheritance overriding 实现类实现 interface 类的时候要加上@Override关键字。 @Override public void addFirst(Item x) { insert(x, 0); } 9....

1 min