cs61a 第二週課時筆記

control

條件聲明

python中使用條件語句總的形式如下,一個完整控制過程包括header,statement,多個statement組成suite。
2-1

if條件語句

2-2

布爾操作和值

Python支持3個布爾操作:and,or,not
優先級:not > and > or

>>> print(not '' or not 0 and False)
False

and操作中,所有值是True則結果是True,否則是False。or操作只要有一個正確就正確。但是對於每一個值的檢測時從左往右的。

>>> 1 / 0
ZeroDivisionError: division by zero
>>> print(True or 1 / 0)
True
>>> print(1 / 0 or True)
ZeroDivisionError: division by zero
>>> print(True and 1 / 0 or True)
ZeroDivisionError: division by zero
>>> print(False and 1 / 0)
False
>>> 1 / 0 and False
ZeroDivisionError: division by zero
>>> 0 or False or 2 or 1 / 0
ZeroDivisionError: division by zero

Python中表示布爾False的值:False, 0, ‘’, None
表示布爾值True的值:除了False之外都是True.

迭代-While循環

high order function

高階函數就是把函數作爲參數或者函數作爲返回值的函數。

設計一個函數

考慮3個要素

domain-所有的輸入
range-所有返回值
behavior-輸入輸出間的關係

設計原則

一個函數只做一件事;
一個函數能被類似功能的函數調用,具有一般性。
如:
2-3
可以直接設計3個獨立函數求解,也可以找出其中相同的地方,只在不同的地方做修改。
如第一個函數實現:

def summation(n):
    total, k = 0, 1
    while k <= n:
        total, k = total + k, k + 1
    return total

累加是3個函數相同的操作,可以改爲如下,這時候就用到了這個課程的精髓,函數作爲參數傳入。
2-4

返回局部定義的函數作爲函數返回值

def make_adder(n):
    """
    
    >>> add_three = make_adder(3)
    >>> add_three(4)
    7
    """
    def adder(k):
        return k + n
    return adder 

lambda expressions

2-5

environment

局部變量

主要理解局部定義的函數尋找參數的過程中,先在當前局部環境查找,如果不存在,則在全局環境查找。

2-6
例子中全局環境只定義了兩個函數,當調用函數g()時,會執行a+y,實際上通過調用g()時只傳入了參數a,當前局部環境沒有變量y,往全局環境查找,全局環境也沒有,所以報錯。

高階函數的環境分析

2-7

自調用

2-8

lab 1

Coding Practice:

Q3:

def repeated(f, n, x):
    """Returns the result of composing f n times on x.

    >>> def square(x):
    ...     return x * x
    ...
    >>> repeated(square, 2, 3)  # square(square(3)), or 3 ** 4
    81
    >>> repeated(square, 1, 4)  # square(4)
    16
    >>> repeated(square, 6, 2)  # big number
    18446744073709551616
    >>> def opposite(b):
    ...     return not b
    ...
    >>> repeated(opposite, 4, True)
    False
    >>> repeated(opposite, 5, True)
    False
    >>> repeated(opposite, 631, 1)
    False
    >>> repeated(opposite, 3, 0)
    True
    """
    return f(x)**n

def square(x):
    return x * x

def opposite(b):
    return not b    

print(repeated(square, 2, 3))
print(repeated(square, 1, 4))
print(repeated(square, 6, 2))
print(repeated(opposite, 4, True))
print(repeated(opposite, 5, True))
print(repeated(opposite, 631, 1))
print(repeated(opposite, 3, 0))

Q9:

def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 0)
    1
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    """
    if k==0:
        return 1
    elif k==1:
        return n
    else:
        total = n
        while k > 1:
            total, k = total*(n-1), k-1
        return total

作業

  1. 考察函數調用,python中的布爾邏輯,print和None, if條件語句
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():
    """
    >>> result = with_if_statement()
    6
    >>> print(result)
    None
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    """
    >>> result = with_if_function()
    5
    6
    >>> print(result)
    None
    """
    return if_function(c(), t(), f())

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

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

def f():
    "*** YOUR CODE HERE ***"
  1. 考察while循環,基本的運算操作
def hailstone(x):
    """Print the hailstone sequence starting at x and return its
    length.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"
    i = 1
    if x == 1:
        return 1

    while(x != 1):
        if x % 2 == 0: 
            x = x / 2
            i += 1
        else:
            x = 3*x + 1
            i += 1
    return i
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章