CS 61A Spring 2018 Week 1 学习笔记

一、Learning form Textbook

1. 纯函数与非纯函数

Pure functions : 函数有输入(their arguments) and 输出(the result of applying them),比如一些内置函数(built in function)

ex_1
>>> abs(-2)
2

可以描述成如下图
在这里插入图片描述
Pure functions have the property that applying them has no effects beyond returning a value.
纯函数的特征是不会产生返回一个值之外的效果。

Non-pure functions:除了返回一个值,非纯函数可能产生副作用(side effect)。常见的side effect 是生成除了返回值外的输出,比如print function

ex_2
>>> print(1,2,3)
1 2 3

尽管在这个例子看来 print()abs() 并无不同,但是他们以完全不同的方式work。print() 的返回值是 None,但是交互式的python interpreter 并不会自动地打印 None 这个值。
而在这个关于print()的case中,显示在屏幕上的output ‘1,2,3’ 是print()被调用的side effect。

ex_3
>>> print(2)
2
>>> print(print(2))
2
None

ex_4
>>> two=print(2)
2
>>> print(two)
None

可以用下图来描述Non-pure函数print()
在这里插入图片描述

二、Learning form Homework

1.除号’/'和‘//’的区别
在python中,一个反斜杠 ‘ / ’会输出完整运算结果,即会输出小数点后的,返回的是浮点类型结果。

>>> a=5
>>> b=2
>>> a/b
2.5

而双反斜杠“ // ” 会输出整数结果,即返回整型结果。

>>> a=5
>>> b=2
>>> a//b
2

2. 倒序和排序
(1)字符串颠倒

>>> 'draw'[::-1]
'ward'

(2)对一串数进行排序

>>> sorted([3,5,2,9])
[2, 3, 5, 9]            # 默认为升序

>>> sorted([3,5,2,9],reverse=False)
[2, 3, 5, 9]            #reverse=False时降序排列

也可以使用 list 的 list.sort() 方法进行排序

3.如何使用homework 中的 doctest 判断代码是否正确

# hw1 Q3
Write a function that takes an integer n that is greater than 1 and returns the largest integer that is smaller than n and evenly divides n.

def largest_factor(n):
    """Return the largest factor of n that is smaller than n.

    >>> largest_factor(15) # factors are 1, 3, 5
    5
    >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
    40
    >>> largest_factor(13) # factor is 1 since 13 is prime
    1
    """
    "*** YOUR CODE HERE ***"

我的解答

 return max(i for i in range(1, n + 1) if n % i == 0 and i < n)

以 >>> 开头的行称为doctests,它是一个文档测试模块,引入该模块能对我们的程序进行测试。添加以下程序段即可在run窗口中显示测试的相关信息。

if __name__ == "__main__":  #模块运行进行自我测试
      import doctest
      doctest.testmod(verbose=True)

语句 ‘verbose=True’ 强制将详细信息显示出来,故在run窗口中会显示如下信息

Trying:
    largest_factor(15) # factors are 1, 3, 5
Expecting:
    5
ok
Trying:
    largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
Expecting:
    40
ok
Trying:
    largest_factor(13) # factor is 1 since 13 is prime
Expecting:
    1
ok
1 items had no tests:
    __main__
1 items passed all tests:
   3 tests in __main__.largest_factor
3 tests in 2 items.
3 passed and 0 failed.
Test passed.

4. Hw1_Q4: If Function vs Statement 总结

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    "*** YOUR CODE HERE ***"

def t():
    "*** YOUR CODE HERE ***"

def f():
    "*** YOUR CODE HERE ***"

我的解答:

def c():
    return True

def t():
    return 1

def f():
    print('wfsad')
    return None

Note

这道题可以考虑到之前提到的Non-pure 函数 print()会产生side-effect。
并且在执行 return if_function(c(), t(), f())时,解释器会先计算c(),t(),f()。也就是说,即使 c()的return 结果为 True,也不会影响 function f() 的被调用。

参考答案: https://cs61a.org/hw/sol-hw01/
课程网址: http://inst.eecs.berkeley.edu/~cs61a/sp18/

参考文献及网站
[1] Python中关于doctest的使用
[2]CS 61A Spring 2019 HW01 学习笔记
[3]CS61A 教材

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值