觉得自己虽然见过递归,但几乎不用,不逼着我我都不用,循环用得越来越遛。前两题我和参考答案得出的结论一致,最后一题,我觉得参考答案有问题。下面的都是我的脚本。下面要用到的words.txt在这里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | def double_letter(word): num = 0 i = 0 if len(word) >= 6: while i < len(word)-1: if word[i] == word[i+1]: num = num + 1 i = i + 2 elif i > 2 and word[i-2] != word[i-3]: break else: i = i + 1 if num == 3: print(word) fin = open('words.txt') n = 0 for line in fin: word = line.strip() double_letter(word) # bookkeeper # bookkeepers # bookkeeping # bookkeepings |
def double_letter(word): num = 0 i = 0 if len(word) >= 6: while i < len(word)-1: if word[i] == word[i+1]: num = num + 1 i = i + 2 elif i > 2 and word[i-2] != word[i-3]: break else: i = i + 1 if num == 3: print(word) fin = open('words.txt') n = 0 for line in fin: word = line.strip() double_letter(word) # bookkeeper # bookkeepers # bookkeeping # bookkeepings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def is_palindrome(word): if word[::-1] == word: return True def test_palindrome(number): if is_palindrome(str(number)[2:]): if is_palindrome(str(number+1)[1:]): if is_palindrome(str(number+2)[1:1]): if is_palindrome(str(number+3)): return True for number in range(100000, 999999): if test_palindrome(number): print(number) # 198888 # 199999 |
def is_palindrome(word): if word[::-1] == word: return True def test_palindrome(number): if is_palindrome(str(number)[2:]): if is_palindrome(str(number+1)[1:]): if is_palindrome(str(number+2)[1:1]): if is_palindrome(str(number+3)): return True for number in range(100000, 999999): if test_palindrome(number): print(number) # 198888 # 199999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | year = 99 meet = int(input('how many times have we met?(1-8): ')) print('mom born me at', '\t','my age', '\t',"mon's age") for i in range(10, 80): # 假设你妈生你的最低年龄是10,最高年龄是80 n = 0 for age in range(1, year): if age < int(str(age).zfill(2)[::-1]) and int(str(age).zfill(2)[::-1]) - age == i: # print(i, '\t\t', age, '\t\t', str(age).zfill(2)[::-1]) n = n + 1 if n == meet: print(i, '\t\t', age, '\t\t', str(age).zfill(2)[::-1]) # how many times have we met?(1-8): 6 # mom born me at my age mon's age # 18 57 75 # 27 58 85 # 36 59 95 # how many times have we met?(1-8): 8 # mom born me at my age mon's age # 18 79 97 # mom born me at my age mon's age # 18 2 20 # 18 13 31 # 18 24 42 # 18 35 53 # 18 46 64 # 18 57 75 # 18 68 86 # 18 79 97 # 27 3 30 # 27 14 41 # 27 25 52 # 27 36 63 # 27 47 74 # 27 58 85 # 27 69 96 # 36 4 40 # 36 15 51 # 36 26 62 # 36 37 73 # 36 48 84 # 36 59 95 # 45 5 50 # 45 16 61 # 45 27 72 # 45 38 83 # 45 49 94 # 54 6 60 # 54 17 71 # 54 28 82 # 54 39 93 # 63 7 70 # 63 18 81 # 63 29 92 # 72 8 80 # 72 19 91 |
year = 99 meet = int(input('how many times have we met?(1-8): ')) print('mom born me at', '\t','my age', '\t',"mon's age") for i in range(10, 80): # 假设你妈生你的最低年龄是10,最高年龄是80 n = 0 for age in range(1, year): if age < int(str(age).zfill(2)[::-1]) and int(str(age).zfill(2)[::-1]) - age == i: # print(i, '\t\t', age, '\t\t', str(age).zfill(2)[::-1]) n = n + 1 if n == meet: print(i, '\t\t', age, '\t\t', str(age).zfill(2)[::-1]) # how many times have we met?(1-8): 6 # mom born me at my age mon's age # 18 57 75 # 27 58 85 # 36 59 95 # how many times have we met?(1-8): 8 # mom born me at my age mon's age # 18 79 97 # mom born me at my age mon's age # 18 2 20 # 18 13 31 # 18 24 42 # 18 35 53 # 18 46 64 # 18 57 75 # 18 68 86 # 18 79 97 # 27 3 30 # 27 14 41 # 27 25 52 # 27 36 63 # 27 47 74 # 27 58 85 # 27 69 96 # 36 4 40 # 36 15 51 # 36 26 62 # 36 37 73 # 36 48 84 # 36 59 95 # 45 5 50 # 45 16 61 # 45 27 72 # 45 38 83 # 45 49 94 # 54 6 60 # 54 17 71 # 54 28 82 # 54 39 93 # 63 7 70 # 63 18 81 # 63 29 92 # 72 8 80 # 72 19 91
本文链接:https://my.lmcjl.com/post/9161.html
4 评论