1.寫一個(gè)匿名函數(shù)趣竣,判斷指定的年是否是閏年
leap_year = lambda year: (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
print(leap_year(2016))
- 寫一個(gè)函數(shù)將一個(gè)指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自帶的逆序函數(shù))
def reverse_list(list1):
new_list1 = []
for index in list1[-1::-1]:
new_list1.append(index)
return new_list1
print(reverse_list([1, 2, 3]))
- 寫一個(gè)函數(shù),獲取指定列表中指定元素的下標(biāo)(如果指定元素有多個(gè),將每個(gè)元素的下標(biāo)都返回)
例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def get_subscript(list1, i):
for x in range(0, len(list1)):
if list1[x] == i:
print(x)
get_subscript([1, 3, 4, 1], 3)
- 寫一個(gè)函數(shù)呜魄,能夠?qū)⒁粋€(gè)字典中的鍵值對(duì)添加到另外一個(gè)字典中(不使用字典自帶的update方法)
def add_dict(dict1, dict2):
for index in dict1:
dict2[index] = dict1[index]
return dict2
print(add_dict({'a': 1, 'b': 2}, {'c': 6}))
- 寫一個(gè)函數(shù)犯戏,能夠?qū)⒅付ㄗ址械乃械男懽帜皋D(zhuǎn)換成大寫字母仲智;所有的大寫字母轉(zhuǎn)換成小寫字母(不能使用字符串相關(guān)方法)
def alpha(str1):
change_alpha = 0
new_str1 = ''
for index in str1:
if 'a' <= index <= 'z':
index = chr(ord(index)-32)
elif 'A' <= index <= 'Z':
index = chr(ord(index) + 32)
new_str1 += index
return new_str1
print(alpha('dfhs45KI'))
- 實(shí)現(xiàn)一個(gè)屬于自己的items方法,可以將自定的字典轉(zhuǎn)換成列表锈死。列表中的元素是小的列表贫堰,里面是key和value (不能使用字典的items方法)
例如:{'a':1, 'b':2} 轉(zhuǎn)換成 [['a', 1], ['b', 2]]
def my_items(dict1):
new_list = []
for index in dict1:
list1 = []
list1.append(index)
list1.append(dict1[index])
new_list.append(list1)
return new_list
# 這兒是在聲明一個(gè)局部變量a1
print(my_items({'a': 1, 'b': 6}))
- 用遞歸函數(shù)實(shí)現(xiàn),逆序打印一個(gè)字符串的功能:
def reversed_print(str1):
length = len(str1)
if len(str1) == 1:
print(str1)
else:
print(str1[len(str1)-1], end='')
str1 = str1[0: length-1]
reversed_print(str1)
reversed_print('afdji')
- 編寫一個(gè)遞歸函數(shù)待牵,求一個(gè)數(shù)的n次方
def n_power(num, n):
if n == 1 and (num == 1 or num == 0):
return 1
return num**(n-1)*num
print(n_power(8, 2))
def study_id_creater(str1='abc', num=5):
for i in range(1, num+1):
yield str1 + str(i).zfill(7 - len(str1))
for id in study_id_creater():
print(id)
- 寫一個(gè)可以產(chǎn)生學(xué)號(hào)的生成器, 生成的時(shí)候可以自定制學(xué)號(hào)數(shù)字位的寬度和學(xué)號(hào)的開(kāi)頭
study_id_creater('py',5) -> 依次產(chǎn)生: 'py00001', 'py00002', 'py00003',....
study_id_creater('test',3) -> 依次產(chǎn)生: 'test001', 'test002', 'test003',...
def study_id_creater(str1, n):
for i in range(1, 6):
print(str1 + str(i).zfill(n))
study_id_creater('py', 5)
- 編寫代碼模擬打地鼠的小游戲严嗜,
假設(shè)一共有5個(gè)洞口,老鼠在里面隨機(jī)一個(gè)洞口洲敢, 人隨機(jī)打開(kāi)一個(gè)洞口漫玄,如果有老鼠,代表抓到了压彭, 如果沒(méi)有睦优,繼續(xù)打地鼠;但是地鼠會(huì)跳到其他洞口
import random
def whack_a_mole(num):
mouse = random.randint(1, num)
hole = random.randint(1, num)
print("地鼠出的洞口:", mouse)
print("打地鼠的洞口:", hole)
while mouse == hole:
return '恭喜抓到了壮不!'
return '未抓到汗盘!'
print(whack_a_mole(5))
- 編寫一個(gè)函數(shù),計(jì)算一個(gè)整數(shù)的各位數(shù)的平方和
例如: sum1(12) -> 5 sum1(123) -> 14
def power_sum(num):
sum1 = 0
for i in str(num):
sum1 += int(i)**2
return sum1
print(power_sum(12))
12.樓梯有n階臺(tái)階询一,上樓可以一步上1階隐孽,也可以一步上2階,編程序計(jì)算共有多少種不同的走法健蕊?需求: 編制一個(gè)返回值為整型的函數(shù)Fib(n)菱阵,用于獲取n階臺(tái)階的走法(掙扎一下)
def step(num):
num1 = 0
sum1 = 1
for _ in range(num):
sum2 = sum1 + num1
num1 = sum1
sum1 = sum2
print(sum2)
(step(2))
- 寫一個(gè)函數(shù)對(duì)指定的數(shù)分解因式
例如: mab(6) —> 打印: 2 3 mab(3) -> 1 3 mab(12) -> 2 2 3
def factorization(num):
i = 2
list1 = []
if num == 1:
list1.append(num)
while i <= num:
if num % i == 0:
list1.append(str(i))
num /= i
else:
i += 1
if len(list1) == 1:
list1.insert(0, 1)
return ' '.join(list1)
print(factorization(6))
- 寫一個(gè)函數(shù)判斷指定的數(shù)是否是回文數(shù)
123321是回文數(shù) 12321是回文數(shù) 525是回文數(shù)
def plalindrome(str1):
str2 = list(str(str1))
str2.reverse()
if str(str1) == ''.join(str2):
return True
else:
return False
print(plalindrome(12321))
- 寫一個(gè)函數(shù)判斷一個(gè)數(shù)是否是丑數(shù)(自己百度丑數(shù)的定義)
def ugly_number(num: int):
if num <= 0:
return False
while num % 2 == 0:
num = num / 2
while num % 3 == 0:
num = num / 3
while num % 5 == 0:
num = num / 5
if num == 1:
return True
else:
return False
print(ugly_number(19))
def study_id_creater(str1='abc', num=5):
for i in range(1, num+1):
yield str1 + str(i).zfill(7 - len(str1))
gen1 = study_id_creater()
print(next(gen1))
print(next(gen1))
print(next(gen1))
print(next(gen1))
print(next(gen1))