本篇為追加過濾規則的最後一個關於字數的限制, 本系列之前的筆記參考 :
第十五道過濾的規則是 : "每12個字的排列中,這12字加起來的字母總數介於64~85個字母". 此規則不需要用到正規式, 用 Python 的 len() 函式就能搞定.
第一個測試語料是字母總數少於 64 個字元的情況 (應排除) :
# perm_test_15.py
# 字母總數須介於 64~85 個字母
words=('air', 'root', 'arch', 'try', 'close', 'amber', 'silent', 'beauty', 'trial', 'engage', 'more', 'after')
chars=''.join(words)
print(chars)
print(len(chars))
if len(chars) < 64 or len(chars) > 85: # 須 64~85 個字母才入選
print('the permutation is excluded')
else:
print('the permutation is included')
執行結果為排除 (正確) :
>>> %Run perm_test_15.py
airrootarchtrycloseambersilentbeautytrialengagemoreafter
56
the permutation is excluded
第二個測試語料是字母總數等於 64 個字元的情況 (應入選) :
# perm_test_15.py
# 字母總數須介於 64~85 個字母
words=('air', 'root', 'machine', 'jacket', 'close', 'country', 'silent', 'beauty', 'trial', 'engage', 'more', 'after')
chars=''.join(words)
print(chars)
print(len(chars))
if len(chars) < 64 or len(chars) > 85: # 須 64~85 個字母才入選
print('the permutation is excluded')
else:
print('the permutation is included')
執行結果為入選 (正確) :
>>> %Run perm_test_15.py
airrootmachinejacketclosecountrysilentbeautytrialengagemoreafter
64
the permutation is included
第三個測試語料是字母總數等於 85 個字元的情況 (應入選) :
# perm_test_15.py
# 字母總數須介於 64~85 個字母
words=('abandon', 'negative', 'machine', 'jacket', 'champion', 'country', 'balance', 'beauty', 'obscure', 'engage', 'shoulder', 'identify')
chars=''.join(words)
print(chars)
print(len(chars))
if len(chars) < 64 or len(chars) > 85: # 須 64~85 個字母才入選
print('the permutation is excluded')
else:
print('the permutation is included')
執行結果為入選 (正確) :
>>> %Run perm_test_15.py
abandonnegativemachinejacketchampioncountrybalancebeautyobscureengageshoulderidentify
85
the permutation is included
第四個測試語料是字母總數多於 85 個字元的情況 (應排除) :
# perm_test_15.py
# 字母總數須介於 64~85 個字母
words=('abandon', 'negative', 'machine', 'jacket', 'champion', 'country', 'balance', 'kangaroo', 'obscure', 'engage', 'shoulder', 'identify')
chars=''.join(words)
print(chars)
print(len(chars))
if len(chars) < 64 or len(chars) > 85: # 須 64~85 個字母才入選
print('the permutation is excluded')
else:
print('the permutation is included')
執行結果為排除 (正確) :
>>> %Run perm_test_15.py
abandonnegativemachinejacketchampioncountrybalancekangarooobscureengageshoulderidentify
87
the permutation is excluded
2023-05-19 補充 :
規則改為這 12 個字用空格串起來後長度要在 64~88 個之間, 故上面的測試程式改成如下 :
chars=' '.join(words) # 以空格串接排列中的 12 個字母
print(chars)
print(len(chars))
if len(chars) < 64 or len(chars) > 88: # 含空格長度須 64~88 個字母才入選
print('the permutation is excluded')
else:
print('the permutation is included')
沒有留言 :
張貼留言