Homework 1

2015-05-28  本文已影响53人  myang199088

from operator import add, sub

def a_plus_abs_b(a, b):
"""Return a+abs(b), but without calling abs."""
if b < 0:
op = sub
else:
op = add
return op(a, b)

def two_of_three(a, b, c):
"""Return x2 + y2, where x and y are the two largest of a, b, c."""
return a2 + b2 + c2 - min(a, b, c)2

def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and false_result otherwise."""
if condition:
return true_result
else:
return false_result

def hailstone(n):
"""Print the hailstone sequence starting at n, returning its length."""
length = 0
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
length += 1
return length

上一篇 下一篇

猜你喜欢

热点阅读