查看: 2601| 回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Discussion02:Recursion

全局:
公开课
学校名称: UCBerkely
Unit号: 1
开课时间: 4
课程全名: cs61a
平台: 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 AveMaleficum 于 2015-5-7 01:34 编辑

同上一个lab一样是对recursion的学习,请大家尽量用纸笔完成discussion
http://gaotx.com/cs61a//disc/disc02.pdf


补充内容 (2015-5-9 13:05):
我的答案
http://gaotx.com/blogs/2015/05/10/cs61a-disc02/

上一篇:[CS61A]Lab03: Recrusion
下一篇:[CS61A]Midterm01
🔗
AveMaleficum 2015-5-7 01:35:07 | 只看该作者
全局:
这个discussion都可以加学分的。
怎么才能证明认真做了这个discussion?手机拍图片?
回复

使用道具 举报

🔗
 楼主| sky420 2015-5-7 01:49:11 | 只看该作者
全局:
嗯,也是个办法,不过有的画environment diagram,画一个一张纸就满了。
回复

使用道具 举报

🔗
FFFelix 2015-5-7 08:57:13 | 只看该作者
全局:
sky420 发表于 2015-5-7 01:49
嗯,也是个办法,不过有的画environment diagram,画一个一张纸就满了。

有的一张纸都画不下
回复

使用道具 举报

🔗
 楼主| sky420 2015-5-7 09:58:12 | 只看该作者
全局:
FFFelix 发表于 2015-5-7 08:57
有的一张纸都画不下

恩恩,加油吧。不过画一画确实有助于理解
回复

使用道具 举报

🔗
geminiiiiiii 2015-5-10 21:00:04 | 只看该作者
全局:
1.1
#1
def countdown(n):

        if n <= 0:
                return
        else:
                print(n)
                countdown(n - 1)
#2
def countup(n):
        if n == 1:
                print(1)
                return
        else:
                countdown(n - 1)
                print(n)
#3
def expt(base, power):
        if power == 0:
                return 1
        elif power < 0:
                return expt(base, power+1) / base
        else:
                return expt(base, power-1) * base
#4
def is_prime(n):
#5
def sum_primes_up_to(n):
        if n == 2:
                return 2
        elif is_prime(n):
                return sum_primes_up_to(n-1) + n
        else:
                return sum_primes_up_to(n-1)
2.1
#1
def count_stair_ways(n):
        if n == 1:
                return 1
        elif n ==2:
                return 2
        else:
                count_stair_ways(n-1) + count_stair_ways(n-2)

#2
def pascal(row, column):
        if column < 0 or row < 0:
                return 0
        elif column ==0:
                return 1
        else:
                return pascal(row-1, column)+ pascal(row-1, column-1)
#3
def has_sum(sum, n1, n2):
        if sum < min(ni,n2)
                return False
        elif sum == n1 or sum ==n2:
                return True
        else: has_sum(sum-n1, n1, n2) or has_sum(sum-n2, n1, n2)




回复

使用道具 举报

🔗
go7going 2015-5-13 00:56:30 | 只看该作者
全局:
这个discussion的代码并不难 难的是算法
对于木有学过算法的小白 基本上都是看答案做出来的=。=

""""
def countdown(n):
    if n>0:
        print(n)
        return countdown(n-1)

countdown(5)
"""""
""""
def expt(base, power):
    if power==0:
        return 1
    elif power<0:
        return expt(base,power+1)/base
    else:
        return expt(base, power-1)*base
print expt(3,2)

"""""
""""
def is_prime(n):
    def helper(k):
        if k >= n:
         return True
        if n % k == 0:
         return False
        return helper(k + 1)
    return helper(2)



def sum_primes_up_to(n):
    if n<=1:
        return 0
    elif is_prime(n):
        return n+sum_primes_up_to(n-1)
    else:
        return sum_primes_up_to(n-1)

print sum_primes_up_to(5)
"""""
""""
def count_stair_ways(n):
    if n == 1:
        return 1
    elif n == 2:
        return 2
    else:
        n==3
        return 3
    return count_stair_ways(n-1) + count_stair_ways(n-2)

def pascal(row, column):
    if column == 0:
        return 1

    elif row == 0:
        return 0
    else:
        return pascal(row - 1, column) + pascal(row - 1, column - 1)


def has_sum(sum, n1, n2):

    if sum == n1 or sum == n2:
        return True
    if sum < min(n1, n2):
        return False
    return has_sum(sum - n1, n1, n2) or has_sum(sum - n2, n1, n2)

another way:
这道题感觉很难想出来 尤其是木有学算法的情况下。
def has_sum(sum, n1, n2):
    def helper(cur_sum):
        if cur_sum == sum:
            return True
        if cur_sum > sum:
            return False
        return helper(cur_sum + n1) or helper(cur_sum + n2)
    return helper(0)
"""""
""""
def sum_range(lower, upper):
    def summed(print_min, print_max):
        if lower <= print_min and print_max <= upper:
            return True
        if upper < print_min:
            return False
        return summed(print_min + 50, print_max + 60) or summed(print_min + 130, print_max + 140)
    return summed(0, 0)
回复

使用道具 举报

🔗
jy_121 2015-5-18 11:27:49 | 只看该作者
全局:
# 1. Print out a countdown using recursion.

def countdown(n):
        if(n <= 0):
                return
        print(n)
        countdown(n-1)


#2.  Is there an easy way to change countdown to count up instead?

Move the print statement to after the recursive call.


#3.  Write a procedure expt(base, power), which implements the exponent function.

def expt(base, power):
        if(power == 0):
                return 1
        else:
                return base*expt(base,power - 1)


#4.  Fill in the is prime function, which returns True if n is a prime number and False otherwise.
def is_prime(n):
        def helper(k):
                if(k >= n):
                        return True
                if(n % k == 0):
                        return False
                return helper(k+1)
        return helper(2)


#5.  Write sum primes up to(n), which sums up every prime up to and including n. Assume you have an is prime() predicate.

def sum_primes_up_to(n):
        if(n == 1):
                return n
        elif is_prime(n)
                return n + sum_primes_up_to(n-1)
        else:
                return sum_primes_up_to(n-1)


#2.1  Exercises:
        def count_stair_ways(n):
                if(n == 1):
                        return 1
                elif(n == 2):
                        return 2
                else:
                        return count_stair_ways(n-1) + count_stair_ways(n-2)


#2.2  Pascal’s triangle
        def pascal(row, column):
                if(column == 0):
                        return 1
                elif(row == 0):
                        return 0
                else:
                        return pascal(row - 1, column) + pascal(row - 1, column - 1)

#3           The TAs want to print handouts for their students.

def has_sum(sum, n1, n2):
        if(n1 == sum or sum == n2):
                return True
        if(sum < min(n1,n2)):
                return False
        else:
                return has_sum(sum - n1,n1,n2) or has_sum(sum - n2,n1,n2)

# Try a helper function: it makes the next one easier to solve
def has_sum(sum, n1, n2):
        def helper(cur_sum):
                if cur_sum == sum:
                        return True
                if cur_sum > sum:
                        return False
                return helper(cur_sum + n1) or helper(cur_sum + n2)
        return helper(0)


def summed(print_min, print_max):
        if lower <= print_min and print_max <= upper:
                return True
        if upper < print_min:
                return False
        return summed(print_min + 50, print_max + 60) or
                summed(print_min + 130, print_max + 140)
return summed(0, 0)
回复

使用道具 举报

🔗
wuxiaomin98 2015-5-23 08:31:05 | 只看该作者
全局:
# This is for cs61a discussion 2

def countdown(n):
    """ 1.1.1
    >>> countdown(3)
    3
    2
    1
    """
    if n <= 0:
            return
    print(n)
    countdown(n - 1)

def expt(base, power):
        """ 1.1.3

        >>> expt(3, 2)
        9
        >>> expt(2, 3)
        8
        """
        if power == 0:
                return 1
        else:
                return base * expt(base, power - 1)

def is_prime(n):
        # 1.1.4
    def helper(k):
        if(k >= n):
            return True
        if(n % k == 0):
            return False
        return helper(k+1)
    return helper(2)

def sum_primes_up_to(n):
        # 1.1.5
        if n <= 1:
                return 0
    elif is_prime(n):
            return n + sum_primes_up_to(n - 1)
    else:
            return sum_primes_up_to(n - 1)

def count_stair_ways(n):
        # 2.1.1
        if n == 1:
                return 1
        elif n == 2:
                return 2
        else:
                return count_stair_ways(n - 1) + count_stair_ways(n - 2)

def pascal(row, column):
        # 2.1.2
        if column == 0:
                return 1
        elif row == 0:
                return 0
        else:
                return pascal(row - 1, column) + pascal(row -  1, column - 1)
   
def has_sum(sum, n1, n2):
        # 2.1.3
        if sum == n1 or sum == n2:
                return True
        if sum < min(n1, n2):
                return False
        return has_sum(sum - n1, n1, n2) or has_sum(sum - n2, n1, n2)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表