day9
看到答案和我的代码以后,相形惭愧啊
1 def is_magic_word(s): 2 i = 0 3 index = 0 4 while i < len(s) - 1: 5 if s[i] == s[i+1]: 6 index = i 7 break 8 i += 1 9 if (index+5) < len(s): 10 if s[index+2] != s[index+3] or s[index+4] != s[index+5]: 11 return False 12 else: 13 return True 14 else: 15 return False 16 17 file = open("words.txt") 18 19 for line in file: 20 word = line.strip() 21 if is_magic_word(word): 22 print(word)
1 """This module contains a code example related to 2 3 Think Python, 2nd Edition 4 by Allen Downey 5 http://thinkpython2.com 6 7 Copyright 2015 Allen Downey 8 9 License: http://creativecommons.org/licenses/by/4.0/ 10 """ 11 12 from __future__ import print_function, division 13 14 15 def is_triple_double(word): 16 """Tests if a word contains three consecutive double letters. 17 18 word: string 19 20 returns: bool 21 """ 22 i = 0 23 count = 0 24 while i < len(word)-1: 25 if word[i] == word[i+1]: 26 count = count + 1 27 if count == 3: 28 return True 29 i = i + 2 30 else: 31 count = 0 32 i = i + 1 33 return False 34 35 36 def find_triple_double(): 37 """Reads a word list and prints words with triple double letters.""" 38 fin = open('words.txt') 39 for line in fin: 40 word = line.strip() 41 if is_triple_double(word): 42 print(word) 43 44 45 print('Here are all the words in the list that have') 46 print('three consecutive double letters.') 47 find_triple_double() 48 print('')
再扔一段我的丑陋代码
1 def is_huiwen(num): 2 s = str(num) 3 if len(s) != 6: 4 print('wrong') 5 else: 6 if s[2] == s[5] and s[3] == s[4]: 7 return True 8 if s[1] == s[5] and s[2] == s[4]: 9 return True 10 if s[0] == s[5] and s[1] == s[4] and s[2] == s[3]: 11 return True 12 return False 13 count = 0 14 for i in range(100000,1000000): 15 if is_huiwen(i): 16 count += 1 17 print(i) 18 print(count)
感觉被习题的最后一题完全带跑偏了...
1 def get_mom_age(age_son): 2 s = str(age_son) 3 s1 = s[::-1] 4 age_mom = int(s1) 5 return age_mom 6 7 # print(get_mom_age(67)) 8 9 def is_fun(age_son, age_mom): 10 if age_mom == get_mom_age(age_son): 11 return True 12 else: 13 return False 14 15 def is_fun_num(age_son): 16 count = 0 17 age_mom = get_mom_age(age_son) 18 19 new_age_son = age_son 20 new_age_mom = age_mom 21 22 while new_age_son >= 1: 23 new_age_son = new_age_son - 1 24 new_age_mom = new_age_mom - 1 25 if is_fun(new_age_son, new_age_mom): 26 # print(new_age_son, new_age_mom) 27 count += 1 28 else: 29 pass 30 31 if count == 6: 32 new_age_mom = age_mom 33 new_age_son = age_son 34 35 while new_age_son < 100: 36 new_age_son = new_age_son + 1 37 new_age_mom = new_age_mom + 1 38 if is_fun(new_age_son, new_age_mom): 39 # print(new_age_son, new_age_mom) 40 count += 1 41 else: 42 pass 43 44 if count == 8: 45 return True 46 else: 47 return False 48 else: 49 return False 50 51 for i in range(10,100): 52 s = str(i) 53 if s[0] > s[1]: 54 # print('no way', i) 55 pass 56 else: 57 if is_fun_num(i): 58 print(i)
今天看到按值传递和按引用传递了,传一个列表给函数是传一个引用的,这样意味在函数内对列表的修改都会在函数外部有效。
我们正常传一个数值类的值或字符串的时候,都不用考虑这个问题,都可以当成是函数怎么对参数修改,都不会影响函数外这个参数的值。就相当与按值传递,也就是说复制了一份值。
有些太晚了,不要去太深入思考这个问题了,容易睡不着觉,今天还是浪费了比较多的时间,后续要提高提高效率。
Think!

浙公网安备 33010602011771号