import argparse import random import re import sys QUESTION_RE =…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import argparse import random import re import sys
QUESTION_RE = re.compile(r"\s*(\d+).\s*(.+)") ANSWER_RE = re.compile(r"\s*([a-z]).\s*(.+)", re.I) LETTERS = "abcdefghijklmnopqrstuvwxyz"
def parse_questions(handle): """Return a list of dicts {question: str, answers: [str, …]}.""" questions = [] current = None
for raw in handle:
line = raw.rstrip("\n")
# Skip empty lines
if not line.strip():
continue
q_match = QUESTION_RE.match(line)
a_match = ANSWER_RE.match(line)
if q_match: # New question
if current:
questions.append(current)
current = {"question": q_match.group(2).strip(),
"answers": []}
elif a_match and current is not None: # Answer line
current["answers"].append(a_match.group(2).strip())
else: # line-continuation
if current is None:
# Something before the first question – ignore
continue
# Append extra text to the last question line
current["question"] += " " + line.strip()
# Push the last question caught in the loop
if current:
questions.append(current)
return questions
def write_questions(questions, handle): """Write questions back out numbering 1,2,3,... and a.,b.,c.,…""" for idx, q in enumerate(questions, 1): handle.write(f"{idx}. {q['question']}\n") for pos, answer in enumerate(q["answers"]): handle.write(f"{LETTERS[pos]}. {answer}\n") handle.write("\n") # blank line between questions
def main(argv=None): parser = argparse.ArgumentParser( description="Shuffle questions and answers inside a plain-text quiz." ) parser.add_argument("input", help="input .txt file") parser.add_argument("-o", "--output", default="shuffled_quiz.txt", help="output file (default: shuffled_quiz.txt)") parser.add_argument("-s", "--seed", type=int, help="optional random seed for reproducibility") args = parser.parse_args(argv)
if args.seed is not None: random.seed(args.seed)
with open(args.input, encoding="utf-8") as f: questions = parse_questions(f)
if not questions: sys.exit("ERROR: No questions detected – check input format.")
random.shuffle(questions) for q in questions: random.shuffle(q["answers"])
with open(args.output, "w", encoding="utf-8") as f: write_questions(questions, f)
print(f"Done. Shuffled quiz written to {args.output}") if name == "main": main()
Study the above script very carefully and correct indentations in it. Do NOT change anything, just correct the indentations.
Here's the script with corrected indentations:
import argparse
import random
import re
import sys
QUESTION_RE = re.compile(r"\s*(\d+).\s*(.+)")
ANSWER_RE = re.compile(r"\s*([a-z]).\s*(.+)", re.I)
LETTERS = "abcdefghijklmnopqrstuvwxyz"
def parse_questions(handle):
"""Return a list of dicts {question: str, answers: [str, …]}."""
questions = []
current = None
for raw in handle:
line = raw.rstrip("\n")
# Skip empty lines
if not line.strip():
continue
q_match = QUESTION_RE.match(line)
a_match = ANSWER_RE.match(line)
if q_match: # New question
if current:
questions.append(current)
current = {"question": q_match.group(2).strip(),
"answers": []}
elif a_match and current is not None: # Answer line
current["answers"].append(a_match.group(2).strip())
else: # line-continuation
if current is None:
# Something before the first question – ignore
continue
# Append extra text to the last question line
current["question"] += " " + line.strip()
# Push the last question caught in the loop
if current:
questions.append(current)
return questions
def write_questions(questions, handle):
"""Write questions back out numbering 1,2,3,... and a.,b.,c.,…"""
for idx, q in enumerate(questions, 1):
handle.write(f"{idx}. {q['question']}\n")
for pos, answer in enumerate(q["answers"]):
handle.write(f"{LETTERS[pos]}. {answer}\n")
handle.write("\n") # blank line between questions
def main(argv=None):
parser = argparse.ArgumentParser(
description="Shuffle questions and answers inside a plain-text quiz."
)
parser.add_argument("input", help="input .txt file")
parser.add_argument("-o", "--output", default="shuffled_quiz.txt",
help="output file (default: shuffled_quiz.txt)")
parser.add_argument("-s", "--seed", type=int,
help="optional random seed for reproducibility")
args = parser.parse_args(argv)
if args.seed is not None:
random.seed(args.seed)
# --- read
with open(args.input, encoding="utf-8") as f:
questions = parse_questions(f)
if not questions:
sys.exit("ERROR: No questions detected – check input format.")
# --- shuffle
random.shuffle(questions)
for q in questions:
random.shuffle(q["answers"])
# --- write
with open(args.output, "w", encoding="utf-8") as f:
write_questions(questions, f)
print(f"Done. Shuffled quiz written to {args.output}")
if __name__ == "__main__":
main()