女兒在做英語練習題, 有一種型別叫做字母組合, 就是將打亂順序的字母組合成學過的單詞, 女兒常常想不出來, 我也經常搔頭, 順序是亂的, 查字典也不好使.
這個難不住程式員, 打開電腦寫了十幾行 python 程式, 這種問題以后就是小菜一碟了
- 首先下載一個英語字典的單詞文本檔案
wget https://github.com/dwyl/english-words/blob/master/words_alpha.txt
格式如下
$ head words_alpha.txt
a
aa
aaa
aah
aahed
aahing
aahs
aal
aalii
aaliis
...
- 用 python 寫個小程式
程式如下, 加上空行, 總共18行, 輕松搞定
# vi words_compose.py
import sys
from itertools import permutations
words = []
with open('./words_alpha.txt') as file:
for line in file:
line = line.strip()
words.add(line)
inputstr = 'hoiystr'
if(len(sys.argv)>1):
inputstr = sys.argv[1].lower()
perms = permutations(inputstr)
for p in perms:
word = ''.join(p)
if word in words:
print(word)
- 使用方法
輸入引數為亂序的字母串
$ python words_compose.py ipturec
picture
cuprite
$ python words_compose.py oihystr
history
toryish
女兒很滿意, 我也乘機自吹了一番, 勸說女兒也學一點 python 編程,最后,給大家推薦一個資源很全的python學習免非解答.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,這里有資深程式員分享以前學習心得,學習筆記,還有一線企業的作業經驗,且給大家精心整理一份python零基礎到專案實戰的資料,每天給大家講解python最新的技術,前景,學習需要留言的小細節
本文的文字及圖片來源于網路加上自己的想法,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/165925.html
標籤:Python
