美文网首页
Homework 1

Homework 1

作者: myang199088 | 来源:发表于2015-05-28 16:35 被阅读53次

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

相关文章

网友评论

      本文标题:Homework 1

      本文链接:https://www.haomeiwen.com/subject/gxirqttx.html