CS61a DISC 1 2021

CS61a DISC 1

Control, Environment Diagrams

我的个人博客 https://lipancake.tk/ (挂在github上,第一次打开很慢,但是排版好看)
Link to this section: https://inst.eecs.berkeley.edu/~cs61a/fa21/disc/disc01/
Run pythontutor link: https://pythontutor.com/composingprograms.html#mode=edit

Q1: Jacket Weather?

Alfonso will only wear a jacket outside if it is below 60 degrees or it is raining.

Write a function that takes in the current temperature and a boolean value telling if it is raining. This function should return True if Alfonso will wear a jacket and False otherwise.

First, try solving this problem using an if statement.

::: code-tabs#Q1
@tab Question

def wears_jacket_with_if(temp, raining):
    """
    >>> wears_jacket_with_if(90, False)
    False
    >>> wears_jacket_with_if(40, False)
    True
    >>> wears_jacket_with_if(100, True)
    True
    """
    "*** YOUR CODE HERE ***"

@tab Answer

#Q1
def wears_jacket_with_if(temp, raining):
    """
    >>> wears_jacket_with_if(90, False)
    False
    >>> wears_jacket_with_if(40, False)
    True
    >>> wears_jacket_with_if(100, True)
    True
    """
    "*** YOUR CODE HERE ***"
    if temp < 60 or raining:
        return True
    else:
        return False


@tab Answer 2

return True if temp < 60 or raining else False

:::

Q2: Case Conundrum

In this question, we will explore the difference between if and elif.

What is the result of evaluating the following code?

::: code-tabs#Q2
@tab Question

def special_case():
    x = 10
    if x > 0:
        x += 2
    elif x < 13:
        x += 3
    elif x % 2 == 1:
        x += 4
    return x

special_case()

def just_in_case():
    x = 10
    if x > 0:
        x += 2
    if x < 13:
        x += 3
    if x % 2 == 1:
        x += 4
    return x

just_in_case()

def case_in_point():
    x = 10
    if x > 0:
        return x + 2
    if x < 13:
        return x + 3
    if x % 2 == 1:
        return x + 4
    return x

case_in_point()

@tab Answer

# Q2
def special_case():
    x = 10
    if x > 0:
        x += 2
    elif x < 13:
        x += 3
    elif x % 2 == 1:
        x += 4
    return x

print(special_case()) # 12

def just_in_case():
    x = 10
    if x > 0:
        x += 2
    if x < 13:
        x += 3
    if x % 2 == 1:
        x += 4
    return x

print(just_in_case()) # 19

def case_in_point():
    x = 10
    if x > 0:
        return x + 2
    if x < 13:
        return x + 3
    if x % 2 == 1:
        return x + 4
    return x

print(case_in_point()) # 12

:::

Q3: If Function vs Statement

Now that we’ve learned about how if statements work, let’s see if we can write a function that behaves the same as an if statement.

::: info Hint
Hint: If you are having a hard time identifying how with_if_statement and with_if_function would differ in behavior, consider the rules of evaluation for if statements and call expressions.
:::
::: code-tabs#Q3
@tab Question

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, 'equal', 'not equal')
    'not equal'
    >>> if_function(3>2, 'bigger', 'smaller')
    'bigger'
    """
    if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
    """
    >>> result = with_if_statement()
    61A
    >>> print(result)
    None
    """
    if cond():
        return true_func()
    else:
        return false_func()

def with_if_function():
    """
    >>> result = with_if_function()
    Welcome to
    61A
    >>> print(result)
    None
    """
    return if_function(cond(), true_func(), false_func())

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

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

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

@tab Answer

# Q3
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, 'equal', 'not equal')
    'not equal'
    >>> if_function(3>2, 'bigger', 'smaller')
    'bigger'
    """
    if condition:
        return true_result
    else:
        return false_result

def with_if_statement():
    """
    >>> result = with_if_statement()
    61A
    >>> print(result)
    None
    """
    if cond():
        return true_func()
    else:
        return false_func()

def with_if_function():
    """
    >>> result = with_if_function()
    Welcome to
    61A
    >>> print(result)
    None
    """
    return if_function(cond(), true_func(), false_func())

def cond():
    "*** YOUR CODE HERE ***"
    return 0

def true_func():
    "*** YOUR CODE HERE ***"
    print('Welcome to')

def false_func():
    "*** YOUR CODE HERE ***"
    print('61A')

:::

Q4: Square So Slow

What is the result of evaluating the following code?
::: code-tabs#Q4
@tab Question

def square(x):
    print("here!")
    return x * x

def so_slow(num):
    x = num
    while x > 0:
        x=x+1
    return x / 0

square(so_slow(5))

@tab Answer

# Q4
def square(x):
    print("here!")
    return x * x

def so_slow(num):
    x = num
    while x > 0:
        x=x+1
    return x / 0

square(so_slow(5)) # 死循环

:::

Q5: Is Prime?

Write a function that returns True if a positive integer n is a prime number and False otherwise.

A prime number n is a number that is not divisible by any numbers other than 1 and n itself. For example, 13 is prime, since it is only divisible by 1 and 13, but 14 is not, since it is divisible by 1, 2, 7, and 14.

::: info Hint
Hint: Use the % operator: x % y returns the remainder of x when divided by y.
:::

::: code-tabs#Q5
@tab Question

def is_prime(n):
    """
    >>> is_prime(10)
    False
    >>> is_prime(7)
    True
    """
    "*** YOUR CODE HERE ***"

@tab Answer

def is_prime(n):
    """
    >>> is_prime(10)
    False
    >>> is_prime(7)
    True
    """
    "*** YOUR CODE HERE ***"
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

:::

Q6: Fizzbuzz

Implement the fizzbuzz sequence, which prints out a single statement for each number from 1 to n. For a number i,

If i is divisible by 3 only, then we print “fizz”.
If i is divisible by 5 only, then we print “buzz”.
If i is divisible by both 3 and 5, then we print “fizzbuzz”.
Otherwise, we print the number i by itself.
Implement fizzbuzz(n) here:

::: code-tabs#Q6
@tab Question

def fizzbuzz(n):
    """
    >>> result = fizzbuzz(16)
    1
    2
    fizz
    4
    buzz
    fizz
    7
    8
    fizz
    buzz
    11
    fizz
    13
    14
    fizzbuzz
    16
    >>> result == None
    True
    """
    "*** YOUR CODE HERE ***"

@tab Answer

def fizzbuzz(n):
    """
    >>> result = fizzbuzz(16)
    1
    2
    fizz
    4
    buzz
    fizz
    7
    8
    fizz
    buzz
    11
    fizz
    13
    14
    fizzbuzz
    16
    >>> result == None
    True
    """
    "*** YOUR CODE HERE ***"
    for i in range(1, n+1):
        if i % 3 == 0 and i % 5 == 0:
            print("fizzbuzz")
        elif i % 3 == 0:
            print("fizz")
        elif i % 5 == 0:
            print("buzz")
        else:
            print(i)

:::

Q7: Assignment Diagram

Alfonso will only wear a jacket outside if it is below 60 degrees or it is raining.

Write a function that takes in the current temperature and a boolean value telling if it is raining. This function should return True if Alfonso will wear a jacket and False otherwise.

First, try solving this problem using an if statement.

::: code-tabs#Q7
@tab Question

x = 11 % 4
y = x
x **= 2

:::
在这里插入图片描述

Q8: def Diagram

Use these rules for defining functions and the rules for assignment statements to draw a diagram for the code below.

::: code-tabs#Q8
@tab Question

def double(x):
    return x * 2

def triple(x):
    return x * 3

hat = double
double = triple

:::
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V6afjXHc-1659335405484)(../source/D1Q8.png)]

Q9: Call Diagram

Let’s put it all together! Draw an environment diagram for the following code. You may not have to use all of the blanks provided to you.

::: code-tabs#Q1
@tab Question

def double(x):
    return x * 2

hmmm = double
wow = double(3)
hmmm(wow)

:::
在这里插入图片描述

Q10: Nested Calls Diagrams

Draw the environment diagram that results from executing the code below. You may not need to use all of the frames and blanks provided to you.

::: code-tabs#Q10
@tab Question

def f(x):
    return x

def g(x, y):
    if x(y):
        return not y
    return y

x = 3
x = g(f, x)
f = g(f, 0)

:::

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CS61A 2020秋季学期的讨论课5主要涉及到关于递归的问题。这节讨论课中我们学习了递归函数的定义、调用和实例,并进行了一些练习。 首先,我们回顾了递归函数的定义。递归函数是指在一个函数的定义中调用了该函数本身的情况。这样的定义允许我们通过将问题分解为更小的子问题来解决复杂的问题。递归函数通常包括一个基本情况和一个递归情况。基本情况表示问题已经足够简单,可以直接计算出结果,而递归情况则表示将问题拆解为更小的子问题,并调用自身来解决这些子问题。 在练习中,我们通过编写递归函数来解决一系列问题。例如,我们实现了一个递归函数来计算一个列表的长度。首先,我们检查基本情况,即当列表为空时长度为0。然后,我们将问题拆解为子问题,即将列表分解为其第一个元素和其余部分。然后,我们递归地计算剩余部分的长度,并将其加上第一个元素,最终得到整个列表的长度。 在讨论课中,我们还学习了尾递归。尾递归是指递归函数中递归调用发生在函数的最后一步操作的情况。尾递归函数可以通过迭代的方式执行,而不会在每次递归调用时创建新的栈帧,从而减少了内存的使用。这对于处理大规模数据非常有用。 总而言之,本次讨论课中我们学习了递归函数的定义和使用。通过理解递归的原理和练习编写递归函数,我们能够更好地解决复杂的问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值