麻省理工学院公开课:计算机科学及编程导论问题1

1) 判断:


1.1. Any program that can be written using only function definitions and calls, the basic arithmetic operators, assignment, and conditionals will run in constant time.

任何用函数的定义和调用、基础算术符号、赋值和条件编写而成的程序,会在常数时间内运行。

错误。

递归话可能会是线性、对数级别的,O(n)或者O(2**n)这种。

常数指的是O(1)这种。


1.2. Newton’s method will always converge on a correct root of a function.

牛顿法总会收敛于函数的一个准确的根。

错误。

f(x)=1-x**2

f(x)=x**3-2x+2

都不行。

(来自wiki: https://en.wikipedia.org/wiki/Newton%27s_method#Mitigation_of_non-convergence)


1.3. In Python, dictionaries are immutable.

Python中,字典不可变。

错误,字典是可以变的。


1.4. The value of ‘math.sqrt(2.0)*math.sqrt(2.0) == 2.0’ is True.

错误。

math.sqrt(2.0)得出是一个近似2的平方根的数字;所以一个约等于2的平方根的数字的平方根不会等于2.。


1.5. One should always avoid iteration when a recursive solution is possible.

当递归可行时,必须总要避免迭代。

错误。

这是不一定的,根据个人需求和算法来决定。


1.6. Typically, the use of functions in a program reduces the total number of lines of code.

通常使用在程序中使用函数能减少编码行数。

对的。

通常是这样的,减少了重复的编程。


1.7. In Python, retrieving the value associated with a dictionary key takes roughly constant time.

Python中,用key值搜索字典花费的大概是常数时间。

对的。

搜索字典用的是hash表...



2) 思考compare1和compare 2的执行,a、b是float


def compare1(a, b):
    if a < 0:
        a = -a
    if b < 0:
        b = -b
    res = (a == b)
    if res:
        print a, 'and', b, 'have the same absolute value.'
    else:
        print a, 'and', b, 'have different absolute values.'
    return res


"""------------------------------------------------------------------------------"""


def absolute_value(n):
    if n < 0:
        n = -n
    return n


def compare2(a, b):
    res = absolute_value(a) == absolute_value(b)
    if res:
        print a, 'and', b, 'have the same absolute value.'
    else:
        print a, 'and', b, 'have different absolute values.'
    return res


2.1) 对于所有可能是输入,compare1和compare2 返回的是同样的值吗?? 如果不是,给出不是的输入。

是的,返回的res是同样的值。


2.2)  对于所有可能是输入,compare1和compare2 打印的是同样的值吗?? 如果不是,给出不是的输入。

不一定,absolute_value(n)只是得出了a和b的绝对值,并没有像compare1那样把得出的绝对值再次赋值给a、b。

只要a、b其中有一个是负数,就不一样。



3) 思考函数f,x为正整数。


def f(x):
    xs = str(x)                           # 将x转化为字符串,并赋值给xs
    if len(xs) == 1:                      # 如果xs的长度等于1
        return int(xs)                    # 返回整数化后的xs
    n = int(xs[0]) + int(xs[1])           # 将字符串xs的第一个字符和第二个字符转化为整数后,它们的和赋值给n
    if len(xs) == 2:                      # 如果字符串xs的长度是2
        return n                          # 返回n
    else:                                 # 其他
        return n + f(xs[2:])              # 返回n加上从xs字符串第三个开始的f(x)函数

f(2112) 是什么值?

xs = "2112"

len(xs) = 4

n = 2 + 1 = 3

xs[2:] = "12"

len(xsxs[2:]) = 2

f(xs[2:] = 1 + 2 = 3

f(2112) = 3+3 =6



3.2. 写出函数f的说明。

当x有一位数时,返回x;

当x有两位数时,返回这个数字每位上的单独数字的和;

当x有三位数时候,先把第一位和第二位的数字相加,再加上第三位;

当x有四位数时候,先把第一位和第二位的数字相加,再加上第三位和第四位的和;

当x有五位数时候,先把第一位和第二位的数字相加,再加上第三位和第四位的和,再加上第五位。

这个函数就是把x的各位数字相加求和。



4) 写出一个函数first_N,只有一个正整数参数n。函数能打印出第n个完美平方不是偶数的数字。如果n是2,那么应该打印出1和9。


如果n = 2:

1*1=1 是

2*2=4 不是

3*3=9 是


如果n = 3:

1*1=1 是

2*2=4 不是

3*3=9 是

4*4=16不是

5*5=25是


def first_N(n):
    count = 0
    number = 1
    while count < n:
        perfect_sqr = number * number
        if perfect_sqr % 2 != 0:
            print perfect_sqr
            count += 1
        number += 1



5. 写一段伪代码,用穷举法来猜测变量的值。

for a in range(某个规定范围内):

if a 满足某个条件:

return a



6.) 写一个函数findSide(),满足询问用户矩形的面积和其中一个边长。返回另一边的边长,类型float。

def findSide():
    a = float(raw_input("Please enter the area of the rectangle: "))
    b = float(raw_input("Please enter the length of one side of the rectangle: "))
    c = a / b
    return c


7) 下列函数满足说明吗? 如果不,改成满足。


def f(L):
    """Returns a copy of the list L without modifying L.""" # 用不改变数组L的方式,返回数组L的一份复制
    result = []
    for e in L: result.append(e)
    return result

满足。



8) At McDonalds one can buy chicken nuggets in packages containing 6, 9 or 20 pieces. Write a Python function that accepts an integer, num, as an argument and decides whether or not it is possible to buy num nuggets at McDonalds.

麦当劳买鸡块,有6、9、20个鸡块的组合。写一个函数,整数参数num

def eq(num):
    for a in range(0, num / 6 + 1):
        for b in range(0, num / 9 + 1):
            for c in range(0, num / 20 + 1):
                if 6 * a + 9 * b + 20 * c == num:
                    return True
    return False


9) 为下列函数写说明,假设n是整数。

def f(n):
    s = str(n)                        # 将n转化为字符串,并赋值给s
    if len(s) <= 1: return s          # 如果s长度小于等于1,返回s
    return s[-1] + f(int(s[:-1]))     # 否则返回s的倒数第一个字符串,再加上从倒数第二个开始的f(n)

这个函数就是将整数n变成字符串,然后把字符串的颠倒一下,例如最后位变第一位,第二位变倒数第二位。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值