更相减损术
def __str__(self):
s = self.p
t = self.q
while s != t:
if s < t:
t = t - s
else:
s = s - t
return '%s/%s' %(self.p/s, self.q/t)
def __str__(self):
s = self.p
t = self.q
while s != t:
if s < t:
t = t - s
else:
s = s - t
return '%s/%s' %(self.p/s, self.q/t)
2016-02-21
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def f(x,y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-21
def cmp_ignore_case(s1, s2):
s1 = s1.upper()
s2 = s2.upper()
if s1 > s2:
return 1
if s1 < s2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
s1 = s1.upper()
s2 = s2.upper()
if s1 > s2:
return 1
if s1 < s2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-02-20
import math
def is_sqr(x):
r = math.sqrt(x)
return r%1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
r = math.sqrt(x)
return r%1==0
print filter(is_sqr, range(1, 101))
2016-02-20
def prod(x, y):
return x * y
print reduce(prod, [2, 4, 5, 7, 12])
return x * y
print reduce(prod, [2, 4, 5, 7, 12])
2016-02-20
def format_name(s):
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-02-20
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2016-02-20
def calc_prod(lst):
def c():
j=1
for i in lst:
j*=i
print j
return c
f = calc_prod([1, 2, 3, 4])
print f()
def c():
j=1
for i in lst:
j*=i
print j
return c
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-20
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-02-20
def calc_prod(lst):
def my_prod(a,b):
return a*b
def lazy_prod():
return reduce(my_prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def my_prod(a,b):
return a*b
def lazy_prod():
return reduce(my_prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-19
def is_sqr(x):
return math.floor(math.sqrt(x))==math.ceil(math.sqrt(x))
return math.floor(math.sqrt(x))==math.ceil(math.sqrt(x))
2016-02-18