習(xí)題41

#!/usr/bin/python
# -*- coding: utf-8 -*-
Import random
from urllib import urlopen
Import sys

for word in urlopen(WORLD_URL).readlines( ):
    WORDS.append( word.strip( ) )

WORD_URL = 'http://learncodethehardway.org/words.txt'
WORDS = []

#do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == 'english':
    PHRASE_FIRST = True
    
# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORD.append(word.strip())
    
def convert(snippet,phrase):
    class_names = [w.capitalize() for w in random.sample(WORDS,snippet.count('%%%'))]
    other_names = random.sample(WORDS,snippet.count('***'))
    results = []
    param_names = []
    
    for i in range(0,snippet.count('@@@')):
        param_count = random.randint(1,3)
        param_names.append(', '.join(random.sample(WORDS,param_count)))
    
    for sentence in snippet,phrase:
        result = sentence[:]
        
        #fake class param_names
        for word in class_names:
            result = result.replace('%%%',word,1)
            
        # fake other class_names
        for word in other_names:
            result = result.replace('***',word,1)
            
        # fake parameter lists
        for word in param_names:
            result = result.replace('@@@',word,1)
            
            results.append(result)
            
    return results
    
# keep going until they hit CTRL-D
try:
    while True:
        snippets = PHRASES.keys()
        random.shuffle(snippets)
        
        for snippet in snippets:
            phrase = PHRASES[snippet]
            question,answer = convert(snippet,phrase)
            if PHRASES_FIRST:
                question,answer = answer,question
                
            print(question)
            
            input('>')
            print('answer:  %s\n\n'%answer)
except EOFError:
    print('\nBye')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【程序41】題目:海灘上有一堆桃子,五只猴子來分。第一只猴子把這堆桃子憑據(jù)分為五份,多了一個(gè),這只猴子把多的一個(gè)扔...
    Xplorist閱讀 261評(píng)論 0 0
  • 習(xí)題 41: 來自 Percal 25 號(hào)行星的哥頓人 (Gothons)在文本編輯器中,編輯以下內(nèi)容并保存到ex...
    周英杰Anita閱讀 1,074評(píng)論 0 0
  • 下面是培臻教育小編為大家整理的一篇關(guān)于LSAT考試邏輯推理練習(xí)題(41)的文章,供大家參考,下面是詳細(xì)內(nèi)容。 83...
    peizhenjy閱讀 642評(píng)論 0 0
  • ①from sys import exit 從sys模組中引入exit即退出功能 退出程序的方式有兩種:os._e...
    d1b0f55d8efb閱讀 1,031評(píng)論 0 0
  • SQL進(jìn)階練習(xí)題41-45 大背景和建表、插入語句就不啰嗦了,參考第一篇。 四張表概要: 學(xué)生表student(s...
    awsome365閱讀 443評(píng)論 0 0

友情鏈接更多精彩內(nèi)容