1.编写函数,求1+2+3+…N的和
def sum1(n):
"""求1+2+3+…n"""
sum2 = 0
for num in range(0, n + 1):
sum2 += num
return sum2
print(sum1(3))
2.编写一个函数,求多个数中的最大值
def max1(*num):
"""求多个数中的最大值"""
max_num = 0
for item in num:
if item > max_num:
max_num = item
return max_num
print(max1(1, 7, 2, 4, 8))
3.编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
import random
def points_sum(n):
"""求n个随机数的和"""
sum3 = 0
for i in range(n + 1):
random_num = random.randint(1, 6)
sum3 += random_num
return sum3
print(points_sum(3))
4.编写一个函数,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
def n_exchange(dict1: dict):
"""交换字典中的key和value"""
dict2 = {}
for x in dict1:
dict2.setdefault(dict1[x], x)
dict1 = dict2
print(dict1)
return
5.编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd'
def func1(a=str):
"""提取指定字符串所有的字母,组成新字符串"""
new_str = ''
for char in a:
if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
new_str += char
return new_str
print(func1('234eww12dfse'))
6.写一个函数,求多个数的平均值
def average_value(*num):
"""求多个数的平均值"""
n_sum = 0
for item in num:
n_sum += item
average_value1 = n_sum / len(num)
return average_value1
print(average_value(2, 2, 3, 3, 5))
7.写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def n_mul(n=10):
"""求1*2*3*...n的值"""
mul1 = 1
for num in range(1, n + 1):
mul1 *= num
return mul1
print(n_mul())
8.写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
def n_capitalize(a=str):
"""将字符串首字母变成大写"""
n_str = a[1:]
str1 = a[0]
if 'a' <= str1 <= 'z':
n_str = str1.upper() + n_str
else:
n_str = a
return n_str
print(n_capitalize('b23xcs'))
9.写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True
字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
def n_endswith(a=str, b=str):
"""判断a是否以b结束"""
result = a[-(len(b)):] == b
return result
print(n_endswith('abc231ab', 'ab1'))
10.写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True
'23函数' 结果: False
'a2390' 结果: False
def n_isdigit(a=str):
"""判断a是否是纯数字字符"""
for char in a:
result1 = char in '123456789'
return result1
print(n_isdigit('1235464'))
11.写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1'
def n_upper(a=str):
"""将a中所有小写字母变成大写字母"""
dict1 = {'a':'A','b':'B','c':'C','d':'D','e':'E','f':'F','g':'G','h':'H','i':'I','j':'J',
'k':'K','l':'L','m':'M','n':'N','o':'O','p':'P','q':'Q','r':'R','s':'S','t':'T',
'u':'U','v':'V','w':'W','x':'X','y':'Y','z':'Z'}
for char in a:
if 'a' <= char <= 'z':
a = a.replace(char,dict1[char])
return a
print(n_upper('abH23好rp1'))
12.写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
例如: 原字符:'abc' 宽度: 7 字符:'^' 结果: '^^^^abc'
原字符:'你好吗' 宽度: 5 字符:'0' 结果: '00你好吗'
"""创建一个b长度的字符串,a在新字符串中右对齐,剩下的用c来填充"""
def n_rjust(b,a=str,c=str):
list1 = c*(b-len(a))
n_list = list1+a
return n_list
print(n_rjust(7,'abc','^'))
13.写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 结果: 0,4,6
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '赵云' 结果: 0,4
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '关羽' 结果: -1
def n_index(a=list,b=str):
"""统计a中b元素的下标"""
n = 0
list1 = []
for index in range(len(a)):
if a[index] == b:
n += 1
list1.append(index)
if n == 0:
return -1
return list1
print(n_index([1, 2, 45, 'abc', 1, '你好', 1, 0],1))
14.写一个自己的len函数,统计指定序列中元素的个数
例如: 序列:[1, 3, 5, 6] 结果: 4
序列:(1, 34, 'a', 45, 'bbb') 结果: 5
序列:'hello w' 结果: 7
def n_len(a):
"""统计a的元素个数"""
count = 0
for item in a:
count += 1
return count
print(n_len([1, 3, 5, 6]))
15.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1
序列:'abcdpzasdz' 结果: 'z'
序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
def n_max(a):
"""求a中元素的最大值"""
if type(a) == dict:
new_max = 0
for key in a:
if a[key] > new_max:
new_max = a[key]
else:
new_max = a[0]
for item in a:
if item > new_max:
new_max = item
return new_max
print(n_max([-7, -12, -1, -9]))
16.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
例如: 序列: (12, 90, 'abc') 元素: '90' 结果: False
序列: [12, 90, 'abc'] 元素: 90 结果: True
def n_in(a,b):
"""判断b是否在a中存在"""
n = 0
for item in a:
if item == b:
result = True
n += 1
break
if n == 0:
result = False
return result
print(n_in((12, 90, 'abc'),'90'))
17.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
def n_replace(a=str,b=str,c=str):
"""将a中指定的所有b替换成c"""
start_index = 0
while True:
index = a.find(b, start_index)
if index == -1:
break
a = a[:index] + c + a[index + len(b):]
return a
print(n_replace('how are you? and you?','you','me'))
18写四个函数,分别实现求两个列表的交集、并集、差集、补集的功能
def intersection(a=list,b=list):
"""求a,b的交集"""
list3 = []
for item1 in a:
for item2 in b:
if item1==item2:
list3.append(item1)
return list3
def union_set(a=list,b=list):
"""求a,b的并集"""
list4 = list(set(a + b))
return list4
def difference_set(a=list,b=list):
"""求a,b的差集"""
list5 = list(set(a) - set(b))
return list5
def complementary_set(a,b):
"""求a,b的补集"""
list6 = list(set(a) ^ set(b))
return list6
print(intersection([1,3,2,5,4],[2,1,3,4]))
print(union_set([1,3,2,5,4],[2,1,3,4]))
print(difference_set([1,3,4],[3,5]))
print(complementary_set([1,3,4,5],[3,4,6,5]))