目录
习题 01:定义一个函数,接收任意 3 个数字,并按顺序从大到小输出
习题 02:要求创建一个函数,它可以接收位置参数、不定长位置参数、不定长关键词参数,并按要求输出
习题 03:使用 reduce 函数实现找出一组数字列表中的最大值
习题 04:求 1000 以内能同时被 3 和 7 整除的数有哪些(要求使用 map 与 filter)
习题 06:要求使用 map 与 filter 函数,输出一个输入字符串里每个字符出现次数(提示:结合 dict 使用实现)
习题 08:创建一个能打印一个字典中包含所有字典对象的递归函数
习题 09:编写一个生成器,实现 Fibonacci数列的效果
习题 01:定义一个函数,接收任意 3 个数字,并按顺序从大到小输出
def sort01(*args):
list01 = list(args)
list01 = sorted(list01)
return tuple(list01)
a = sort01(5, 2, 7, 1, 6)
print(a)
习题 02:要求创建一个函数,它可以接收位置参数、不定长位置参数、不定长关键词参数,并按要求输出
def higoInfo(name, *args, company='HIGO', **kwargs):
print('姓名:', name)
if len(args) == 0:
print("其它信息:无")
else:
n = 1
for item in args:
print("其它信息", n, ":", item)
n += 1
print("公司:", company)
if len(kwargs) == 0:
print("工作概况:无")
else:
for h_key, h_value in kwargs.items():
print(">>>", h_key, ":", h_value)
higoInfo('飞', 19, '男', position='DBA', department='技术部')
习题 03:使用 reduce 函数实现找出一组数字列表中的最大值
def compare_max(x, y):
if x >= y:
return x
else:
return y
list01 = [1, 3, 7, 2, 5]
z = reduce(compare_max, list01)
print(z)
习题 04:求 1000 以内能同时被 3 和 7 整除的数有哪些(要求使用 map 与 filter)
def divisible(x):
if x % 3 == 0 and x % 7 == 0:
return x
# Python 2.x 返回列表。
# Python 3.x 返回迭代器。
iterator01 = map(divisible, [x for x in range(1000)])
it_list = []
while True:
try:
x = next(iterator01)
if x != None and x != 0:
it_list.append(x)
except StopIteration:
break
print(it_list)
print('#######################################')
# Python 2.x 返回列表。
# Python 3.x 返回一个迭代器对象,可以使用 list() 来转换
new_list = filter(divisible, [x for x in range(1000)])
print(list(new_list))
习题 05:请以 round 函数,定义一个偏函数 roundN,调用为输入一个数字 N,返回圆周率后 N 位的数字
提示:import math
math.pi
请以 sorted 函数,定义一个偏函数 sortedDESC,结果为输入一个序列,返回为安降序排列后的序列
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import math
from functools import partial
from collections.abc import Iterator
"""
习题 05:请以 round 函数,定义一个偏函数 roundN,调用为输入一个数字 N,返回圆周率后 N 位的数字。
提示:import math
math.pi
请以 sorted 函数,定义一个偏函数 sortedDESC,结果为输入一个序列,返回为安降序排列后的序列。
"""
def roundN(n):
return round(math.pi, n)
pi_n = partial(roundN)
print(pi_n(5))
def sortedDESC(a):
return sorted(a, reverse=True)
list_desc = partial(sortedDESC)
a = [5,7,6,3,4,1,2]
b = list_desc(a)
print(b)
习题 06:要求使用 map 与 filter 函数,输出一个输入字符串里每个字符出现次数(提示:结合 dict 使用实现)
def statisticsStr(str_info):
if str_info in str_dict:
str_dict[str_info] += 1
else:
str_dict.setdefault(str_info, 1)
return sorted(str_dict.items(), key=lambda x: x[1], reverse=True)
str_a = 'This is a calculated pi source can be used as the primary portal learning'
str_info = str_a.replace(' ', '')
str_dict = dict()
iterator_01 = map(statisticsStr, str_info)
while True:
try:
str_list01 = next(iterator_01)
except StopIteration:
break
print(str_list01)
习题 07:创建一个能够快速排序的递归函数
def quickSort(array):
if len(array) < 2:
# 当数组中只包含一个元素的时候,它肯定是有序的
return array
else:
# 取第一个值为基准值
base = array[0]
# 所有由小于等于基准值的元素组的子数组
less = [i for i in array[1:] if i <= base]
# 所有由大于基准值的元素组的子数组
more = [i for i in array[1:] if i > base]
return quickSort(less) +[base] + quickSort(more)
print(quickSort([10, 2, 5, 9, 20, 13, 17, 7]))
习题 08:创建一个能打印一个字典中包含所有字典对象的递归函数
dic = {
"北京":{
"东城":["天坛", "东单", "王府井"],
"西城":["西单", "军博", "复兴门"]
},
"上海":{
"杨浦":["五角场", "政通路", "世界路"]
},
"天津":["天津港"]
}
def ergodic_dict(dic):
for dic_k, dic_v in dic.items():
if isinstance(dic_v, dict):
print(dic_k)
ergodic_dict(dic_v)
else:
print((dic_k, dic_v))
dict01 = {
"北京":{
"东城": ["天坛", "东单", "王府井"],
"西城": ["西单", "军博", "复兴门"]
},
"上海":{
"杨浦": ["五角场", "政通路", "世界路"]
},
"天津": ["天津港"]
}
ergodic_dict(dict01)
习题 09:编写一个生成器,实现 Fibonacci数列的效果
def recursiveFib(n, fib):
if n == 1:
return fib
else:
fib += [fib[-1] + fib[-2]]
n -= 1
return recursiveFib(n, fib)
initial_fib = [1, 1, 2]
print(recursiveFib(13, initial_fib))