Skip to content

Instantly share code, notes, and snippets.

@shosanna
Created October 17, 2013 17:55
Show Gist options
  • Save shosanna/7029346 to your computer and use it in GitHub Desktop.
Save shosanna/7029346 to your computer and use it in GitHub Desktop.
questions = [
{"text" => "Hlavni mesto San Marina?",
"answers" => ["Torino", "San Marino", "Vatikan", "Terst"],
"correct" => 1},
{"text" => "Kdy byl upalen mistr Jan Hus?",
"answers" => ["6.7.1415", "6.6.1414", "6.8.1415", "5.7.1415"],
"correct" => 0},
{"text" => "Respektovani jineho presvedceni, jinych nazoru je:",
"answers" => ["Ochota", "Tolerance", "Ucta", "Duvera"],
"correct" => 1},
{"text" => "Telefoni cislo na Hasice je?",
"answers" => ["150", "155", "156", "158"],
"correct" => 0},
{"text" => "Ktere z nasledujicich slov patri do spisovne cestiny?",
"answers" => ["sprtat", "vyhecovat", "unyly", "cimra"],
"correct" => 2},
{"text" => "Ktery z nasledujicich jazyku pouziva AZBUKU?",
"answers" => ["Bulharstina", "Litevstina", "Rectina", "Rumunstina"],
"correct" => 0},
{"text" => "Ktere z nasledujicich slov cteme stejne jako piseme?",
"answers" => ["Dialog", "Exhalace", "Pointa", "Atase"],
"correct" => 3},
{"text" => "Jak se jmenoval vlastnim jmenem V.I.Lenin?",
"answers" => ["Putin", "Rasputin", "Uljanov", "Onegin"],
"correct" => 2},
{"text" => "Jak se jmenovala babicka Bozeny Nemcove?",
"answers" => ["Palkova", "Pankova", "Patkova", "Nemcova"],
"correct" => 1}
]
VALUES = ["a", "b", "c", "d"]
def quiz(questions)
questions.inject(0) { |score, question| score + solve_question(question) }
end
# Prints question and options, reads the user input
# and returns if the answer was correct
def solve_question(question)
# print options
puts question["text"] + "\n" + format_answers(question["answers"])
# read answer until valid
return evaluate_answer(question, gets.strip.downcase)
# once answer is valid return it
end
def format_answers(answers)
VALUES.map { |a| "#{a}) " }.zip(answers).map { |pair| pair[0] + pair[1] }.join("\n")
# answers.each_with_index { |a, i| puts "#{VALUES[i]}) #{a}" }
end
def evaluate_answer(question, answer)
correct_answer?(question, answer) ? print_correct : handle_incorrect(question, answer)
end
def correct_answer?(question, answer)
VALUES.index(answer) == question["correct"]
end
def handle_incorrect(question, answer)
VALUES.index(answer) ? print_incorrect(question) : wrong_format(question)
end
def print_correct
puts "Spravne!" or return 1
end
def print_incorrect(question)
puts "Spatne, spravna odpoved je #{question["answers"][question["correct"]]}" or return 0
end
def wrong_format(question)
puts "Spatny format, odpovezte prosim a,b,c nebo d" or return solve_question(question)
end
score = quiz(questions)
# print score for quiz
puts "Ziskali jste #{score} bodu z 9"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment