[CS61A]Lecture #9: Functions Examples

仅用作个人学习记录
Reference:https://inst.eecs.berkeley.edu/~cs61a

Exercise: Reversing Digits

def reverse_digits(n):
	"""Assuming N >= 0 is an integer.  Return the number whose
    base-10 representation is the reverse of that of N.
    
    >>> reverse_digits(1)
    1
    >>> reverse_digits(100)
    1
    >>> reverse_digits(1234)
    4321
    >>> reverse_digits(12321)
    12321
    >>> reverse_digits(2222222)
    2222222
    """
    assert type(n) is int and n >= 0
    if n < 10:
        return n
    return reverse_digits(n // 10) + (n % 10) * 10 ** (num_digits(n) - 1)

def num_digits(n):
    """return the number of decimal digits in the positive integer N

    >>> num_digits(100)
    3
    >>> num_digits(123456)
    6
    """
    assert type(n) is int and n >= 0
    count = 1
    while n >= 10:
        count += 1
        n //= 10
    return count

TDD测试驱动开发。在设计函数时,应该先写设计文档,包括先决条件、函数功能和测试用例。

黑盒测试,不了解函数的内部实现进行的测试,如设计文档中用于驱动开发的测试用例。

白盒测试,白盒即可视的,你可以清楚盒子内部的东西以及里面是如何运作的,因此白盒测试需要你对系统内部的结构和工作原理有一个清楚的了解,并且基于这个知识来设计你的用例。

在编写reverse_digits时,我们想要知道数字的位数,这意味着可能需要抽象出一个新的功能(函数)。恰好这个函数在之前的课程学习中已经实现了,可以直接复制过来。

Exercise: Interleaving Digits

def interleave_digits(a, b):
    """Assuming A and B are non-negative integers with the same
    number of base-10 digits, return the number whose base-10
    representation is the interleaving of A's and B's digits,
    starting with A.

    >>> interleave_digits(1, 2)
    12
    >>> interleave_digits(0, 1)
    1
    >>> interleave_digits(1, 0)
    10
    >>> interleave_digits(135,246)
    123456
    """
    if a < 10:
        return a * 10 + b
    return interleave_digits(a // 10, b // 10) * 100 + interleave_digits(a % 10, b % 10)

Environment Detective

在这里插入图片描述

def flip(flop):
	if flop == 3 :
		return None
	flip = lambda flip: 3
	return flip

def flop(flip):
	return flop

flip, flop = flop, flip

flip( flop(1)(2) )(3)

Decorators

想要一个函数trace1,它以一个单参数函数(比如说f)为参数,并返回一个单参数函数,该函数:

  • 打印它的参数,前面有一个“->”。
  • 打印应用于其参数的 f 值,前面带有“<-”,然后返回该值。
>>> def square(x):
... return x*2
...
>>> square(3) + square(4)
-> 3
<- 9
-> 4
<- 16
25

Python 有一个有趣的特性——装饰器,它以一种有用的方式利用高阶函数。

@ATTR
def aFunc(...):
	...

ATTR 是个表达式,上述代码块等价于:

def aFunc(...):
	...
aFunc = ATTR(aFunc)

实现trace1:

def trace1(f):
    """Return a function that takes a single argument, x, prints it,
    computes and prints F(x), and returns the computed value."""
    def traced(x):
        print("->", x)
        r = f(x)
        print("<-", r)
        return r
    return traced

@trace1
def square(x):
    return x * x

>>> x = square(4)
-> 4
<- 16
>>> x
16

考虑下面这种替代跟踪方式有什么问题:

def aFunc1(x):
...
aFunc = trace1(aFunc1)

以上方式并没有改变aFunc1。

def fib1(n):
	return 0 if n <= 0 else 1 if n == 1 else fib1(n-2) + fib1(n-1)
fib = trace1(fib1)

>>> fib(4)
-> 4
<- 3
3

诸如 fib(4) 之类的调用将仅跟踪外部调用,而不跟踪递归内部调用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值