Skip to content

Commit a97b5fe

Browse files
authored
Merge pull request #2 from yplog/codex/fix-bug-in-important-codebase-part
Fix duplicate distractor options
2 parents 0ad2828 + b49be2f commit a97b5fe

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

internal/csv/handler.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,29 @@ func createMultipleChoiceQuestionFromAnswers(question, correctAnswer string, all
179179
func getRandomDistractors(correctAnswer string, allAnswers []string, seed int) []string {
180180
r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(seed*13)))
181181

182-
var distractors []string
183182
var otherAnswers []string
183+
unique := make(map[string]struct{})
184184

185185
for _, answer := range allAnswers {
186186
if answer != correctAnswer {
187-
otherAnswers = append(otherAnswers, answer)
187+
if _, exists := unique[answer]; !exists {
188+
unique[answer] = struct{}{}
189+
otherAnswers = append(otherAnswers, answer)
190+
}
188191
}
189192
}
190193

191-
if len(otherAnswers) > 0 {
192-
r.Shuffle(len(otherAnswers), func(i, j int) {
193-
otherAnswers[i], otherAnswers[j] = otherAnswers[j], otherAnswers[i]
194-
})
194+
if len(otherAnswers) == 0 {
195+
return []string{}
196+
}
195197

196-
maxDistractors := len(otherAnswers)
197-
if maxDistractors > 3 {
198-
maxDistractors = 3
199-
}
198+
r.Shuffle(len(otherAnswers), func(i, j int) {
199+
otherAnswers[i], otherAnswers[j] = otherAnswers[j], otherAnswers[i]
200+
})
200201

201-
distractors = otherAnswers[:maxDistractors]
202+
if len(otherAnswers) > 3 {
203+
otherAnswers = otherAnswers[:3]
202204
}
203205

204-
return distractors
206+
return otherAnswers
205207
}

internal/models/quiz.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ func (m Model) HandleMenuUpdate(msg tea.KeyMsg) (Model, tea.Cmd) {
119119
}
120120

121121
func (m Model) HandleQuestionUpdate(msg tea.KeyMsg) (Model, tea.Cmd) {
122+
if len(m.Questions) == 0 {
123+
switch msg.String() {
124+
case "ctrl+c":
125+
return m, tea.Quit
126+
case "q":
127+
m.State = StateMenu
128+
m.Cursor = 0
129+
m.CurrentQ = 0
130+
m.InputText = ""
131+
m.CorrectCount = 0
132+
return m, nil
133+
default:
134+
return m, nil
135+
}
136+
}
137+
122138
switch msg.String() {
123139
case "ctrl+c", "q":
124140
return m, tea.Quit

internal/ui/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func RenderEmptyQuestionsError() string {
1818
s += "Please check that your CSV file is properly formatted:\n"
1919
s += "- Each line should have: question,answer\n"
2020
s += "- Questions and answers should not be empty\n\n"
21-
s += lipgloss.NewStyle().Faint(true).Render("q: Exit to main menu")
21+
s += lipgloss.NewStyle().Faint(true).Render("q: Back to menu")
2222

2323
return s
2424
}

0 commit comments

Comments
 (0)