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

[cs61a]hw04

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

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

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

x
hw04: http://gaotx.com/cs61a/hw/hw04/
my solution: http://gaotx.com/blogs/2015/05/14/cs61a-hw04/

上一篇:【新课召集】CS61B_Spring 2015版
下一篇:[CS61A]disc04
🔗
karte_polo 2015-5-25 18:28:48 | 只看该作者
全局:
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a,b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert upper_bound(y) !=0 and lower_bound(y)!=0
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    p1 = lower_bound(x) - lower_bound(y)
    p2 = lower_bound(x) - upper_bound(y)
    p3 = upper_bound(x) - lower_bound(y)
    p4 = upper_bound(x) - upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))
   

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"
r1= interval(-100,99)
r2= interval(-150,-148)
print ("Using Par1",par1(r1,r2))
print ("Using Par2",par2(r1,r2))

def multiple_references_explanation():
    return """The mulitple reference problem..."""

def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    p1 = lower_bound(x)
    p2 = upper_bound(x)
    p3 = -b/(2*a)
    def f(t):
        return a*t*t+b*t+c
    v1,v2,v3 = map(f,(p1,p2,p3))
    if p3<=p2 and p3>=p1:
        return interval(min(v1,v2,v3),max(v1,v2,v3))
    else:
        return interval(min(v1,v2),max(v1,v2))

def polynomial(x, c):
    """Return the interval that is the range of the polynomial defined by
    coefficients c, for domain interval x.

    >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
    '-3 to 0.125'
    >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
    '0 to 10'
    >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
    '18.0 to 23.0'
    """
    "*** YOUR CODE HERE ***"
    def f(t):
        return sum([x*pow(t,y) for x,y in zip(c,range(len(c)))])
    "def f1(t):return sum([x*y(pow(t,y-1) for x,y in zip("


回复

使用道具 举报

🔗
wuxiaomin98 2015-5-28 08:34:55 | 只看该作者
全局:
只做了一部分。。。
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a, b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert lower_bound(y) > 0 or upper_bound(y) < 0, 'Divided by zero'
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    lower = lower_bound(x) - upper_bound(y)
    upper = upper_bound(x) -  lower_bound(y)
    return interval(lower, upper)

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"
a = interval(1, 1)
b = interval(2, 1)
print(str_interval(par1(a, b)), '!=', str_interval(par1(a, b)))
回复

使用道具 举报

🔗
FFFelix 2015-5-28 09:03:52 | 只看该作者
全局:
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a,b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    negative_y = interval(-upper_bound(y), -lower_bound(y))
    return add_interval(x, negative_y)

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"

def multiple_references_explanation():
    return """The mulitple reference problem..."""

def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    sol = - b / (2 * a)
    f = lambda x: a * x * x + b * x + c
    o, p, q = map(f,(lower_bound(x), upper_bound(x), sol)
    if lower_bound(x) <=  sol and upper_bound(x) >= sol:
        return interval(min(o,p,q), max(o,p,q))
    else:
        return interval(min(o,p), max(o,p))


def polynomial(x, c):
    """Return the interval that is the range of the polynomial defined by
    coefficients c, for domain interval x.

    >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
    '-3 to 0.125'
    >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
    '0 to 10'
    >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
    '18.0 to 23.0'
    """
    "*** YOUR CODE HERE ***"


也是只做了必做的代码部分
回复

使用道具 举报

🔗
reasonapp 2015-6-20 23:07:01 | 只看该作者
全局:
Q5没搞懂是什么意思。。。其余的都做啦~代码如下:
#hw4
#Q1
def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a,b]   

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]
   
def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

#Q2        
def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert upper_bound(y) != 0
    assert lower_bound(y) != 0
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

#Q3
def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    lower = lower_bound(x) - upper_bound(y)
    upper = upper_bound(x) - lower_bound(y)
   
    return interval(lower, upper)
               
#Q4
def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)


def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
ex1 = interval(-300, 299)
ex2 = interval(-550,-548)
print('This is par1',par1(ex1, ex2))
print('This is par2',par2(ex1, ex2))

#Q6
def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    f = lambda x: a * x ** 2 + b * x + c
    extreme = -b / 2 * a
    lower = f(lower_bound(x))
    upper = f(upper_bound(x))
    mid = f(extreme)
    if extreme <= upper_bound(x) and extreme >= lower_bound(x):
        return interval(min(lower, upper, mid), max(lower, upper, mid))
    else:
        return interval(min(lower, upper), max(lower, upper))

#back up
def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

回复

使用道具 举报

🔗
rockleecsu 2016-2-1 12:15:02 | 只看该作者
全局:
最后一题数**不准备做了。。。反正是Challenge。。。
前面的都做了


代码如下:
  1. def interval(a, b):
  2.     """Construct an interval from a to b."""
  3.     "*** YOUR CODE HERE ***"
  4.     return [a, b]


  5. def lower_bound(x):
  6.     """Return the lower bound of interval x."""
  7.     "*** YOUR CODE HERE ***"
  8.     return x[0]


  9. def upper_bound(x):
  10.     """Return the upper bound of interval x."""
  11.     "*** YOUR CODE HERE ***"
  12.     return x[1]


  13. def str_interval(x):
  14.     """Return a string representation of interval x.

  15.     >>> str_interval(interval(-1, 2))
  16.     '-1 to 2'
  17.     """
  18.     return '{0} to {1}'.format(lower_bound(x), upper_bound(x))


  19. def add_interval(x, y):
  20.     """Return an interval that contains the sum of any value in interval x and
  21.     any value in interval y.

  22.     >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
  23.     '3 to 10'
  24.     """
  25.     lower = lower_bound(x) + lower_bound(y)
  26.     upper = upper_bound(x) + upper_bound(y)
  27.     return interval(lower, upper)


  28. def mul_interval(x, y):
  29.     """Return the interval that contains the product of any value in x and any
  30.     value in y.

  31.     >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
  32.     '-8 to 16'
  33.     """
  34.     p1 = lower_bound(x) * lower_bound(y)
  35.     p2 = lower_bound(x) * upper_bound(y)
  36.     p3 = upper_bound(x) * lower_bound(y)
  37.     p4 = upper_bound(x) * upper_bound(y)
  38.     return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))


  39. def div_interval(x, y):
  40.     """Return the interval that contains the quotient of any value in x divided
  41.     by any value in y.

  42.     Division is implemented as the multiplication of x by the reciprocal of y.

  43.     >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
  44.     '-0.25 to 0.5'
  45.     """
  46.     "*** YOUR CODE HERE ***"
  47.     assert (upper_bound(y) != 0 and lower_bound(y) != 0), "Zero can't be divided"
  48.     reciprocal_y = interval(1 / upper_bound(y), 1 / lower_bound(y))
  49.     return mul_interval(x, reciprocal_y)


  50. def sub_interval(x, y):
  51.     """Return the interval that contains the difference between any value in x
  52.     and any value in y.

  53.     >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
  54.     '-9 to -2'
  55.     """
  56.     "*** YOUR CODE HERE ***"
  57.     return interval(lower_bound(x) - upper_bound(y), upper_bound(x) - lower_bound(y))


  58. def par1(r1, r2):
  59.     return div_interval(mul_interval(r1, r2), add_interval(r1, r2))


  60. def par2(r1, r2):
  61.     one = interval(1, 1)
  62.     rep_r1 = div_interval(one, r1)
  63.     rep_r2 = div_interval(one, r2)
  64.     return div_interval(one, add_interval(rep_r1, rep_r2))


  65. # These two intervals give different results for parallel resistors:
  66. "*** YOUR CODE HERE ***"
  67. interval_1 = (1, 2)
  68. interval_2 = (4, 5)
  69. print("Using par1", par1(interval_1, interval_2))
  70. print("Using par2", par2(interval_1, interval_2))


  71. def multiple_references_explanation():
  72.     return """Because it calculate the inexact value, the more times it reference, the less precision it has"""


  73. def quadratic(x, a, b, c):
  74.     """Return the interval that is the range of the quadratic defined by
  75.     coefficients a, b, and c, for domain interval x.

  76.     >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
  77.     '-3 to 0.125'
  78.     >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
  79.     '0 to 10'
  80.     """
  81.     "*** YOUR CODE HERE ***"
  82.     extreme = -b / (2 * a)
  83.     f = lambda x: a * x * x + b * x + c
  84.     l = lower_bound(x)
  85.     r = upper_bound(x)
  86.     if extreme >= l and extreme <= r:
  87.         return interval(min(f(extreme), f(l), f(r)), max(f(extreme), f(l), f(r)))
  88.     else:
  89.         return interval(min(f(l), f(r)), max(f(l), f(r)))


  90. def polynomial(x, c):
  91.     """Return the interval that is the range of the polynomial defined by
  92.     coefficients c, for domain interval x.

  93.     >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
  94.     '-3 to 0.125'
  95.     >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
  96.     '0 to 10'
  97.     >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
  98.     '18.0 to 23.0'
  99.     """
  100.     "*** YOUR CODE HERE ***"
复制代码
回复

使用道具 举报

🔗
Liaeve 2016-2-21 17:37:52 | 只看该作者
全局:
最后一题各种想还是做不出……

def interval(a, b):
    """Construct an interval from a to b."""
    "*** YOUR CODE HERE ***"
    return [a, b]

def lower_bound(x):
    """Return the lower bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[0]

def upper_bound(x):
    """Return the upper bound of interval x."""
    "*** YOUR CODE HERE ***"
    return x[1]

def str_interval(x):
    """Return a string representation of interval x.

    >>> str_interval(interval(-1, 2))
    '-1 to 2'
    """
    return '{0} to {1}'.format(lower_bound(x), upper_bound(x))

def add_interval(x, y):
    """Return an interval that contains the sum of any value in interval x and
    any value in interval y.

    >>> str_interval(add_interval(interval(-1, 2), interval(4, 8)))
    '3 to 10'
    """
    lower = lower_bound(x) + lower_bound(y)
    upper = upper_bound(x) + upper_bound(y)
    return interval(lower, upper)

def mul_interval(x, y):
    """Return the interval that contains the product of any value in x and any
    value in y.

    >>> str_interval(mul_interval(interval(-1, 2), interval(4, 8)))
    '-8 to 16'
    """
    p1 = lower_bound(x) * lower_bound(y)
    p2 = lower_bound(x) * upper_bound(y)
    p3 = upper_bound(x) * lower_bound(y)
    p4 = upper_bound(x) * upper_bound(y)
    return interval(min(p1, p2, p3, p4), max(p1, p2, p3, p4))

def div_interval(x, y):
    """Return the interval that contains the quotient of any value in x divided
    by any value in y.

    Division is implemented as the multiplication of x by the reciprocal of y.

    >>> str_interval(div_interval(interval(-1, 2), interval(4, 8)))
    '-0.25 to 0.5'
    """
    "*** YOUR CODE HERE ***"
    assert upper_bound(y) != 0 and lower_bound(y) != 0
    reciprocal_y = interval(1/upper_bound(y), 1/lower_bound(y))
    return mul_interval(x, reciprocal_y)

def sub_interval(x, y):
    """Return the interval that contains the difference between any value in x
    and any value in y.

    >>> str_interval(sub_interval(interval(-1, 2), interval(4, 8)))
    '-9 to -2'
    """
    "*** YOUR CODE HERE ***"
    return [lower_bound(x) - upper_bound(y), upper_bound(x) - lower_bound(y)]

def par1(r1, r2):
    return div_interval(mul_interval(r1, r2), add_interval(r1, r2))

def par2(r1, r2):
    one = interval(1, 1)
    rep_r1 = div_interval(one, r1)
    rep_r2 = div_interval(one, r2)
    return div_interval(one, add_interval(rep_r1, rep_r2))

# These two intervals give different results for parallel resistors:
"*** YOUR CODE HERE ***"
r1 = interval(1133, 1134)
r2 = interval(3216, 3217)
print(par1(r1, r2))
print(par2(r1, r2))


def multiple_references_explanation():
    return """The mulitple reference problem..."""

def quadratic(x, a, b, c):
    """Return the interval that is the range of the quadratic defined by
    coefficients a, b, and c, for domain interval x.

    >>> str_interval(quadratic(interval(0, 2), -2, 3, -1))
    '-3 to 0.125'
    >>> str_interval(quadratic(interval(1, 3), 2, -3, 1))
    '0 to 10'
    """
    "*** YOUR CODE HERE ***"
    x_ext = - b / ( 2 * a )
    extreme = a * x_ext * x_ext + b * x_ext + c
    p = a * lower_bound(x) * lower_bound(x) + b * lower_bound(x) + c
    q = a * upper_bound(x) * upper_bound(x) + b * upper_bound(x) + c
    if lower_bound(x) < x_ext < upper_bound(x):
        return interval(min(extreme, p, q), max(extreme, p, q))
    else:
        return interval(min(p, q), max(p, q))

def polynomial(x, c):
    """Return the interval that is the range of the polynomial defined by
    coefficients c, for domain interval x.

    >>> str_interval(polynomial(interval(0, 2), [-1, 3, -2]))
    '-3 to 0.125'
    >>> str_interval(polynomial(interval(1, 3), [1, -3, 2]))
    '0 to 10'
    >>> str_interval(polynomial(interval(0.5, 2.25), [10, 24, -6, -8, 3]))
    '18.0 to 23.0'
    """
    "*** YOUR CODE HERE ***"

   
回复

使用道具 举报

🔗
davewww 2018-7-16 12:38:28 | 只看该作者
全局:
交作业了,求加学分啊


HW_SOURCE_FILE = 'hw04.py'

###############
#  Questions  #
###############
from math import sqrt

def intersection(st, ave):
    """Represent an intersection using the Cantor pairing function."""
    return (st+ave)*(st+ave+1)//2 + ave

def street(inter):
    return w(inter) - avenue(inter)

def avenue(inter):
    return inter - (w(inter) ** 2 + w(inter)) // 2

w = lambda z: int(((8*z+1)**0.5-1)/2)

def taxicab(a, b):
    """Return the taxicab distance between two intersections.

    >>> times_square = intersection(46, 7)
    >>> ess_a_bagel = intersection(51, 3)
    >>> taxicab(times_square, ess_a_bagel)
    9
    >>> taxicab(ess_a_bagel, times_square)
    9
    """
    "*** YOUR CODE HERE ***"
    return abs(street(a)-street(b))+abs(avenue(a)-avenue(b))

def squares(s):
    """Returns a new list containing square roots of the elements of the
    original list that are perfect squares.

    >>> seq = [8, 49, 8, 9, 2, 1, 100, 102]
    >>> squares(seq)
    [7, 3, 1, 10]
    >>> seq = [500, 30]
    >>> squares(seq)
    []
    """
    "*** YOUR CODE HERE ***"
    total = []
    for i in s:
        if sqrt(i) % 1 == 0.0:
            total = total+[int(sqrt(i))]
    return total


def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'g', ['While', 'For'])
    True
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    else:
        return g(n-1) + 2*g(n-2) + 3*g(n-3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'g_iter', ['Recursion'])
    True
    """
    "*** YOUR CODE HERE ***"
    total, i = 0, 4
    p1, p2, p3 = 3, 2, 1

    if n <= 3:
        return n
    else:
        while  i <= n:
            total = p1 + 2*p2 + 3*p3
            p1, p2, p3 = total, p1, p2
            i +=1
        return total



def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'pingpong', ['Assign', 'AugAssign'])
    True
    """
    "*** YOUR CODE HERE ***"
    def counter(i, value, up):
        if i == n:
            return value
        elif has_seven(i) or i%7 == 0:
            if up:
                return counter(i+1, value-1, False)
            else:
                return counter(i+1, value+1, True)
        else:
            if up:
                return counter(i+1, value+1, True)
            else:
                return counter(i+1, value-1, False)


    return counter(1, 1, True)

def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    if k % 10 == 7:
        return True
    elif k < 10:
        return False
    else:
        return has_seven(k // 10)


def find_largest_power(k):
    if k == 0:
        return -1
    else:
        return 1+find_largest_power(k//2)

def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    power = find_largest_power(amount)
    k = pow(2,power)  #the largest coin we can use

    def count_partition(amount, k):
        total = 0
        if amount == 0:
            return 1
        elif amount < 0:
            return 0
        elif k ==0:
            return 0
        else:
            return count_partition(amount-k, k) + count_partition(amount, k//2)

    return count_partition(amount, k)

###################
# Extra Questions #
###################

from operator import sub, mul

def make_anonymous_factorial():
    """Return the value of an expression that computes factorial.

    >>> make_anonymous_factorial()(5)
    120
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'make_anonymous_factorial', ['Assign', 'AugAssign', 'FunctionDef', 'Recursion'])
    True
    """
    return 'YOUR_EXPRESSION_HERE'

def is_sorted(n):
    if n<10:
        return True
    elif (n//10%10) < (n%10):
        return False
    else:
        return is_sorted(n//10)

def mario_numer(level):

    if level == 1:
        return 1
    elif level == 0:
        return 0
    else:
        return mario_numer(level%10)
回复

使用道具 举报

🔗
davewww 2018-7-17 08:55:50 | 只看该作者
全局:
交作业了,hw04:

HW_SOURCE_FILE = 'hw04.py'

###############
#  Questions  #
###############
from math import sqrt

def intersection(st, ave):
    """Represent an intersection using the Cantor pairing function."""
    return (st+ave)*(st+ave+1)//2 + ave

def street(inter):
    return w(inter) - avenue(inter)

def avenue(inter):
    return inter - (w(inter) ** 2 + w(inter)) // 2

w = lambda z: int(((8*z+1)**0.5-1)/2)

def taxicab(a, b):
    """Return the taxicab distance between two intersections.

    >>> times_square = intersection(46, 7)
    >>> ess_a_bagel = intersection(51, 3)
    >>> taxicab(times_square, ess_a_bagel)
    9
    >>> taxicab(ess_a_bagel, times_square)
    9
    """
    "*** YOUR CODE HERE ***"
    return abs(street(a)-street(b))+abs(avenue(a)-avenue(b))

def squares(s):
    """Returns a new list containing square roots of the elements of the
    original list that are perfect squares.

    >>> seq = [8, 49, 8, 9, 2, 1, 100, 102]
    >>> squares(seq)
    [7, 3, 1, 10]
    >>> seq = [500, 30]
    >>> squares(seq)
    []
    """
    "*** YOUR CODE HERE ***"
    total = []
    for i in s:
        if sqrt(i) % 1 == 0.0:
            total = total+[int(sqrt(i))]
    return total


def g(n):
    """Return the value of G(n), computed recursively.

    >>> g(1)
    1
    >>> g(2)
    2
    >>> g(3)
    3
    >>> g(4)
    10
    >>> g(5)
    22
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'g', ['While', 'For'])
    True
    """
    "*** YOUR CODE HERE ***"
    if n <= 3:
        return n
    else:
        return g(n-1) + 2*g(n-2) + 3*g(n-3)

def g_iter(n):
    """Return the value of G(n), computed iteratively.

    >>> g_iter(1)
    1
    >>> g_iter(2)
    2
    >>> g_iter(3)
    3
    >>> g_iter(4)
    10
    >>> g_iter(5)
    22
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'g_iter', ['Recursion'])
    True
    """
    "*** YOUR CODE HERE ***"
    total, i = 0, 4
    p1, p2, p3 = 3, 2, 1

    if n <= 3:
        return n
    else:
        while  i <= n:
            total = p1 + 2*p2 + 3*p3
            p1, p2, p3 = total, p1, p2
            i +=1
        return total



def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(7)
    7
    >>> pingpong(8)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    0
    >>> pingpong(30)
    6
    >>> pingpong(68)
    2
    >>> pingpong(69)
    1
    >>> pingpong(70)
    0
    >>> pingpong(71)
    1
    >>> pingpong(72)
    0
    >>> pingpong(100)
    2
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'pingpong', ['Assign', 'AugAssign'])
    True
    """
    "*** YOUR CODE HERE ***"
    def counter(i, value, up):
        if i == n:
            return value
        elif has_seven(i) or i%7 == 0:
            if up:
                return counter(i+1, value-1, False)
            else:
                return counter(i+1, value+1, True)
        else:
            if up:
                return counter(i+1, value+1, True)
            else:
                return counter(i+1, value-1, False)


    return counter(1, 1, True)

def has_seven(k):
    """Returns True if at least one of the digits of k is a 7, False otherwise.

    >>> has_seven(3)
    False
    >>> has_seven(7)
    True
    >>> has_seven(2734)
    True
    >>> has_seven(2634)
    False
    >>> has_seven(734)
    True
    >>> has_seven(7777)
    True
    """
    if k % 10 == 7:
        return True
    elif k < 10:
        return False
    else:
        return has_seven(k // 10)


def find_largest_power(k):
    if k == 0:
        return -1
    else:
        return 1+find_largest_power(k//2)

def count_change(amount):
    """Return the number of ways to make change for amount.

    >>> count_change(7)
    6
    >>> count_change(10)
    14
    >>> count_change(20)
    60
    >>> count_change(100)
    9828
    """
    "*** YOUR CODE HERE ***"
    power = find_largest_power(amount)
    k = pow(2,power)  #the largest coin we can use

    def count_partition(amount, k):
        total = 0
        if amount == 0:
            return 1
        elif amount < 0:
            return 0
        elif k ==0:
            return 0
        else:
            return count_partition(amount-k, k) + count_partition(amount, k//2)

    return count_partition(amount, k)

###################
# Extra Questions #
###################

from operator import sub, mul

def make_anonymous_factorial():
    """Return the value of an expression that computes factorial.

    >>> make_anonymous_factorial()(5)
    120
    >>> from construct_check import check
    >>> check(HW_SOURCE_FILE, 'make_anonymous_factorial', ['Assign', 'AugAssign', 'FunctionDef', 'Recursion'])
    True
    """
    return 'YOUR_EXPRESSION_HERE'

def is_sorted(n):
    if n<10:
        return True
    elif (n//10%10) < (n%10):
        return False
    else:
        return is_sorted(n//10)

def mario_numer(level):

    if level == 1:
        return 1
    elif level == 0:
        return 0
    else:
        return mario_numer(level%10)
回复

使用道具 举报

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

本版积分规则

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