📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2622| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61A]Discussion01: Discussion 1: Control Structures and Higher Order Functions

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

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

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

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

第一个Discussion,关于Constroal Statements和Higher Order Function。在Discussion里,大家不需要,也最好不要用电脑run,用笔和纸,把自己想成电脑,看看程序究竟是怎么跑的。
http://gaotx.com/cs61a/disc/disc01.pdf


补充内容 (2015-5-8 10:08):
我的答案
http://localhost:4000/2015/05/08/cs61a-disc01/

补充内容 (2015-5-8 10:09):
答案地址更新
http://gaotx.com/blogs/2015/05/08/cs61a-disc01/

上一篇:[CS61A]Hog
下一篇:[CS61A]Homework02
🔗
go7going 2015-5-8 17:34:37 | 只看该作者
全局:
交作业

2. The following code has been loaded into the Python interpreter:
def skipped(f):
def g():
return f
return g
def composed(f, g):
def h(x):
return f(g(x))
return h
def added(f, g):
def h(x):
return f(x) + g(x)
return h
def square(x):
return x*x
def two(x):
return 2

这道题还是回去要再领悟一下

code:(经过pycharm运行之后确保正确,为了使下一个程序运行我打上了备注的“”)

"""
def is_prime(n):
    if n==1:
        return False
    k=2
    while k<n:
        if n%k==0:
            return False
        k+=1
    else:
            return True
print is_prime(1)
"""
""""
def choose(n, k):
    total,totalk=1,1
    j= n-k+1
    while n >=j:
        total=total*n
        n-=1
    while k>1:
        totalk=totalk*k
        k=k-1

    return total/totalk

print choose(20,6)
"""""
"""
def square(x):
    return x * x
def square_ints(n):
    i=1
    while i<=n:
        print(square(i))
        i+=1
print square_ints(3)
"""""
""""
def double(x):
    return 2 * x
def double_ints(n):
    i=1
    while i<=n:
        print(double(i))
        i+=1
print double_ints(3)

"""""
""""
def transform_ints(func, n):
    i=1
    while i<=n:
        print (func(i))
        i+=1
def square(x):
    return x*x

print transform_ints(square,3)
"""
"""
def and_add(f, n):
    def g(x):
        return f(x)+n
    return g
"""

""""
def keep_ints(cond, n):
    i=1
    while i<=n:
        if cond(i):
            print i
        i+=1
"""




回复

使用道具 举报

全局:
lz请问
def choose(n, k):
        """Returns the number of ways to choose K items from
        N items.
        >>> choose(5, 2)
        10

        >>> choose(20, 6)
        38760

        """
        "*** YOUR CODE HERE ***"

        numerator, denominator = 1, 1
        length = 0
        a = n
        t = k

        while length <= k:  
            numerator = numerator * a   
            denominator = denominator * t
            a = a - 1
            t = t - 1
            length = length + 1
        return numerator / denominator

这段代码我运行总是出现 numerator, denominator = 1, 1 有问题,IndentationError: unindent does not match any outer indentation level,可是我觉得缩进没有问题呀,不知道哪里出错了。

评分

参与人数 1大米 +10 收起 理由
zzwcsong + 10

查看全部评分

回复

使用道具 举报

全局:
本帖最后由 乐百氏开开心心 于 2015-5-8 22:20 编辑

问题比较笨,麻烦lz了~~感觉这个high order这个似懂非懂,其实不太明白~~估计视频还要重新看一遍~~
回复

使用道具 举报

🔗
geminiiiiiii 2015-5-9 19:06:49 | 只看该作者
全局:
#1.3.1
def is_prime(n):
        i = 2
        if n == 1:
                return False
        while i < n:
                if n % i == 0:
                        return False
                i += 1
        return True
#1.4.1
def choose(n, k):
        denominator, numerator = 1, 1
        i, j = 1, n
        while i <= k:
                denominator = denominator * i
                i += 1
        while j >= n - k +1:
                numerator = numerator * j
                j -= 1
        return numerator / denominator
#2.1
def square(x):
        return x * x

def square_ints(n):
        i = 1
        while i <= n:
                print(square(i))
                i += 1

def double(x):
        return 2 * x

def double_ints(n):
        i = 1
        while i <= n:
                print(double(i))
                i += 1
#2.2.1
def transform_ints(func, n):
        i = 1
        while i <= n:
                print(func(i))
                i += 1

#2.4.1
def square(x):
        return x * x
def and_add(f, n):
        def g(x):
                return f(x) + n
        return g

#2.5.1
def keep_ints(cond, n):
        i = 1
        while i <= n:
                if cond(i) == True:
                        print(i)
                i += 1

#2.5.2
4; 11; 2


评分

参与人数 1大米 +10 收起 理由
zzwcsong + 10

查看全部评分

回复

使用道具 举报

🔗
wuxiaomin98 2015-5-23 05:34:07 | 只看该作者
全局:
# This is for cs61a discussion 1

def is_prime(n):
        """ This is for question 1.3
        This function is to check whether a number n is a prime number

        >>> is_prime(1)
        True
        >>> is_prime(2)
        True
        >>> is_prime(10)
        False
        >>> is_prime(13)
        True
        """
        "*** MY CODE BELOW ***"
        i = 2
        if n <= 0:
                return False
        elif n == 1 or n == 2:
                return True
        else:
                while i < n:
                        if n % i == 0:
                                return False
                        i += 1
                return True

def choose(n, k):
        """ This is for question 1.4
        This function returns the number of ways to choose k items from N

        >>> choose(5, 2)
        10
        >>> choose(20, 6)
        38760
        """
        i, total = 1, n
        while i < k:
                total = total * (n - i) // (i + 1)
                i += 1
        return total

# question 2.1
def square(x):
        return x * x
def square_ints(n):
        """ Print out the square of every int from 1 to n
        >>> square_ints(3)
        1
        4
        9
        """
        i = 1
        while i <= n:
                print(square(i))
                i += 1
               
def double(x):
        return 2 * x
def double_ints(n):
        """ Print out the double of every int from 1 to n
        >>> double_ints(3)
        2
        4
        6
        """
        i = 1
        while i <= n:
                print(double(i))
                i += 1

# question 2.2
def transform_ints(func, n):
        """ Print out all int from 1 to n with func applied on them

        # >>> def square(x):
        #        return x * x
        >>> transform_ints(square, 3)
        1
        4
        9
        """
        i = 1
        while i <= n:
                print(func(i))
                i += 1

# question 2.4
def and_add(f, n):
        """ Return a new function. This new function takes an
    argument x and returns f(x) + n.

    >>> def square(x):
            return x * x
    >>> new_square = and_add(square, 3)
    >>> new_square(4)  # 4 * 4 + 3
    19
    """
    def g(x):
            return f(x) + n
    return g

# question 2.5
def keep_ints(cond, n):
    """Print out all integers 1..i..n where cond(i) is true
    >>> def is_even(x):
    ... # Even numbers have remainder 0 when divided by 2.
    ...     return x % 2 == 0
    >>> keep_ints(is_even, 5)
    2
    4
    """
    i = 1
    while i <= n:
            if cond(i):
                    print(i)
            i += 1       
回复

使用道具 举报

🔗
reasonapp 2015-6-1 00:17:56 | 只看该作者
全局:
#disc01 Q1.3
def is_prime(n):
        i = 2
        while(n % i != 0):
                i += 1
        if i == n:
                return True
        else:
                return False

#disc02 Q1.4
def choose(n, k):
    """Returns the number of ways to choose K items from
    N items.
    >>> choose(5, 2)
    10
    >>> choose(20, 6)
    38760
    """
    total = 1
    while k > 0:
        total = (n / k) * total
        n -= 1
        k -= 1
    return total
   
#disc02 Q2.1

def square(x):
    return x * x

def square_ints(n):
    """Print out the square of every integer from 1 to n.
    >>> square_ints(3)
    1
    4
    9
    """
    k = 1
    while(k <= n):
        print square(k)
        k += 1
   
def double(x):
    return 2 * x
   
def double_ints(n):
    """Print out the double of every integer from 1 to n.
    >>> double_ints(3)
    2
    4
    6
    """
    k = 1
    while (k <= n):
        print double(k)
        k += 1
        
#disc02 Q2.2
def transform_ints(func, n):
    """Print out all integers from 1 to n with func applied
    on them.
    >>> def square(x):
    ... return x * x
    >>> transform_ints(square, 3)
    1
    4
    9
    """
    k = 1
    while (k <= n):
        print func(k)
        k += 1
        
#disc02 Q2.4
def and_add(f, n):
    """Return a new function. This new function takes an
    argument x and returns f(x) + n.
    >>> def square(x):
    ... return x * x
    >>> new_square = and_add(square, 3)
    >>> new_square(4) # 4 * 4 + 3
    19
    """
    def adder1(x):        
        return f(x) + n
    return adder1
   
#Q2.5
def keep_ints(cond, n):
    """Print out all integers 1..i..n where cond(i) is true
    >>> def is_even(x):
    ... # Even numbers have remainder 0 when divided by 2.
    ... return x % 2 == 0
    >>> keep_ints(is_even, 5)
    2
    4
    """
    k = 1
    while(k <= n):
        if(cond(k)):
            print k
        k += 1

Q2.5 第二问有一点迷惑,
skipped(added(square, two))()(3)中第一个括号是干啥的呢??
回复

使用道具 举报

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

本版积分规则

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