1.编写函数,求1 + 2 + 3 +…N的和
def sum(n):
sum = 0
for i in range(1, n+1):
sum += i
return sum
n = int(input('请输入求的个数:'))
print(sum(n))
#100
#5050
2.编写一个函数,求多个数中的最大值
def nums_max(*nums):
num = 0
for i in nums:
if i > num:
num = i
return num
def nums_max(*nums):
num = max(nums)
return num
num = nums_max(10,20,4,30,12,4,66)
print(num)
# 66
3.编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
import random
def sum_dice(list1):
sum = 0
for i in range(len(list1)):
sum += list1[i]
return sum
n = int(input('请输入次数:'))
list1 = []
for i in range(n):
list1.append(random.randint(1,6))
print(sum_dice(list1))
# 3
# 10
#
import random
def sum_dice(n):
sum = 0
for i in range(n):
num = random.randint(1, 6)
sum += num
return sum
n = int(input('请输入次数:'))
print(sum_dice(n))
4.编写一个函数,交换指定字典的key和value。
例如: dict1 = {'a': 1, 'b': 2, 'c': 3} --> dict1 = {1: 'a', 2: 'b', 3: 'c'}
def exchange(dict1):
dict2 = {}
for key in dict1:
dict2[dict1[key]] = key
return dict2
dict1 = {'a': 1, 'b': 2, 'c': 3}
print(exchange(dict1))
def exchange(dict1):
for k in dict1.copy():
k, dict1[k] = dict1[k], k
del dict1[dict1[k]]
return dict1
def change(dict1):
for key in dict1.copy():
dict1[dict1.pop(key)] = key
return dict1
dict1 = {'a':1, 'b':2, 'c':3}
print(exchange(dict1))
print(change(dict1))
# {1: 'a', 2: 'b', 3: 'c'}
5.编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd'
def letter_joint(str):
str1 = ''
for i in str:
if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
str1 += i
return str1
str = '12a&bc12d-+'
print(letter_joint(str))
# abcd
6.写一个函数,求多个数的平均值
def _avg(*nums):
sum = 0
for i in range(len(nums)):
sum += nums[i]
return sum/len(nums)
print(_avg(10,20,30,12,43,647,8,1))
# 96.375
7.写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def factorial(n = 10):
if n == 0:
return 1
else:
mul = 1
for i in range(1, n+1):
mul *= i
return mul
str = input('请输入要阶乘的数字:')
if str.isdigit():
print(factorial(int(str)))
else:
print(factorial())
# 3 6
# \n 3 628 800
8.写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
def _capitalize(str):
if 'a' <= str[0] <= 'z':
return chr(ord(str[0]) - 32) + str[1:]
else:
return str
str1 = 'abc'
str2 = '12asd'
print(_capitalize(str1),_capitalize(str2))
# Abc 12asd
9.写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True
字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
def _endwith(str1, str2):
if str1[len(str1)-len(str2):len(str1)] == str2:
return True
else:
return False
str1 = 'abc231ab'
str2 = 'ab'
str3 = 'abc231ab'
str4 = 'ab1'
print(_endwith(str1, str2),_endwith(str3,str4))
# True False
10.写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True
'23函数' 结果: False
'a2390' 结果: False
def _isdigit(str):
for i in str:
if '0' <= i <= '9':
pass
else:
return False
else:
return True
str1 = '1234921'
str2 = '23函数'
str3 = 'a2390'
print(_isdigit(str1), _isdigit(str2), _isdigit(str3))
11.写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1'
def _upper(str):
str1 = ''
for i in str:
if 'a' <= i <= 'z':
str1 += chr(ord(i) - 32)
else:
str1 += i
return str1
str = 'abH23好rp1'
print(_upper(str))
# ABH23好RP1
12.写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
例如: 原字符:'abc' 宽度: 7 字符:'^' 结果: '^^^^abc'
原字符:'你好吗' 宽度: 5 字符:'0' 结果: '00你好吗'
def _rjust(str, width, char):
str1 = char * (width - len(str)) + str
return str1
str1 = 'abc'
str2 = '你好吗'
print(_rjust(str1, 7, '^'),_rjust(str2, 5, '0'))
# ^^^^abc 00你好吗
13.写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回 - 1
例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 结果: 0,4,6
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '赵云' 结果: 0,4
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '关羽' 结果: -1
def index(list, ele):
count = 0
index1 = []
for i in range(len(list)):
if list[i] == ele:
index1.append(i)
count += 1
if count == 0:
return -1
else:
return index1
list1 = [1, 2, 45, 'abc', 1, '你好', 1, 0]
list2 = ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
list3 = ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
print(index(list1, 1))
print(index(list2, '赵云'))
print(index(list3, '关羽'))
# [0, 4, 6]
# [0, 4]
# -1
14.写一个自己的len函数,统计指定序列中元素的个数
例如: 序列:[1, 3, 5, 6] 结果: 4
序列:(1, 34, 'a', 45, 'bbb') 结果: 5
序列:'hello w' 结果: 7
def _len(array):
count = 0
for i in array:
count += 1
return count
list1 = [1, 3, 5, 6]
tuple1 = (1, 34, 'a', 45, 'bbb')
str1 = 'hello w'
print(_len(list1), len(tuple1), len(str1))
# 4 5 7
15.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1
序列:'abcdpzasdz' 结果: 'z'
序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
def _max(array):
if type(array) == dict:
max = 0
for key in array:
if array[key] > max:
max = array[key]
return max
else:
max1 = array[0]
for i in range(len(array)):
if array[i] > max1:
max1 = array[i]
return max1
list1 = [-7, -12, -1, -9]
str1 = 'abcdpzasdz'
dict1 ={'小明':90, '张三': 76, '路飞':30, '小花': 98}
print(_max(list1), _max(str1),_max(dict1))
# -1 z 98
16.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
例如: 序列: (12, 90, 'abc') 元素: '90' 结果: False
序列: [12, 90, 'abc'] 元素: 90 结果: True
def _in(array, char):
for i in array:
if i == char:
return True
else:
return False
list1 = [12, 90, 'abc']
tuple1 = (12, 90, 'abc')
print(_in(list1,'90'), _in(tuple1,90))
# False True
17.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
def _replace(str1, str2, str3):
str = ''
j = -len(str1)
for i in range(len(str1)):
if str1[i:i+len(str2)] == str2:
str += str3
j = i
elif j < i < j + len(str2):
pass
else:
str += str1[i]
return str
str1 = 'how are you? and you?'
str2 = 'you'
str3 = 'me'
print(_replace(str1,str2,str3))
# how are me? and me?
def _replace(str1, old, new):
str_list = str1.split(old)
return new.join(str_list)
str1 = 'how are you? and you?'
old = 'you'
new = 'me'
print(_replace(str1,old,new))
18.写四个函数,分别实现求两个列表的交集、并集、差集、补集的功能
def intersection(list1, list2): #交集
list3 = []
for i in list1:
for j in list2:
if i == j:
list3.append(i)
return list3
def union(list1, list2): # 并集
list3 = []
for i in list1:
if i not in list3:
list3.append(i)
for j in list2:
if j not in list3:
list3.append(j)
return list3
def diff(list1, list2): #差集
list3 = list1[:]
for i in list2:
if i in list3:
list3.remove(i)
return list3
def symmetry_diff(list1, list2): # 对称差集, 等于(A - B) U ( B - A)
list3 = list1[:]
for i in list2:
if i in list3:
list3.remove(i)
for i in list1:
if i in list2:
list2.remove(i)
for i in list2:
list3.append(i)
return list3
def complement(list1, list2): # 补集
list3 = []
for i in list1:
if i in list2:
pass
else:
break
else:
for j in list2:
if j not in list1:
list3.append(j)
return list3
list1 = [1,3,4,5,7]
list2 = [1,3,5,7,8]
list3 = [1,2,3]
list4 = [1,2,3,7]
list5 = [1,2,3,5,9,10]
print(intersection(list1, list2))
print(union(list1, list2))
print(diff(list1, list2))
print(symmetry_diff(list1, list2))
print(complement(list3,list5))
print(complement(list4,list5))
# [1, 3, 5, 7]
# [1, 3, 4, 5, 7, 8]
# [4]
# [4, 8]
# [5, 9, 10]
# []