百度做的网站 后台管理怎么进入,成都公司网站设计,四川省建设工程质量监理协会网站,线上推广引流渠道学习python时记录自己或观察别人从错误到正确的思路远比只看正确答案效果好——傅查理
1.判断单词中是否有字母“e
写一个函数has_no_e,当给定的单词不包含字母‘e时#xff0c;返回True
刚开始我写的是这样的#xff1a;
def has_no_e(word):
for letter in word:
if …学习python时记录自己或观察别人从错误到正确的思路远比只看正确答案效果好——傅查理
1.判断单词中是否有字母“e
写一个函数has_no_e,当给定的单词不包含字母‘e时返回True
刚开始我写的是这样的
def has_no_e(word):
for letter in word:
if lettere:
return False
return True
has_no_e(tdy)
但是总是跟预想的结果不一样单词有‘e时也返回True,所以添加一个print想看看情况
def has_no_e(word):
for letter in word:
if lettere:
print(letter)
return False
return True
has_no_e(hello)
但是也没效果print貌似也没生效这时有点怀疑打印问题了
def has_no_e(word):
for letter in word:
print(letter)
has_no_e(hello)
但是这个打印时没有问题了所以想把判断条件单独拿出来试一下
wordhello
for letter in word:
if lettere:
return False
else:
return True
File , line 4
return False
^
SyntaxError: return outside function
这个报了一个语法错误上网查了下原因是return在函数之外没有任何意义后面想把return改成break但是break在多层循环中只能打断一层无法跳出循环既然拆出来没解决只能从最初的代码找原因后面发现return True在循环里了放到循环外应该就可以了然后试了下
def has_no_e(word):
for letter in word:
if lettereor letterE:
return False
return True
has_no_e(tdy)
返回正常这样就可以了后面有补充了对E的筛查。
2.读入words.txt,打印出不含e的单词并计算这种单词在整个单词表中的百分比
words.txt可在http://thinkpython.com/code/w...然后计算打印单词数量和单词总数再求百分比先计算打印单词数量如下
def has_no_e(word):
for letter in word:
if lettereor letterE:
return False
print(word)
return True
finopen(words.txt)
count0
sum0
for line in fin:
wordline.strip()
has_no_e(word)
count1
sum1
print(count)
单词打印倒是对的但是sum和count是一样的这时我想的是先求一下单词总数是多少看看哪个出错了
finopen(words.txt)
count0
for line in fin:
count1
print(count)
发现结果和上面是一样那么上面得出的是单词总数说明自己对循环不够熟悉以为前面加了个条件就是计算条件的数量了。但是加了个函数不知道怎么统计函数里的单词数量所以想着不用函数如下
finopen(words.txt)
sum0
count0
for line in fin:
for letter in word:
if lettere:
continue
print(word)
count1
sum1
print(sum)
print(count)
但是打印出来全是最后一个字母数字统计的也有问题后面想这个逻辑还是对的把判断加到原来代码的函数上试试
def has_no_e(word):
for letter in word:
if lettereor letterE:
return False
return True
finopen(words.txt)
sum0
count0
for line in fin:
wordline.strip()
if has_no_e(word): #返回True时才走下面的逻辑这样统计的就不是全部的单词了
print(word)
count1
sum1
print(count)
print(sum)
print(count/sum)
这样结果是对的了但是得出的是小数需要把小数转换为百分比
acount/sum
b%.2f%%%(a*100)#将小数转换为百分比且保留两位小数
print(b)
3.编写一个函数is_abecedarian如果单词中的字母是按照字母表顺序排列的两个重复也OK则返回True
看到这个让我想起在前面做过一个字符串比较的题题目中说字符串也是可以通过运算符比较大小的大写字母小于小写字母同类字母按照字母表排序前面的小于后面的然后根据这个写出如下代码
def is_abecedarian(word):
for i in range(0,len(word)-1): #此处注意是len(word)-1,因为下面有word[i1]如果不减一后面会造成下标超出范围的情况
if word[i]word[i1]:
return False
return True
is_abecedarian(Aabbcee)
根据这个函数和前面写过的代码可计算出words.txt单词表中符合此规则的单词总数
finopen(words.txt)
count0
sum0
for line in fin:
wordline.strip()
if is_abecedarian(word):
print(word)
count1
sum1
print(count)
print(sum)
acount/sum
b%.2f%%%(a*100)
print(b)
在113809个单词中符合的有596个占比0.52%。
看了答案后有三种方法如下
#for循环法不过与我的略有不同
def is_abecedarian1(word):
previousword[0]
for c in word:
if c
return False
previousc
return True
#递归方法
def is_abecedarian2(word):
if len(word)1:
return True
if word[0]word[1]:
return False
return is_abecedarian2(word[1:])
is_abecedarian2(aello)
#while循环
def is_abecedarian3(word):
i0
while i
if word[i1]
return False
i1
return True
is_abecedarian3(aello)
调试建议
在所有包含e的单词中你应当测试以e开始的单词也应当测试以其结尾的单词以及其在单词中部的情况。应当测试长单词、短单词及非常短的单词比如空字符串。空字符串是特殊情形的一个例子特殊情况往往不那么明显但又常常隐藏着错误。
注意你可能发现一种类型的错误不应该包含但被却被包含的单词但对另一种情况应当包含但没包含的单词则不能发现。
程序测试可以用来显示bug的存在但无法显示它们的缺席。
今天学到一个新的解决问题的方法问题识别
解决问题的一种方式把问题表述为已经解决的某个问题的特例。
4.练习汽车里程表共6位初始情况后四位是回文行使一公里后后五位是回文再过一公里中间四位是回文再过一公里6位数是回文求初始值通过[::-1]来测试一个单词是不是回文
刚开始按照字符串切片和对题目的条件设置得出如下代码
def is_palindrome(word):
if wordword[::-1]:
return True
return False
def chushizhi():
for mile in range(1000000):
if len(str(mile))!6:
return False
if is_palindrome(str(mile)[2:]) and is_palindrome(str(mile1)[1:]) and is_palindrome(str(mile2)[1:5]) and is_palindrome(str(mile3)):
print(mile)
return False
chushizhi()
这个返回是False,因为知道肯定有结果所以知道这肯定有问题但是这个以后要注意以后如果不知道预期结果那么就检查不出bug来了。第一想法就是先减少判断条件看看情况如下代码
def is_palindrome(word):
if wordword[::-1]:
return True
return False
def chushizhi():
for mile in range(1000000):
if len(str(mile))!6:
return False #因为return后都不执行所以后面的代码由于这个return变得无效了
if is_palindrome(str(mile)[2:]):#减少这里的判断条件
print(mile)
return False
chushizhi()
结果还是和上面一样再仔细看上面判断条件发现如果mile从0到1000000那么前面遇到一个字符长度不等于6时直接返回False,所以是因为前面的判断条件后的return导致后面的代码无法执行导致的所以改成下面的代码
def is_palindrome(word):
if wordword[::-1]:
return True
return False
def chushizhi():
for mile in range(1000000):
if len(str(mile))6 and is_palindrome(str(mile)[2:]) and is_palindrome(str(mile1)[1:]) and is_palindrome(str(mile2)[1:5]) and is_palindrome(str(mile3)):
print(mile)
chushizhi()
返回结果198888和199999测试了下是对的。但是总有个疑问汽车里程刚开始不是从100000开始的而是从000000开始的那么从000000到100000之间是否有符合这个的呢先看了下答案怎么写的
def has_palindrome(i, start, len):
Returns True if the integer i, when written as a string,
contains a palindrome with length (len), starting at index (start).s str(i)[start:startlen]
return s[::-1] s
def check(i):
Checks whether the integer (i) has the properties described
in the puzzler.return (has_palindrome(i, 2, 4) and
has_palindrome(i1, 1, 5) and
has_palindrome(i2, 1, 4) and
has_palindrome(i3, 0, 6))
def check_all():
Enumerates the six-digit numbers and prints any that satisfy the
requirements of the puzzler
i 100000
while i 999996:
if check(i):
print(i)
i i 1
check_all()
结果和我的一样而且从代码看来答案代码也没有考虑十万以前的数字。
我决定自己写一下将上面代码改成下面的样子
def is_palindrome(word):
if wordword[::-1]:
return True
return False
def chushizhi():
for mile in range(1000000):
alen(str(mile))
if a6 and is_palindrome(str(mile).zfill(6)) and is_palindrome(str(mile1).zfill(6)) and is_palindrome(str(mile2).zfill(6)) and is_palindrome(str(mile3).zfill(6)):
print(mile)
elif len(str(mile))6 and is_palindrome(str(mile)[2:]) and is_palindrome(str(mile1)[1:]) and is_palindrome(str(mile2)[1:5]) and is_palindrome(str(mile3)):
print(mile)
chushizhi()
加了一个判断当小于6位数时通过字符串方法zfill在前面用0补齐不过得出的结果还是一样的但是感觉这样会更严谨一些。
5.#练习我和母亲年龄的两位数互为倒数至今已经发生过6次它可能总共发生8次我现在多大
提示可能会发现字符串方法zfill有用
根据这个提示说明我年龄小于10的时候也有互为倒数的情况发生需要做个分析互为倒数用到一个前面写过的函数is_reverse然后判断条件是年龄互为倒数且我的年龄小于母亲的年龄得出如下代码
def is_reverse(word1,word2):
if len(word1)!len(word2):
return False
i0
jlen(word2)-1
while j0:
if word1[i]!word2[j]:
return False
ii1
jj-1
return True
def age_me():
for mo_age in range(10,100):
for age in range(100):
if len(str(age))2 and is_reverse(str(age).zfill(2),str(mo_age)) and age
print(age,mo_age,mo_age-age)
if is_reverse(str(age),str(mo_age)) and age
print(age,mo_age,mo_age-age)
return a
age_me()
得出了很多个符合条件的数字对但是这个无法直观看出哪个符合所以需要计算二者差值差值数量为8的数值对的第六组是现在的年龄
def is_reverse(word1,word2):
if len(word1)!len(word2):
return False
i0
jlen(word2)-1
while j0:
if word1[i]!word2[j]:
return False
ii1
jj-1
return True
def all_list(arr): #统计列表中每个元素出现的次数
result{}
for i in set(arr):
result[i]arr.count(i)
return result
def age_me():
a[]
for mo_age in range(10,100):
for age in range(100):
if len(str(age))2 and is_reverse(str(age).zfill(2),str(mo_age)) and age
print(age,mo_age,mo_age-age)
a.append(mo_age-age)
if is_reverse(str(age),str(mo_age)) and age
print(age,mo_age,mo_age-age)
a.append(mo_age-age)
return all_list(a)
age_me()
然后得出了差值为18时共出现了8次从上开始数第六次是57和75所以我现在年龄时57岁。
下面是答案的代码
def str_fill(i, len):
return the integer (i) written as a string with at least
(len) digits
return str(i).zfill(len)
def are_reversed(i, j):return True if the integers i and j, written as strings,
are the reverse of each other
return str_fill(i,2) str_fill(j,2)[::-1]
def num_instances(diff, flagFalse):
returns the number of times the mother and daughter have
pallindromic ages in their lives, given the difference in age.
If flagTrue, prints the details.
daughter 0
count 0
while True:
mother daughter diff
if are_reversed(daughter, mother) or are_reversed(daughter, mother1):
count count 1
if flag:
print(daughter, mother)
if mother 120:
break
daughter daughter 1
return count
def check_diffs():
enumerate the possible differences in age between mother
and daughter, and for each difference, count the number of times
over their lives they will have ages that are the reverse of
each other.
diff 10
while diff 70:
n num_instances(diff)
if n 0:
print(diff, n)
diff diff 1
print(diff #instances)
check_diffs()
# print
print(daughter mother)
num_instances(18, True)