2019-04-26
1.编写函数,求1+2+3+…N的和
def yxy_sum(n):
"""
:param n: 参数
:return:
"""
sum1 = 0
for i in range(1, n+1):
sum1 += i
print('n个数的和是:', sum1)
yxy_sum(10)
2.编写一个函数,求多个数中的最大值
def max1(*num):
print('最大值是:', max(num))
max1(10, 20, 5, 60, 90)
3.编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
import random
def yxy_sum1(n):
"""
:param n: 骰子个数
:return:
"""
sum1 = 0
for i in range(n):
nums = random.randint(1, 6)
sum1 += nums
print('%d个骰子的和是:' %n, sum1)
yxy_sum1(3)
4.编写一个函数,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
dict1 = {'a': 1, 'b': 2}
def exchange1(dictx):
# 注意:遍历删除和增加,遍历对象应该是原来没有进行修改的原容器的值
for key in dictx.copy():
value = dictx[key]
dictx.pop(key)
dictx[value] = key
print(dictx)
exchange1(dict1)
5.编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd'
def extract(a: str):
str1 = ''
for x in a:
if 'a' <= x <= 'z' or 'A' <= x <= 'Z':
str1 += x
print(str1)
extract('12a&bc12d')
6.写一个函数,求多个数的平均值
def ave1(*nums):
sum1 = 0
for i in nums:
sum1 += i
print(sum1/len(nums))
ave1(10, 20, 30, 21)
7.写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def factorial1(num1 = 10):
fac1 = 1
for i in range(1, num1+1):
fac1 *= i
print(fac1)
factorial1(6)
8.写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
def cap1(str1):
str2 = ''
if 'a' <= str1[0] <= 'z':
str2 = chr(ord(str1[0])-32)
for i in str1[1:]:
str2 += i
print(str2)
cap1('abcd')
9.写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
def len1(sequence):
count = 0
for x in sequence:
if x != None:
count +=1
return count
def endwith1(str1, str2):
lenth1 = len1(str1)
lenth2 = len1(str2)
if lenth1 >= lenth2 and str1[-1:-lenth2-1:-1] == str2[::-1]:
print(True)
else:
print(False)
endwith1('abcd', 'cd')
10.写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
def isdigit1(str1):
for i in str1:
if not'0' <= i <= '9':
print(False)
break
else:
print(True)
isdigit1('124578')
11.写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
def upper1(str1):
str2 = ''
for x in str1:
if 'a' <= x <= 'z':
str2 += chr(ord(x)-32)
else:
str2 += x
print(str2)
upper1('asmJJH45')
12.写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充.
def rjust1(str1, lenth1, char1):
str2 = ''
for i in range(lenth1-len(str1)):
str2 += char1
for j in range(len(str1)):
str2 += str1[j]
print(str2)
rjust1('abc', 7, '^')
13.写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
def len1(sequence):
count = 0
for x in sequence:
if x != None:
count += 1
return count
def index1(list1, item1):
count1 = 0
list2 = []
for index in range(len1(list1)):
if item1 == list1[index]:
count1 += 1
list2.append(index)
if count1 == 0:
print(-1)
print(list2)
index1([1, 2, 2, 1], 1)
14.写一个自己的len函数,统计指定序列中元素的个数
def len1(sequence):
count = 0
for x in sequence:
if x != None:
count +=1
print(count)
len1([1, 2, 3, 5])
15.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
def max1(sequence1):
if type(sequence1) == dict:
for key in sequence1:
max2 = sequence1[key]
if sequence1[key] >= max2:
max2 = sequence1[key]
else:
max2 = sequence1[0]
for x in sequence1[1::]:
if x >= max2:
max2 = x
print(max2)
max1({'小明': 90, '张三': 76, '路飞': 30, '小花': 98})
16.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
def in1(seq1, seq2):
count = 0
dec = 0
for i in range(len(seq1)):
if seq1[i] == seq2:
dec = 1
else:
count += 1 # 记seq1中不等于seq2[0]的个数
if count == len(seq1):
print(False)
elif dec == 1:
print(True)
in1([12, 90, 'abc'], '90')
17.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
def replace1(str1, str2, str3):
str4 = ''
i = 0
index = 0
while index < len(str1): # 遍历str1
if str1[index] != str2[i]:
str4 += str1[index]
index += 1
else:
if str1[index+1:index+len(str2)] == str2[1::]:
str4 += str3
index += len(str2)
print(str4)
replace1('how are you? and you?', 'you', 'me')
18.写四个函数,分别实现求两个列表的交集、并集、差集、补集的功能
交集
def intersection(list1, list2):
set1 = set()
for x in list1:
for y in list2:
if x == y:
set1.add(x)
print(set1)
intersection([1, 3, 3, 2, 5], [1, 5, 3, 8])
并集
def union(list1, list2):
list3 = list1 + list2
set4 = set(list3)
print(set4)
union([1, 3, 3, 2, 5], [1, 5, 3, 8])
差集
def difference(list1, list2):
set1 = set(list1)
set2 = set(list2)
print(set1 - set2)
difference([1, 3, 3, 2, 5], [1, 5, 3, 8])
补集
def complementary(list1, list2):
set1 = set(list1)
set2 = set(list2)
print(set1 ^ set2)
complementary([1, 3, 3, 2, 5], [1, 5, 3, 8])