Python学习 —— 阶段综合练习一
Python 阶段综合练习一
综合之前的函数、数据结构、流程控制等,做以下实例练习:(建议先不要看代码,自己先试着写;代码仅供参考,有多种实现方法)
1. 定义 is_Even 函数,传一 int 参数,判断是否是偶数,return True;不是,return False
1 '''is_Even''' 2 def is_Even(i): 3 if(i%2==0): 4 return True 5 else: 6 return False 7 print(is_Even(0),is_Even(1),is_Even(1))
2. 定义 is_Int 函数,传一 float 参数,判断是否是整数(如1.0 即整数),return True;不是,return False
1 '''is_Int''' 2 def is_Int(f): 3 if(f%1==0): 4 return True 5 else: 6 return False 7 print(is_Int(0),is_Int(1.0),is_Int(1.1))
3. 定义 digital_sum 函数,传一 int 参数,return 其所有位数的数字相加的和;如:1234 返回 10(1+2+3+4); 900 返回 9 (9+0+0)
1 '''digital_sum''' 2 def digital_sum(i): 3 sum = 0 4 while(i/10>1): 5 sum += i%10 6 i = int(i/10) 7 sum += i 8 return sum 9 print(digital_sum(23423),digital_sum(900),digital_sum(0))
1 def digital_sum(i): 2 i = str(i) 3 sum = 0 4 for d in i: 5 sum += int(d) 6 return sum 7 print(digital_sum(23423),digital_sum(900),digital_sum(0))
4. 定义 factorial 函数,传一 int 参数,return 其所有位数数字相乘的积;如:1234 返回 24 (1*2*3*4); 909 返回 0 (9*0*9)
1 '''factorial''' 2 def factorial(i): 3 result = 1 4 while(i/10>1): 5 result *= i%10 6 i = int(i/10) 7 result *= i 8 return result 9 print(factorial(1234),factorial(909),factorial(0))
1 '''factorial''' 2 def factorial(i): 3 i = str(i) 4 result = 1 5 for d in i: 6 result *= int(d) 7 return result 8 print(factorial(1234),factorial(909),factorial(0))
5. 定义 is_prime 函数,传一 int 参数,判断是否是质数(仅可被1或自己整除),若是,return True;不是,return False; 如:1 返回 True;2 返回 True;4 返回 False;13 返回 True
规定 0 不是质数,返回 False
1 '''is_prime''' 2 def is_prime(i): 3 result = True 4 for d in range(2,i): 5 if(i%d==0): 6 result = False 7 break 8 if(i==0): 9 result = False 10 return result 11 print(is_prime(1),is_prime(2),is_prime(3),is_prime(4),is_prime(13),is_prime(197),is_prime(201),is_prime(0))
6. 定义 reverse 函数,传一 str 参数,将所有字符串字符倒置并 return 该字符串;如:"abcd" return "dcba"
备注:list.reverse() 及 list[::-1] 可表示 list 倒置
1 '''reverse''' 2 def reverse(s): 3 l = list(s) 4 new_l = [] 5 for i in range(len(l)-1,-1,-1): 6 new_l.append(l[i]) 7 return "".join(new_l) 8 print(reverse("ab cd"),reverse(""))
'''reverse''' def reverse(s): l = list(s) l.reverse() # l = l[::-1] return "".join(l) print(reverse("ab cd"),reverse(""))
7. 定义 anti_vowel 函数,传一 str 参数,将字符串中所有的元音(a e i o u) 去除(注意大小写),并return;
如:"Apple is red." return "ppl is rd."
备注:对于list,for item in list_name 即可遍历list,可以不用下标的方式,参见下方的两种方式(之后的例子也列出了两种方式)
1 '''anti_vowel 1''' 2 def anti_vowel(s): 3 l = list(s) 4 new_l = [] 5 for i in range(len(l)): 6 if (l[i] not in {'a','e','i','o','u','A','E','I','O','U'}): 7 new_l.append(l[i]) 8 return "".join(new_l) 9 print(anti_vowel("Apple is red."))
1 '''anti_vowel 2''' 2 def anti_vowel(s): 3 l = list(s) 4 new_l = [] 5 for letter in l: 6 if (letter not in {'a','e','i','o','u','A','E','I','O','U'}): 7 new_l.append(letter) 8 return "".join(new_l) 9 print(anti_vowel("Apple is red."))
8. 定义 scrabble_score 函数,传一 str 参数。预设一组词典(见下方引用),根据词典中字母对应的数值,将传入参数的每个字母数值相加
(非字母不加,注意大小写转换);如:"Word !" 分别对应取 "w": 4 , "o": 1, "r": 1,"d": 2 , 相加为 4+1+1+2 = 8 return 该值
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10}
1 '''scrabble_score 1''' 2 def scrabble_score(s): 3 score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} 4 l = list(s) 5 sum = 0 6 for i in range(len(l)): 7 letter = l[i].lower() 8 if letter in score.keys(): 9 sum += score[letter] 10 return sum 11 print(scrabble_score("Word !"),scrabble_score("Hello World~!"))
1 '''scrabble_score 2''' 2 def scrabble_score(s): 3 score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} 4 l = list(s) 5 sum = 0 6 for letter in l: 7 letter = letter.lower() 8 if letter in score.keys(): 9 sum += score[letter] 10 return sum 11 print(scrabble_score("Word !"),scrabble_score("Hello World~!"))
9. 定义 censor 函数,传两个 str 参数:s, h;若 s 字符串中的单词包含 h(全小写)时(不区分大小写,暂时仅考虑空格分隔的单词),
将其隐藏,每个隐藏的字母显示为 *;该方法为不完善的隐藏字符方法,如隐藏字符后有标点,则仍会显示。(若需完善,可用正则表达式,后续学习)
如:censor("Fuck U fuck ... ","fuck") 会 return 字符串为 "**** U **** ... " ;
censor("My qq is: 1000100, pls add my QQ","qq") 会 return 字符串为 "My ** is: 1000100, pls add my **"
备注: str.split(" ") 可根据 " " 分隔字符串并返回为list; 字符串相乘,如 "*"*5 可输出 "*****"
1 '''censor 1''' 2 def censor(s,h): 3 l = s.split(" ") 4 new_l = [] 5 for i in range(len(l)): 6 word_l = l[i].lower() 7 if word_l==h: 8 new_l.append("*"*len(h)) 9 else: 10 new_l.append(l[i]) 11 return " ".join(new_l) 12 print(censor("Fuck U fuck ... ","fuck")) 13 print(censor("My qq is: 1000100, pls add my QQ","qq"))
1 '''censor 2''' 2 def censor(s,h): 3 l = s.split(" ") 4 new_l = [] 5 for word in l: 6 if word.lower()==h: 7 new_l.append("*"*len(h)) 8 else: 9 new_l.append(word) 10 return " ".join(new_l) 11 print(censor("Fuck U fuck ... ","fuck")) 12 print(censor("My qq is: 1000100, pls add my QQ","qq"))
10. 定义 count_i 函数,传两个参数 l, item:l 为一 list,item 为任意类型元素;return l 中包含 i 的个数 ;
如: count_i([1,2,3,4,2,1,3,1.0,"1"],1) 会 return 3 (1.0 也可算入;"1" 不可算入)
count_i(["b","a","ab","a","","A"], "a") 会 return 2
count_i([["a"],"a"], ["a"]) 会 return 1; count_i([["a"],"a"], ["a "]) 会 return 0
1 '''count_item 1''' 2 def count_item(l,item): 3 count = 0 4 for i in range(len(l)): 5 if(l[i]==item): 6 count += 1 7 return count 8 print(count_item([1,2,3,4,2,1,3,1.0,"1"],1)) 9 print(count_item(["b","a","ab","a","","A"], "a")) 10 print(count_item([["a"],"a"], ["a"]),count_item([["a"],"a"], ["a "]))
1 '''count_item 2''' 2 def count_item(l,i): 3 count = 0 4 for item in l: 5 if(item==i): 6 count += 1 7 return count 8 print(count_item([1,2,3,4,2,1,3,1.0,"1"],1)) 9 print(count_item(["b","a","ab","a","","A"], "a")) 10 print(count_item([["a"],"a"], ["a"]),count_item([["a"],"a"], ["a "]))
11. 定义 purify 函数,传一 list 参数;去除该 list 中所有的奇数(该list中全为int元素)
如: purify([3,1,5,2,6,1,4]) 会 return [2,6,4]
1 '''purify 1''' 2 def purify(l): 3 l_new = [] 4 for i in range(len(l)): 5 if(l[i]%2==0): 6 l_new.append(l[i]) 7 return l_new 8 print(purify([3,1,5,2,6,1,4]))
1 '''purify 2''' 2 def purify(l): 3 l_new = [] 4 for i in l: 5 if(i%2==0): 6 l_new.append(i) 7 return l_new 8 print(purify([3,1,5,2,6,1,4]))
12. 定义 multiple 函数,传可变参数(该参数全为float),return 所有可变参数相乘的积,无参数则返回 None;
如:multiple(3,2) return 6;multiple(3.5,2) return 7.0;multiple(3.5,2,3) return 21.0;multiple(3,2,0,3,) return 0;
备注:函数调用可变个数的参数,在其参数名前加*,如*args,这些参数将被包装进一个元组
1 '''multiple 1''' 2 def multiple(*f): 3 m = 1 4 if(len(f)==0): 5 return None 6 else: 7 for i in range(len(f)): 8 m *= f[i] 9 return m 10 print(multiple(2,3),multiple(3.5,2),multiple(3.5,2,3),multiple())
1 '''multiple 2''' 2 def multiple(*f): 3 m = 1 4 if(len(f)==0): 5 return None 6 else: 7 for i in f: 8 m *= i 9 return m 10 print(multiple(2,3),multiple(3.5,2),multiple(3.5,2,3),multiple())
13. 定义 remove_duplicate 函数,传一参数 list,删除 list 中重复的项;
如 remove_duplicate([1,2,3,1.0,1,"1"]) return [1,2,3,"1"]
备注:可用转换为集合set 来去重,但无法保留 list 之前的顺序
1 '''remove_duplicate 1''' 2 def remove_duplicate(l): 3 l_new = [] 4 for i in range(len(l)): 5 if l[i] not in l_new: 6 l_new.append(l[i]) 7 return l_new 8 print(remove_duplicate([1,2,3,1.0,1,"1"]))
1 '''remove_duplicate 2''' 2 def remove_duplicate(l): 3 l_new = [] 4 for i in l: 5 if i not in l_new: 6 l_new.append(i) 7 return l_new 8 print(remove_duplicate([1,2,3,1.0,1,"1"]))
1 '''remove_duplicate 3''' 2 def remove_duplicate(l): 3 return list(set(l)) 4 print(remove_duplicate([1,2,3,1.0,1,"1"]))
14. 定义 median 函数,传一参数 list,该 list 中元素均为float或int数值;
若 list 长度为奇数,return 该list 最中间的数;若 list 长度为偶数,return 该 list 最中间两个数的平均数;若 list 为空,return none
如 median([3,4,2]) return 3; median([7,2,3,4]) return 3.5 (3+4/2)
备注:须先对 list 排序,用 sorted(list_name)
1 '''median''' 2 def median(l): 3 l = sorted(l) 4 l_len = len(l) 5 if l_len == 0: 6 return None 7 elif l_len%2 == 1: 8 return l[int((l_len-1)/2)] 9 else: 10 return (l[int((l_len)/2)] + l[int((l_len)/2)-1])/2 11 print(median([]),median([3,4,2]),median([7,2,3,4]))
出处:http://www.cnblogs.com/feeland/
本博客内容大多为作者原创 如果您想转载本博客,请注明出处
如果您对本文有意见或者建议,欢迎留言
感谢您的阅读,请关注我的后续博客
如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐]