day9-homework
6959 ワード
def total(num: int):
sum1 = 0
for x in range(1, num+1):
sum1 += x
return sum1
def max_num(*nums: int):
if not nums:
return 0
else:
max1 = nums[0]
for num in nums:
if max1 < num:
max1 = num
return max1
import random
def dice(n: int):
sum1 = 0
for _ in range(n):
sum1 += random.randint(1, 6)
return sum1
#
def exchange(dict1: dict):
dict2 = {}
for key in dict1:
dict2[dict1[key]] = key
return dict2
#
def exchange(dict1: dict):
for key in dict1.copy():
dict1[dic.pop(key)] = key
return dict1
def gain_letter(str1: str):
new_str = ''
for char in str1:
if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
new_str += char
return new_str
def average(*nums: int):
sum1 = 0
length = len(nums)
if not length:
return None
else:
for num in nums:
sum1 += num
return sum1 / length
def factorial(num=10):
product = 1
if num >= 0:
for x in range(num, 1, -1):
product *= x
return product
else:
return ' !'
=======================================================
def capitalize_iwsyt(str1: str):
if 'a' <= str1[0] <= 'z':
return chr(ord(str1[0])-32)+str1[1:]
return str1
def endswith_iwsyt(str1: str, str2: str):
length1 = len(str2)
if str1[-length1:] == str2:
return True
return False
def isdigit_iwsyt(str1: str):
for char in str1:
if not '0' <= char <= '9':
return False
return True
def upper_iwsyt(str1: str):
new_str = ''
for char in str1:
if 'a' <= char <= 'z':
new_str += chr(ord(char)-32)
else:
new_str += char
return new_str
def rjust_iwsyt(str1: str, num: int, str2: str):
length = len(str1)
new_str = ''
if length == num:
return str1
elif length > num:
return ' !'
else:
return (num-length)*str2 + str1
def index_iwsyt(list1: list, target):
index_list = []
result = ','
for index in range(len(list1)):
if list1[index] == target:
index_list.append(str(index))
if index_list:
return result.join(index_list)
return -1
def len_iwsyt(seq):
count = 0
for _ in seq:
count += 1
return count
def max_iwsyt(seq):
if type(seq) == dict:
value_list = list(seq.values())
max_value = value_list[0]
for value in value_list:
if max_value < value:
max_value = value
return max_value
else:
max_value = seq[0]
for value in seq:
if max_value < value:
max_value = value
return max_value
def in_iwsyt(seq, target):
for element in seq:
if element == target:
return True
return False
def replace_iwsyt(str1: str, old_str: str, new_str: str):
list1 = str1.split(old_str)
return new_str.join(list1)
# ( , )
def intersection(list1: list, list2: list):
result_list = []
for element1 in list1:
for element2 in list2:
if element1 == element2:
if element1 not in result_list:
result_list.append(element1)
for element in result_list:
while True:
if element in list1:
list1.remove(element)
list2.remove(element)
else:
break
return list1+list2+result_list
#
def set_theory(list1: list, list2):
result_list = []
for element1 in list1:
for element2 in list2:
if element1 == element2:
result_list.append(element1)
if element1 not in result_list:
result_list.append(element1)
if len(result_list):
return result_list
return None
#
def difference_set(list1: list, list2: list):
pass
#
def complement(list1: list, list2: list):
pass