import random
# 1.编写函数,求1 + 2 + 3 +…N的和
def xu_sum(n):
"""求1 + 2 + 3 +…N的和 """
sum1 = 0
for num in n:
sum1 += num
return sum1
# 2.编写一个函数,求多个数中的最大值
def xu_max(*args: int):
"""求多个数中的最大值"""
max_num = args[0]
for num in args:
if num > max_num:
max_num = num
return max_num
# 3.
# 编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
def dice(n: int):
"""打印N个骰子的点数和"""
sum1 = 0
for times in range(n):
rand_num = random.randint(1, 6)
print(rand_num)
sum1 += rand_num
print(sum1)
dice(3)
# 4.
# 编写一个函数,交换指定字典的key和value。
# ```python
# 例如: dict1 = {'a': 1, 'b': 2, 'c': 3} --> dict1 = {1: 'a', 2: 'b', 3: 'c'}
# ```
def ex_dict(dict0: dict):
"""交换指定字典的key和value"""
# 交换将导致原来字典中value相同的键值对随机只能保留一个,键值交换则无法避免
# 若直接交换,还会导致原来以某个元素的值作为键的元素消失
# for key in dict0:
# dict0[dict0[key]] = key
# dict0.pop(key)
# 采用新字典并返回的形式交换
new_dict = {}
for key in dict0:
new_dict[dict0[key]] = key
return new_dict
dict1 = {'a': 1, 'b': 2, 'c': 3}
print(ex_dict(dict1))
# 5.
# 编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
#
# ```python
# 例如: 传入
# '12a&bc12d-+' --> 'abcd'
# ```
def get_letter(string: str):
"""提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串"""
new_string = ''
for s in string:
if 'a' <= s <= 'z' or 'A' <= s <= 'Z':
new_string += s
return new_string
print(get_letter('3asA14dfQ'))
# 6.
# 写一个函数,求多个数的平均值
def xu_average(*args: int):
"""求多个数的平均值"""
sum1 = 0
for num in args:
sum1 += num
return sum1/len(args)
print(xu_average(1, 4, 8, 3))
# 7.
# 写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
#
# ######=====================注意:以下方法不能使用系统提供的方法和函数,全部自己写逻辑==============
def xu_factorial(num=10):
"""默认求10的阶乘,也可以求其他数字的阶乘"""
factorial_num = 1
for n in range(2, num+1):
factorial_num *= n
return factorial_num
print(xu_factorial())
# 8.
# 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
# ```python
# 例如: 'abc' -> 'Abc' '12asd' --> '12asd'
# ```
def capitalize(string: str):
"""将指定字符串的首字母变成大写字母"""
if 'a' <= string[0] <= 'z':
first_letter = chr(ord(string[0]) - 32)
return first_letter + string[1:]
print(capitalize('sfsaf'))
# 9.
# 写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
# ```python
# 例如: 字符串1:'abc231ab'
# 字符串2: 'ab'
# 函数结果为: True
# 字符串1: 'abc231ab'
# 字符串2: 'ab1'
# 函数结果为: False
# ```
def xu_endswith(string1: str, string2: str):
"""判断一个string1是否以string2结束"""
str1_len = len(string1)
str2_len = len(string2)
if string2 == string1[-str2_len:]:
return True
else:
return False
print(xu_endswith('asdaf', 'af'))
# 10.
# 写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
#
# ```python
# 例如: '1234921'
# 结果: True
# '23函数'
# 结果: False
# 'a2390'
# 结果: False
def xu_isdigit(string: str):
"""判断一个字符串是否是纯数字字符串"""
for s in string:
if not '0' <= s <= '9':
return False
else:
return True
print(xu_isdigit('324235撒'))
# 11.
# 写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
#
# ```python
# 例如: 'abH23好rp1'
# 结果: 'ABH23好RP1'
def xu_upper(string: str):
"""将一个字符串中所有的小写字母变成大写字母"""
new_string = ''
for s in string:
if 'a' <= s <= 'z':
s = chr(ord(s) - 32)
new_string += s
return new_string
print(xu_upper('ssAfs10'))
# 12.
# 写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
# ```python
# 例如: 原字符:'abc'
# 宽度: 7
# 字符: '^'
# 结果: '^^^^abc'
# 原字符: '你好吗'
# 宽度: 5
# 字符: '0'
# 结果: '00你好吗'
# ```
def xu_rjust(string: str, string1: str, num: int):
"""创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充"""
if num < len(string):
print('需正确输入宽度')
return None
new_str = ''
for _ in range(num - len(string)):
new_str += string1
new_str += string
return new_str
print(xu_rjust('sds', '*', 5))
# 13.
# 写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回 - 1
#
# ```python
# 例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0]
# 元素: 1
# 结果: 0, 4, 6
# 列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
# 元素: '赵云'
# 结果: 0, 4
# 列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
# 元素: '关羽'
# 结果: -1
def xu_index(list2: list, item):
""" 统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回 - 1"""
all_index = []
for index in range(len(list2)):
if list2[index] == item:
all_index.append(index)
if all_index:
return all_index
else:
return -1
list1 = ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权']
print(xu_index(list1, '关羽'))
# 14.
# 写一个自己的len函数,统计指定序列中元素的个数
#
# ```python
# 例如: 序列:[1, 3, 5, 6]
# 结果: 4
# 序列: (1, 34, 'a', 45, 'bbb')
# 结果: 5
# 序列: 'hello w'
# 结果: 7
def xu_len(seq):
"""统计指定序列中元素的个数"""
seq_len = 0
for item in seq:
seq_len += 1
return seq_len
print(xu_len('asfdasf'))
#
# 15.
# 写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
# ```python
# 例如: 序列:[-7, -12, -1, -9]
# 结果: -1
# 序列: 'abcdpzasdz'
# 结果: 'z'
# 序列: {'小明': 90, '张三': 76, '路飞': 30, '小花': 98}
# 结果: 98
dict1 = {'小明': 90, '张三': 76, '路飞': 30, '小花': 98}
def xu_max_seq(seq):
if isinstance(seq, dict):
max_value = seq.popitem()[1]
for key in seq:
if seq[key] > max_value:
max_value = seq[key]
else:
max_value = seq[0]
for item in seq:
if item > max_value:
max_value = item
return max_value
print(xu_max_seq({'小明': 90, '张三': 76, '路飞': 30, '小花': 98}))
# 16.
# 写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
#
# ```python
# 例如: 序列: (12, 90, 'abc')
# 元素: '90'
# 结果: False
# 序列: [12, 90, 'abc']
# 元素: 90
# 结果: True
def xu_in(seq, item2):
for item in seq:
if item == item2:
return True
else:
return False
print(xu_in((12, 90, 'abc'), 90))
# 17.
# 写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
# ```python
# 例如: 原字符串: 'how are you? and you?'
# 旧字符串: 'you'
# 新字符串: 'me'
# 结果: 'how are me? and me?'
def xu_replace(string: str, target_string: str, turn_to_string: str):
"""将指定string1中指定的旧字符串转换成指定的新字符串"""
i = 0
new_str = ''
target_len = len(target_string)
while i < len(string):
if string[i:target_len+i] == target_string:
new_str += turn_to_string
i += target_len
continue
new_str += string[i]
i += 1
return new_str
str11 = xu_replace('how are you? and you?', 'you', 'me')
print(str11)
# 18.
# 写四个函数,分别实现返回两个列表的交集、并集、差集、补集的功能
def intersect_list(list1: list, list2: list):
"""返回列表的交集"""
new_list = []
for item in list1:
if item in list2:
new_list.append(item)
list2.remove(item)
return new_list
print(intersect_list([1, 1, 2, 3, 4], [1, 2, 5, 6, 7]))
def union_list(list1: list, list2: list):
"""返回列表的并集"""
new_list = list2.copy()
for item in list1:
if item not in list2:
new_list.append(item)
else:
list2.remove(item)
return new_list
print(union_list([1, 1, 2, 3, 4], [1, 2, 5, 6, 7]))
def diff_list(list1: list, list2: list):
"""返回list1-list2的差集"""
new_list = list1.copy()
for item in list2:
if item in new_list:
new_list.remove(item)
return new_list
print(diff_list([1, 1, 2, 3, 4], [1, 2, 5, 6, 7]))
def sup_list(list1: list, list2: list):
"""返回列表的补集"""
new_list1 = list1.copy()
new_list2 = list2.copy()
for item in list2:
if item in new_list1:
new_list1.remove(item)
for item in list1:
if item in new_list2:
new_list2.remove(item)
return new_list1 + new_list2
print(sup_list([1, 1, 2, 3, 4], [1, 2, 5, 6, 7]))
day10homework
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 6月11日到13日,我有幸被联社抽调到临猗农商行参与检查汇总。临走的时候,王蕴华监事长就一再地说:吃透文件精神,和...