本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。
目录
Ⅰ Representing Pairs Using Lists
Ⅲ Rational Data Abstraction Implemented as Functions
01 Data Abstraction 数据抽象
Compound objects combine objects together:
A date: a year, a month, and a day.
A geographic position: latitude and longitude.
An abstract data type lets us manipulate compound objects as units.
Isolate two parts of any program that uses data:
How data are represented (as parts).
How data are manipulated (as units).
Data abstraction: A methodology by which functions enforce an abstraction barrier between representation and use.
Ⅰ Rational Numbers

Exact representation of fractions is a pair of integers. However, as soon as division occurs, the exact representation may be lost !
Assume we can compose and decompose rational numbers:

Ⅱ Rational Number Arithmetic

#最上层
def mul_rational(x, y):
"""Multiply rational numbers x and y."""
return rational(numer(x) * numer(y),
denom(x) * denom(y))
def add_rational(x, y):
"""Add rational numbers x and y."""
return rationa

最低0.47元/天 解锁文章
831

被折叠的 条评论
为什么被折叠?



