当前位置: 首页 > news >正文

伯克利 CS61A 课堂笔记 09 —— Data Abstraction

本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。

目录

01 Data Abstraction 数据抽象

Ⅰ Rational Numbers

Ⅱ Rational Number Arithmetic

02 Pairs 对

Ⅰ Representing Pairs Using Lists

Ⅱ Reducing to Lowest Terms

03 Abstraction Barriers 抽象障碍

04 Data Representations 数据表示

Ⅰ What is Data?

Ⅱ Demo

Ⅲ 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 rational(numer(x) * denom(y) + numer(y) * denom(x),
                    denom(x) * denom(y))

def equal_rational(x, y):
    """Return whether rational numbers x and y are equal."""
    return numer(x) * denom(y) == numer(y) * denom(x)

rational(n, d) returns a rational number x.

numer(x) returns the numerator of x.

denom(x) returns the denominator of x.

These functions implement an abstract data type for rational numbers.

02 Pairs 对

Ⅰ Representing Pairs Using Lists
#最底层

#A list literal: Comma-seperated expressions in brackets
>>> pair = [1, 2]
>>> pair
[1, 2]

#"Unpacking" a list
>>> a, b = pair
>>> a
1
>>> b
2

#Element selection using the selection operator
>>> pair[0]
1
>>> pair[1]
2

#Element selection function
>>> from operator import getitem
>>> getitem(pair, 0)
1
>>> getitem(pair, 1)
2
#中间层

def rational(n, d):
    """Construct a rational number that represents N/D."""
    return [n, d] #Construct a list

def numer(x):
    """Return the numerator of rational number X."""
    return x[0] #Select item from a list

def denom(x):
    """Return the denominator of rational number X."""
    return x[1]
Ⅱ Reducing to Lowest Terms

from fractions import gcd #Greatest common divisor
def rational(n, d):
    """Construct a rational number that represents N/D."""
    g = gcd(n, d)
    return (n // g, d // g)

03 Abstraction Barriers 抽象障碍

Violating abstraction barriers:

04 Data Representations 数据表示

Ⅰ What is Data?

We need to guarantee that constructor and selector functions work together to specify the right behavior.

Behavior condition: If we construct rational number x from numerator n and denominator d, then numer(x)/denom(x) must equal to n/d.

Data abstraction uses selectors and constructors to define behavior.

If behavior conditions are met, then the representation is valid.

Ⅱ Demo
#Rational arithmetic

def mul_rational(x, y):
    return rational(numer(x) * numer(y),
                    denom(x) * denom(y))

def add_rational(x, y):
    return rational(numer(x) * denom(y) + numer(y) * denom(x),
                    denom(x) * denom(y))

def equal_rational(x, y):
    return numer(x) * denom(y) == numer(y) * denom(x)

def print_rational(x):
    print(numer(x), "/", denom(x))

# Constructor and selectors

def rational(n, d):
    return [n, d] 

def numer(x):
    return x[0] 

def denom(x):
    return x[1]
>>> x, y = rational(1, 2), rational(3, 8)
>>> print_rational(mul_rational(x, y))
3 / 16
# Constructor and selectors

def rational(n, d):
    def select(name):
        if name == 'n':
            return n
        elif name == 'd':
            return d
    return select

def numer(x):
    return x('n') 

def denom(x):
    return x('d')
>>> x, y = rational(1, 2), rational(3, 8)
>>> print_rational(mul_rational(x, y))
3 / 16
>>> x
<function rational.<locals>.select at 0x10293e6a8>
Ⅲ Rational Data Abstraction Implemented as Functions

附:词汇解释

latitude / ˈlætɪtuːd / 纬度、longitude / ˈlɑːndʒɪtuːd / 经度、represent 表示、manipulate / məˈnɪpjuleɪt / 操作、numerator / ˈnuːməreɪtər / 分子、denominator / dɪˈnɑːmɪneɪtər / 分母、fraction / ˈfrækʃ(ə)n / 分数、exact representation 精确表示、rational numbers 有理数、compose / kəmˈpoʊz / 构成、decompose / ˌdiːkəmˈpoʊz / 分解、arithmetic / əˈrɪθmətɪk / 算术,演算、constructor 构造器、selector 选择器、selection operator 选择操作符、bracket / ˈbrækɪt / 中括号、parentheses / pəˈrenθəsiːz / 小括号、lowest terms 最简形式、violate / ˈvaɪəleɪt / 违背、list literals 文字列表、element selection 元素选择、rational operation 有理运算、guarantee / ˌɡærənˈtiː / 确保、specify / ˈspesɪfaɪ / 明确指出,具体说明、met 符合、valid 有效的

相关文章:

  • 图书管理项目(spring boot + Vue)
  • Cisco Catalyst交换机和ASR路由器上加vty下的列表时最后的vrf-also命令作用
  • DedeBIZ系统审计小结
  • RabbitMQ的死信队列的产生与处理
  • PHP 超级全局变量
  • 手机用流量怎样设置代理ip?
  • ArcGIS基础知识之ArcMap基础设置——ArcMap选项:常规选项卡设置及作用
  • 蓝桥杯篇---温度传感器 DS18B20
  • visual studio导入cmake项目后打开无法删除和回车
  • 51-ArrayList
  • 【LeetCode Hot100 双指针】移动零、盛最多水的容器、三数之和、接雨水
  • 人工智能之深度学习的革命性突破
  • 【Stable Diffusion部署至GNU/Linux】安装流程
  • Dify 是什么?Dify是一个开源的LLM应用开发平台,支持快速搭建生成式AI应用,具有RAG管道、Agent功能、模型集成等特点
  • 计算机网络,大白话
  • 代码随想录算法【Day44】
  • 2.13学习记录
  • Docker Desktop Windows 之 安装 SqlServer
  • RabbitMQ 延迟队列
  • 全功能Python测试框架:pytest
  • 东莞公司建设网站制作/网站优化包括哪些
  • 自己制作logo免费生成器/seo网站建设优化什么意思
  • 深圳市做网站知名公司/今天最新新闻摘抄
  • 以家乡为主题做网站/安卓优化大师手机版下载
  • wordpress会计模板下载/青岛seo建站
  • 欧美设计网站推荐/网络推广外包业务怎么样