一千萬個為什麽

搜索

在Emacs Lisp中生成測驗?

原諒“重復”的問題。我也想在Emacs Lisp中看到這個問題,如果我只為兩個主題標記它,我可能只會得到一個答案。 Emacs的答案應該是完全不同的,因此可能值得擁有它。

我想教自己西班牙語,我有幾個單詞列表,如 數據如下所示。如何根據數據生成測驗 看起來像這樣?

amarillo?  [ ] blue    [ ] yellow  [ ] gray   [ ] pink
azul?      [ ] red     [ ] blue    [ ] green  [ ] orange
 .
 .
 .
verde?     [  ] purple [ ] gold   [ ] green   [ ] black

這個想法是隨機選擇3個答案 錯誤答案。理想情況下,不正確的答案也不會 重復。

amarillo|yellow
azul|blue
blanco|white
dorado|golden
gris|gray
marrón|brown
naranja|orange
negro|black
oro|gold
púrpura|purple
rojo|red
rosa|pink
verde|green

最佳答案

好的,所以我假設您在Emacs緩沖區中打開的文件中有輸入。

(defun insert-quiz (a-buffer)
  (interactive "bBuffer name: ")
  (let* ((question-pairs (split-string (with-current-buffer a-buffer (buffer-string))))
         (quiz-answers (mapcar (lambda (x) (cadr (split-string x "|"))) question-pairs)))
    (insert 
     (apply #'concat 
            (mapcar
             (lambda (x)
               (let ((q-pair (split-string x "|")))
                 (make-question (car q-pair) (answers-list quiz-answers (cadr q-pair)))))
             question-pairs)))))

insert-quiz是一個交互式函數,它接受緩沖區名稱,並使用該緩沖區中的東西為您生成測驗,然後在該點插入該測驗作為副作用。它調用了一些較小的函數,我將在下面解釋。

(defun make-question (question answers)
  (apply #'format
         "%-16s[ ] %-16s[ ] %-16s[ ] %-16s[ ] %s \n"
         (append 
          (list (concat question "?"))
          answers)))

make-question提出問題和答案列表,並將它們格式化為測驗的一行。

(defun answers-list (quiz-answers right-answer)
  (replace (n-wrong-answers quiz-answers right-answer)
           (list right-answer)
           :start1 (random 3)))

answers-list獲取測驗中所有可能答案的列表,並使用正確的答案並使用n-wrong-answers創建四個答案的列表,其中一個是正確答案。

(defun n-wrong-answers (answer-list right-answer &optional answers)
    (if (= 4 (list-length answers))
        answers
      (n-wrong-answers 
       answer-list 
       right-answer 
       (add-to-list 'answers (random-wrong-answer answer-list right-answer)))))

n-wrong-answers在測驗和正確答案中列出所有可能的答案,然後使用random-wrong-answer返回四個唯一錯誤答案的列表。

(defun random-wrong-answer (answer-list right-answer)
  (let ((gen-answer (nth (random (list-length answer-list)) answer-list)))
    (if (and gen-answer (not (string= gen-answer right-answer)))
        gen-answer
        (random-wrong-answer answer-list right-answer))))

最後,在最低級別,隨機錯誤答案會在測驗中列出所有可能的答案,並返回一個錯誤的答案。

將上述函數加載到Emacs後,使用M-x insert-quiz並鍵入輸入加載的緩沖區的名稱(您將獲得制表符完成)。更改insert-quiz函數以便它采用文件名而不是打開的緩沖區名稱並不太難。

您在上面列出的輸入將產生:

amarillo?               [ ] yellow          [ ] orange          [ ] gray            [ ] red 
azul?                   [ ] gold            [ ] purple          [ ] blue            [ ] orange 
blanco?                 [ ] pink            [ ] red             [ ] white           [ ] black 
dorado?                 [ ] yellow          [ ] golden          [ ] red             [ ] orange 
gris?                   [ ] red             [ ] pink            [ ] gray            [ ] green 
marrón?                 [ ] brown           [ ] yellow          [ ] white           [ ] golden 
naranja?                [ ] orange          [ ] gold            [ ] black           [ ] golden 
negro?                  [ ] pink            [ ] black           [ ] blue            [ ] white 
oro?                    [ ] red             [ ] gold            [ ] purple          [ ] brown 
púrpura?                [ ] purple          [ ] orange          [ ] gray            [ ] black 
rojo?                   [ ] gray            [ ] red             [ ] black           [ ] pink 
rosa?                   [ ] red             [ ] green           [ ] pink            [ ] yellow 
verde?                  [ ] green           [ ] purple          [ ] red             [ ] brown 

希望有所幫助。

轉載註明原文: 在Emacs Lisp中生成測驗?