廖雪峰python教程答案

import math
#一元二次方程
def quadratic(a, b, c):
	n = b/a/2
	m = abs(n*n-c/a)
	x1 = math.sqrt(m)-n
	x2 = -math.sqrt(m)-n
	return x1,x2

#一个或多个数的乘积
def product(f, *args):
	result = f;
	for x in args:
		result *= x
	return result

#汉诺塔
def hanoi(n, a, b, c):
    if n == 1:
        print(a, '-->', c)
    else:
    	hanoi(n-1, a, c, b)
    	print(a, '-->', c)
    	hanoi(n-1, b, a, c)

#slice切片实现trim
def trim(s):
	while s[:1] == ' ':
		s = s[1:]
	while s[-1:] == ' ':
		s = s[:-1]
	return s;

#查找一个list中最小和最大值,并返回一个tuple
def findMinAndMax(L):
	if len(L) <= 0:
		return (None, None)
	else:
		min = L[0]
		max = L[0]
		for x in L[1:]:
			if x > max: 
				max = x
			elif x < min:
				min = x
		return (min, max)

#用生成器实现杨辉三角
def triangles():
	L = [1]
	while (True):
		yield L
		L = L.copy()
		L.append(0)
		L = [L[i-1]+L[i] for i in range(len(L))]

#把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字
def normalize(name):
	s = name[0].upper()
	for x in name[1:]:
		s += x.lower()
	return s

#对list求积:
from functools import reduce
def prod(L):
    return reduce(lambda x,y:x*y, L)

#str2float函数,把字符串'123.456'转换成浮点数123.456
def str2float(s):
	n = s.index('.')
	return reduce(lambda x,y:x*10+y,map(int,s[:n]+s[n+1:]))/pow(10,n)

#输出一定范围内的回数,回数是指从左向右读和从右向左读都是一样的数,例如12321,909
def is_palindrome(n):
	return str(n) == str(n)[::-1] #反转字符串

#利用闭包返回一个计数器函数
def createCounter():
	fs = [0]
	def counter():
		fs[0] = fs[0] + 1
		return fs[0]
	return counter

你可能感兴趣的:(Python)