Last active
November 25, 2025 07:47
-
-
Save tamphh/7917c18c4bfd652fe73191c10eea705a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a simple Ruby script for a vocabulary test in the terminal. | |
| # | |
| # How to use this code: | |
| # Save the file: Save the code above as a Ruby file (e.g., vocab_quiz.rb). | |
| # Open your terminal or command prompt. | |
| # Navigate to the directory where you saved the file. | |
| # Run the script using the command: ruby vocab_quiz.rb | |
| # Follow the prompts in the terminal to enter your answers | |
| # 1. Define the dictionary of words and numbers. | |
| # The keys are the numbers (or definitions in a more complex app), and the values are the words. | |
| DICTIONARY = { | |
| 0 => 'sauce', 1 => 'seed', 2 => 'sun', 3 => 'summo', 4 => 'sierra,Soros', 5 => 'soil', 6 => 'Sanji', 7 => 'sky', 8 => 'sofa', 9 => 'soap', | |
| 10 => 'toes', 11 => 'tit', 12 => 'tin', 13 => 'tomb', 14 => 'T-Rex,tire', 15 => 'tail', 16 => 'dish', 17 => 'tack', 18 => 'dove', 19 => 'tub,tip,top', | |
| 20 => 'nose', 21 => 'net,noodle', 22 => 'nun', 23 => 'Nemo', 24 => 'Nero', 25 => 'nail', 26 => 'Neiji', 27 => 'neck,Nika', 28 => 'knife', 29 => 'NBA,Nippon,honeybee', | |
| 30 => 'mouse', 31 => 'mat', 32 => 'moon', 33 => 'mummy', 34 => 'mower', 35 => 'mill,mail,mall', 36 => 'mage,match', 37 => 'mug', 38 => 'movie,muffin', 39 => 'map', | |
| 40 => 'rice,rose', 41 => 'rat', 42 => 'rain', 43 => 'rum', 44 => 'rower', 45 => 'railway,rail', 46 => 'roach', 47 => 'Rocky,rack', 48 => 'roof', 49 => 'rope,rapper', | |
| 50 => 'lace', 51 => 'latte', 52 => 'lion', 53 => 'lime', 54 => 'lorry', 55 => 'lily', 56 => 'leech', 57 => 'leg', 58 => 'lava,leaf,Luffy', 59 => 'lip', | |
| 60 => 'cheese', 61 => 'cheetah', 62 => 'chin', 63 => 'gem', 64 => 'cherry,jar,shrew', 65 => 'jail,chilly', 66 => 'Cha-Cha', 67 => 'shark', 68 => 'chef', 69 => 'ship,chip', | |
| 70 => 'case', 71 => 'cat', 72 => 'coin', 73=> 'comb', 74=> 'car', 75 => 'koala,coal', 76 => 'geisha,coach', 77 => 'cake,KaKa', 78 => 'cave,cafe', 79 => 'cop', | |
| 80 => 'vase', 81 => 'video,Voodoo', 82 => 'fan', 83 => 'farm', 84 => 'fairy,fire', 85 => 'fall', 86 => 'fish', 87 => 'fig,Viking', 88 => 'fife,Fifa', 89 => 'FBI,vape,VIP', | |
| 90 => 'boss', 91 => 'bat', 92 => 'bunny,pony', 93 => 'bum', 94 => 'bear', 95 => 'bell', 96 => 'beach,Batch', 97 => 'book', 98 => 'beef', 99 => 'pipe,baby,Pepsi' | |
| }.freeze # Using .freeze makes the hash immutable | |
| # 2. Main function to run the quiz. | |
| def run_quiz | |
| puts "Welcome to the English Vocabulary Major System Test!" | |
| puts "You will be given a number, and you need to type the corresponding word." | |
| puts "Type 'exit' to quit the test at any time." | |
| puts "--------------------------------------------------" | |
| # Shuffle | |
| shuffled_keys = DICTIONARY.keys.shuffle | |
| shuffled_dictionary = {} | |
| shuffled_keys.each do |key| | |
| shuffled_dictionary[key] = DICTIONARY[key] | |
| end | |
| # Get start time | |
| start_time = Time.now | |
| # test only | |
| # shuffled_dictionary = shuffled_dictionary.to_a.first(3).to_h | |
| score = 0 | |
| total_questions = shuffled_dictionary.length | |
| wrong_words = [] | |
| # Iterate through each number and word pair in the dictionary | |
| shuffled_dictionary.each_pair.each_with_index do |(number, correct_words), index| | |
| puts "\nWhat is the word for number #{number}?" | |
| print "Your answer: " | |
| # Get user input and remove any leading/trailing whitespace | |
| # user_answer = gets.chomp.downcase | |
| user_answer = gets.chomp | |
| # Check if the user wants to exit | |
| if user_answer == 'exit' | |
| puts "\nQuiz exited." | |
| return | |
| end | |
| # Check the answer | |
| nth_word_msg = "Word #{index + 1} of #{shuffled_dictionary.length}" | |
| if correct_words.split(',').map(&:strip).include?(user_answer) | |
| puts "#{nth_word_msg}. Correct! 🎉" | |
| score += 1 | |
| else | |
| wrong_words << number | |
| puts "#{nth_word_msg}. Wrong. The correct word was '#{correct_words}'. ❌" | |
| end | |
| end | |
| # 3. Display final results | |
| puts "\n--------------------------------------------------" | |
| puts "Quiz finished in #{((Time.now - start_time) / 60.to_f).ceil} minute(s)!" | |
| puts "Wrong words: #{wrong_words.length > 0 ? wrong_words : 0}." | |
| puts "Your final score is #{score} out of #{total_questions}." | |
| puts "--------------------------------------------------" | |
| end | |
| system('clear') | |
| # Run the quiz | |
| run_quiz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment