HW
1.寫一個匿名函數(shù),判斷指定的年是否是閏年
fn = lambda x : x % 400 == 0 or (x % 4 == 0 and x %100 != 0)
print(fn(1999))
print(fn(1600))
print(fn(2004))
2.寫一個函數(shù)將一個指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自帶的逆序函數(shù))
def reverse_num(list1:list):
for list in list1:
return list1[::-1]
print(reverse_num([1, 2, 3]))
3.寫一個函數(shù),獲取指定列表中指定元素的下標(如果指定元素有多個,將每個元素的下標都返回)
例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def traver_ele(list1, element):
index_list = []
for i in range(len(list1)):
if element == list1[i]:
index_list.append(i)
return index_list
4.寫一個函數(shù),能夠?qū)⒁粋€字典中的鍵值對添加到另外一個字典中(不使用字典自帶的update方法)
def tsfm_dict(dict1:dict, dict2:dict):
for key in dict1:
dict2[key] = dict1[key]
return dict2
print(tsfm_dict({'a':2},dict2={}))
5.寫一個函數(shù),能夠?qū)⒅付ㄗ址械乃械男懽帜皋D(zhuǎn)換成大寫字母;所有的大寫字母轉(zhuǎn)換成小寫字母(不能使用字符串相關方法)
def caps_o_f(str1:str):
n_str = ''
for i in str1:
if 'A' <= i <= 'Z':
n_str += chr(ord(i) + 32)
elif 'a' <= i <= 'z':
n_str += chr(ord(i) - 32)
return n_str
print(caps_o_f('AsDdfffEE'))
6.實現(xiàn)一個屬于自己的items方法,可以將指定的字典轉(zhuǎn)換成列表。列表中的元素是小的列表,里面是key和value (不能使用字典的items方法)
def m_items(dict1:dict):
new_list = []
for key in dict1:
new_list.append([key, dict1[key]])
return new_list
print(m_items({'a':1, 'b':2}))
7.用遞歸函數(shù)實現(xiàn),逆序打印一個字符串的功能:
""" 例如:reverse_str('abc') -> 打印 ‘cba’"""
def background(str1:str):
if str1 == 1:
print('abc')
return
# return str1[::-1]
print(str[-1])
print(str[-1])
print(background('abc'))
8.編寫一個遞歸函數(shù),求一個數(shù)的n次方
def pow_num(x, y):
if x == 1:
return x
return pow_num(x, x - y)*x
print(pow(3, 4))
9.寫一個可以產(chǎn)生學號的生成器, 生成的時候可以自定制學號數(shù)字位的寬度和學號的開頭
'''
例如:
study_id_creater('py',5) -> 依次產(chǎn)生: 'py00001', 'py00002', 'py00003',....
study_id_creater('test',3) -> 依次產(chǎn)生: 'test001', 'test002', 'test003',...
'''
def id_made(str1, width):
srt_id = str1
for i in range(1, 10 ** width):
yield srt_id + str(i).rjust(width, '0')
study_id = id_made('py', 5)
print(next(study_id))
print(next(study_id))
10.編寫代碼模擬達的鼠的小游戲
# 假設一共有5個洞口,老鼠在里面隨機一個洞口;
# 人隨機打開一個洞口,如果有老鼠,代表抓到了
# 如果沒有,繼續(xù)打地鼠;但是地鼠會跳到其他洞口
import random
def hit_mouse_game(nums:int):
hole = random.randint(0, 5)
while True:
choose_hole = nums
if hole < choose_hole:
print('more than this hole num, choose others')
elif hole > choose_hole:
print('less than this hole num, choose others')
else:
print('Congratulation! Mouse has benn done!!')
break
return nums
print(hit_mouse_game(5))
11.編寫一個函數(shù),計算一個整數(shù)的各位數(shù)的平方和
def calculation(nums):
sum1 = 0
while nums >0:
u = nums % 10
nums = nums //10
sum1 += u **2
return sum1
print(calculation(34))
12.樓梯有n階臺階,上樓可以一步上1階,也可以一步上2階,編程序計算共有多少種不同的走法?
需求: 編制一個返回值為整型的函數(shù)Fib(n),用于獲取n階臺階的走法(掙扎一下)
def prime_factor(n):
if n == 0 or n ==1:
return 1
return prime_factor(n-1) + prime_factor(n-2)
print(prime_factor(8))
13.寫一個函數(shù)對指定的數(shù)分解因式
"""例如: mab(6) —> 打印: 2 3 mab(3) -> 1 3 mab(12) -> 2 2 3"""
```python
def resolve(num):
list1 = []
i = 2
while num > 0 and i < num:
if not (num % i):
list1.append(i)
num = num // i
i = 2
else:
i += 1
list1.append(num)
if len(list1) == 1:
list1.insert(0,1)
return list1
print(resolve(9))
14.寫一個函數(shù)判斷指定的數(shù)是否是回文數(shù)
"""123321是回文數(shù) 12321是回文數(shù) 525是回文數(shù)"""
def palindromic_number(num):
str1 = str(num)
if str1 == str1[::-1]:
return True
else:
return False
print(palindromic_number(12321))
15.寫一個函數(shù)判斷一個數(shù)是否是丑數(shù)(自己百度丑數(shù)的定義)
flag = 1
def ugly_number(num):
global flag
list1 = [2, 3, 5]
while num > 1 :
for i in range(len(list1)):
if not (num % list1[i]):
flag = 1
num = num //list1[i]
ugly_number(num)
break
else:
flag = 0
break
if flag == 1:
return True
else:
return False