- 编写函数,求1+2+3+…N的和
def sum_cc(N):
sum = 0
for x in range(1, N + 1):
sum += x
print('和为',sum)
N = int(input('请输入一个正整数'))
sum_cc(N)
- 编写一个函数,求多个数中的最大值
def max_cc(*nums):
num1 = 0
for num in nums:
if num > num1:
num1 = num
print('最大值为', num1)
max_cc(12,23,44,55,655,66,43,423)
- 编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
import random
def sum_cc(N):
sum = 0
for x in range(1, N + 1):
random_num = random.randint(1, 6)
sum += random_num
print(sum)
N = int(input('请输入摇骰子的次数'))
sum_cc(N)
- 编写一个函数,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
def exchange_cc(dict1):
dict2 = {}
for key in dict1:
dict2[dict1[key]] = key
print(dict2)
dict1={'a':1, 'b':2, 'c':3}
exchange_cc(dict1)
- 编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd'
def letter_cc(string):
string1 = ''
for x in string:
if 'a' <= x <= 'z' or 'A' <= x <= 'Z':
string1 += x
print(string1)
string = 'shdhs3hd8273_+*&^dhgdhsjsj'
letter_cc(string)
- 写一个函数,求多个数的平均值
def average_cc(*nums):
sum1 = 0
for num1 in nums:
sum1 += num1
average = sum1/(len(nums))
print(average)
average_cc(1,2,3,4,5,6,7,8,9,10)
- 写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def fatorial_cc(a=10):
fatorial1 = 1
for x in range(1,11):
fatorial1 *= x
print(fatorial1)
fatorial_cc()
=====================注意:以下方法不能使用系统提供的方法和函数,全部自己写逻辑==============
- 写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd'
def capitalize_cc(string1):
string2 = ''
if 'a' <= string1[0] <= 'z':
string2 += chr(ord(string1[0])-32)
for s in range(1,len(string1)):
string2 += string1[s]
else:
string2 = string1
print(string2)
string1 = '1absb2343hdh_'
capitalize_cc(string1)
- 写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True
字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False
def endwith_cc(string1,string2):
if string1[len(string1)-1:len(string1)-1-len(string2):-1] == string2[::-1]:
print('True')
else:
print('False')
endwith_cc('addhhf3436*', '3436*')
- 写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True
'23函数' 结果: False
'a2390' 结果: False
def isdigit_cc(string):
string2 = ''
for x in string:
if 'a' <= x <= 'z' or 'A' <= x <= 'Z':
string2 += x
if string2 == string:
print('True')
else:
print('False')
isdigit_cc('sdhsafhH7ddf')
- 写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1'
def upper_cc(string):
string2 = ''
for i in range(0,len(string)):
if 'a' <= string[i] <= 'z':
string2 += chr(ord(string[i])-32)
else:
string2 += string[i]
print(string2)
upper_cc('shdhahD')
- 写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
例如: 原字符:'abc' 宽度: 7 字符:'^' 结果: '^^^^abc'
原字符:'你好吗' 宽度: 5 字符:'0' 结果: '00你好吗'
def rjust_cc(string,str,N):
string2 = ''
string2 += str * N
for s in string:
string2 += s
print(string2)
rjust_cc('hsdhdjs','d',3)
- 写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 结果: 0,4,6
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '赵云' 结果: 0,4
列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '关羽' 结果: -1
def index_cc(list1,elements):
list2 = ''
count = 0
for x in range(0,len(list1)):
if elements == list1[x]:
list2 += str(x) +str(' ')
count = 1
if count == 0:
return -1
else:
return list2
print(index_cc([12,33,44,12,'jdj',34,'jdj',12,'jdij',34,56,78,'jdj'],'jdj'))
- 写一个自己的len函数,统计指定序列中元素的个数
例如: 序列:[1, 3, 5, 6] 结果: 4
序列:(1, 34, 'a', 45, 'bbb') 结果: 5
序列:'hello w' 结果: 7
def len_cc(string):
count = 0
for x in string:
count += 1
print(count)
len_cc('shhshshdhs343')
- 写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1
序列:'abcdpzasdz' 结果: 'z'
序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98
def max_cc(array):
max_num = 0
for i in array:
if type(i) == dict:
for key in i:
if i[key] > max_num:
max_num = i[key]
else :
if i > max_num:
max_num = i
print(max_num)
max_cc([11,23,4,45,5,900,6,6,6,66,6])
- 写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
例如: 序列: (12, 90, 'abc') 元素: '90' 结果: False
序列: [12, 90, 'abc'] 元素: 90 结果: True
def in_cc(array,elements):
for x in array:
if x == elements:
print('True')
break
else:
print('False')
in_cc([12,12,34,56,78,89],12)
- 写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?'
def replace_cc(string,old_chars,new_chars):
return new_chars.join(string.split(old_chars))
- 写四个函数,分别实现求两个列表的交集、并集、差集、补集的功能
def intersection_cc(list1,list2):
list_new = []
for x in list1:
for y in list2:
if x == y:
list_new.append(x)
print(list_new)
intersection_cc([1,2,3,4,5],[1,3,6,7,5])
def union_cc(list1,list2):
list_new = list1
for y in list2:
list_new.append(y)
list_new = list(set(list_new))
print(list_new)
union_cc([1,2,3,7,5],[3,4,5])
def subtract_cc(list1,list2):
for x in list1:
for y in list2:
if x == y:
list1.remove(x)
print(list1)
subtract_cc([1,2,3,4,5,6],[1,3,5,6,7,8])
def complement_cc(list1,list2):
list_new = []
for x in list1:
if x not in list2:
list_new.append(x)
for y in list2:
if y not in list1:
list_new.append(y)
print(list_new)
complement_cc([1,2,34,5,6,7,2],[34,5,8,2,9,45])