File size: 20,441 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
export interface Topic {
  id: string;
  topic: string;
  domain: string;
  difficulty: "beginner" | "intermediate" | "expert";
  /** IDs of topics that should be demonstrated before attempting this one. */
  prerequisites?: string[];
}

export const FEYNMAN_TOPICS: Topic[] = [
  // Computer Science
  { id: "cs-01", topic: "How a hash table works", domain: "CS", difficulty: "beginner" },
  { id: "cs-02", topic: "Big O notation and time complexity", domain: "CS", difficulty: "beginner" },
  { id: "cs-03", topic: "How recursion works", domain: "CS", difficulty: "beginner" },
  { id: "cs-04", topic: "The difference between a stack and a queue", domain: "CS", difficulty: "beginner" },
  { id: "cs-05", topic: "How public-key cryptography works", domain: "CS", difficulty: "intermediate", prerequisites: ["cs-02"] },
  { id: "cs-06", topic: "How TCP/IP enables reliable data transmission", domain: "CS", difficulty: "intermediate" },
  { id: "cs-07", topic: "How garbage collection works in managed languages", domain: "CS", difficulty: "intermediate", prerequisites: ["cs-04"] },
  { id: "cs-08", topic: "The CAP theorem in distributed systems", domain: "CS", difficulty: "expert", prerequisites: ["cs-06"] },
  { id: "cs-09", topic: "How a B-tree index speeds up database queries", domain: "CS", difficulty: "expert", prerequisites: ["cs-01", "cs-02"] },
  { id: "cs-10", topic: "How a compiler turns source code into machine code", domain: "CS", difficulty: "beginner" },
  { id: "cs-11", topic: "How the MapReduce pattern works", domain: "CS", difficulty: "intermediate", prerequisites: ["cs-02"] },
  { id: "cs-12", topic: "How neural networks learn using backpropagation", domain: "CS", difficulty: "expert", prerequisites: ["cs-02", "ma-01"] },
  { id: "cs-13", topic: "What the halting problem proves about computation limits", domain: "CS", difficulty: "expert", prerequisites: ["cs-03"] },
  { id: "cs-14", topic: "How a relational database uses SQL joins to combine tables", domain: "CS", difficulty: "beginner" },
  { id: "cs-15", topic: "What a REST API is and how stateless HTTP requests work", domain: "CS", difficulty: "beginner" },
  { id: "cs-16", topic: "How version control with Git tracks changes using DAGs", domain: "CS", difficulty: "intermediate", prerequisites: ["cs-03"] },

  // Physics
  { id: "ph-01", topic: "Newton's three laws of motion", domain: "Physics", difficulty: "beginner" },
  { id: "ph-02", topic: "How electromagnetic waves propagate", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-01"] },
  { id: "ph-03", topic: "The photoelectric effect and wave-particle duality", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-02"] },
  { id: "ph-04", topic: "How special relativity affects time and length", domain: "Physics", difficulty: "expert", prerequisites: ["ph-01", "ph-02"] },
  { id: "ph-05", topic: "The laws of thermodynamics", domain: "Physics", difficulty: "beginner" },
  { id: "ph-06", topic: "How nuclear fission produces energy", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-05"] },
  { id: "ph-07", topic: "What gravity actually is in general relativity", domain: "Physics", difficulty: "expert", prerequisites: ["ph-04"] },
  { id: "ph-08", topic: "How a magnet works at the atomic level", domain: "Physics", difficulty: "beginner" },
  { id: "ph-09", topic: "The uncertainty principle and what it actually says", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-03"] },
  { id: "ph-10", topic: "How a laser works: stimulated emission and coherent light", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-02", "ph-03"] },
  { id: "ph-11", topic: "What entropy means and why it always increases", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-05"] },
  { id: "ph-12", topic: "How quantum entanglement works and why it isn't faster-than-light", domain: "Physics", difficulty: "expert", prerequisites: ["ph-09"] },
  { id: "ph-13", topic: "How superconductors achieve zero electrical resistance", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-02"] },
  { id: "ph-14", topic: "What the Higgs boson is and how it gives particles mass", domain: "Physics", difficulty: "intermediate", prerequisites: ["ph-03"] },
  { id: "ph-15", topic: "How black holes form and what happens at the event horizon", domain: "Physics", difficulty: "beginner" },
  { id: "ph-16", topic: "Why quantum mechanics and general relativity are fundamentally incompatible", domain: "Physics", difficulty: "expert", prerequisites: ["ph-07", "ph-09"] },

  // Biology
  { id: "bi-01", topic: "How DNA replication works", domain: "Biology", difficulty: "beginner" },
  { id: "bi-02", topic: "The process of natural selection", domain: "Biology", difficulty: "beginner" },
  { id: "bi-03", topic: "How the immune system responds to a virus", domain: "Biology", difficulty: "intermediate", prerequisites: ["bi-01"] },
  { id: "bi-04", topic: "How CRISPR gene editing works", domain: "Biology", difficulty: "expert", prerequisites: ["bi-01", "bi-03"] },
  { id: "bi-05", topic: "The Krebs cycle and cellular respiration", domain: "Biology", difficulty: "intermediate" },
  { id: "bi-06", topic: "How neurons transmit signals across synapses", domain: "Biology", difficulty: "intermediate" },
  { id: "bi-07", topic: "How mRNA vaccines train the immune system", domain: "Biology", difficulty: "intermediate", prerequisites: ["bi-03"] },
  { id: "bi-08", topic: "The central dogma of molecular biology: DNA to RNA to protein", domain: "Biology", difficulty: "beginner" },
  { id: "bi-09", topic: "How epigenetics allows gene expression to change without DNA changes", domain: "Biology", difficulty: "expert", prerequisites: ["bi-01", "bi-08"] },
  { id: "bi-10", topic: "How photosynthesis converts sunlight into chemical energy", domain: "Biology", difficulty: "beginner" },
  { id: "bi-11", topic: "How evolution produces new species: speciation explained", domain: "Biology", difficulty: "intermediate", prerequisites: ["bi-02"] },
  { id: "bi-12", topic: "The role of mitochondria beyond energy production", domain: "Biology", difficulty: "intermediate", prerequisites: ["bi-05"] },
  { id: "bi-13", topic: "How stem cells differentiate into specialised cell types", domain: "Biology", difficulty: "intermediate", prerequisites: ["bi-01", "bi-08"] },
  { id: "bi-14", topic: "How viruses hijack host cells to replicate", domain: "Biology", difficulty: "beginner" },
  { id: "bi-15", topic: "The microbiome: how gut bacteria influence health and behaviour", domain: "Biology", difficulty: "intermediate", prerequisites: ["bi-03"] },
  { id: "bi-16", topic: "How cancer arises from unchecked cell division and genetic mutation", domain: "Biology", difficulty: "expert", prerequisites: ["bi-01", "bi-09"] },

  // History
  { id: "hi-01", topic: "The causes of the French Revolution", domain: "History", difficulty: "beginner" },
  { id: "hi-02", topic: "How the printing press changed European society", domain: "History", difficulty: "beginner" },
  { id: "hi-03", topic: "The economic factors behind the fall of the Roman Empire", domain: "History", difficulty: "intermediate" },
  { id: "hi-04", topic: "How the Marshall Plan reshaped post-war Europe", domain: "History", difficulty: "intermediate" },
  { id: "hi-05", topic: "The causes and consequences of the Columbian Exchange", domain: "History", difficulty: "expert", prerequisites: ["hi-02"] },
  { id: "hi-06", topic: "How the Industrial Revolution began in Britain", domain: "History", difficulty: "beginner" },
  { id: "hi-07", topic: "Why the Cold War never became a hot war (deterrence theory)", domain: "History", difficulty: "intermediate", prerequisites: ["hi-04"] },
  { id: "hi-08", topic: "How the Silk Road shaped the ancient world", domain: "History", difficulty: "intermediate" },
  { id: "hi-09", topic: "The causes and outcomes of the First World War", domain: "History", difficulty: "beginner" },
  { id: "hi-10", topic: "How the Scientific Revolution transformed European thought", domain: "History", difficulty: "intermediate", prerequisites: ["hi-02"] },
  { id: "hi-11", topic: "How the Atlantic slave trade shaped modern economies and identities", domain: "History", difficulty: "intermediate", prerequisites: ["hi-01"] },
  { id: "hi-12", topic: "Why the Ottoman Empire declined over three centuries", domain: "History", difficulty: "expert", prerequisites: ["hi-03"] },
  { id: "hi-13", topic: "How the Black Death reshaped medieval European society", domain: "History", difficulty: "beginner" },
  { id: "hi-14", topic: "The origins and legacy of the Magna Carta", domain: "History", difficulty: "intermediate", prerequisites: ["hi-01"] },
  { id: "hi-15", topic: "How the Space Race drove technological and geopolitical change", domain: "History", difficulty: "intermediate", prerequisites: ["hi-04"] },
  { id: "hi-16", topic: "The causes and global consequences of the 2008 financial crisis", domain: "History", difficulty: "expert", prerequisites: ["hi-04"] },

  // Economics
  { id: "ec-01", topic: "Supply and demand: how prices are determined", domain: "Economics", difficulty: "beginner" },
  { id: "ec-02", topic: "How inflation works and what causes it", domain: "Economics", difficulty: "beginner" },
  { id: "ec-03", topic: "The concept of comparative advantage in trade", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-01"] },
  { id: "ec-04", topic: "How central banks use interest rates to control the economy", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-01", "ec-02"] },
  { id: "ec-05", topic: "The tragedy of the commons", domain: "Economics", difficulty: "beginner" },
  { id: "ec-06", topic: "How moral hazard affects financial markets", domain: "Economics", difficulty: "expert", prerequisites: ["ec-04"] },
  { id: "ec-07", topic: "What GDP measures and what it misses", domain: "Economics", difficulty: "beginner" },
  { id: "ec-08", topic: "How network effects create winner-take-all markets", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-01"] },
  { id: "ec-09", topic: "The efficient market hypothesis and its critics", domain: "Economics", difficulty: "expert", prerequisites: ["ec-04", "ec-06"] },
  { id: "ec-10", topic: "How game theory explains strategic decision-making", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-01"] },
  { id: "ec-11", topic: "What externalities are and how Pigouvian taxes fix them", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-01", "ec-05"] },
  { id: "ec-12", topic: "How auction theory explains bidding behaviour and optimal pricing", domain: "Economics", difficulty: "expert", prerequisites: ["ec-10"] },
  { id: "ec-13", topic: "What public goods are and why markets consistently under-provide them", domain: "Economics", difficulty: "beginner" },
  { id: "ec-14", topic: "How behavioural economics challenges the rational actor model", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-01"] },
  { id: "ec-15", topic: "How fractional reserve banking and the money multiplier create credit", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-04"] },
  { id: "ec-16", topic: "How index funds changed investing and challenged active management", domain: "Economics", difficulty: "intermediate", prerequisites: ["ec-04", "ec-09"] },

  // Philosophy
  { id: "pl-01", topic: "The trolley problem and utilitarian ethics", domain: "Philosophy", difficulty: "beginner" },
  { id: "pl-02", topic: "Descartes' method of radical doubt", domain: "Philosophy", difficulty: "intermediate", prerequisites: ["pl-03"] },
  { id: "pl-03", topic: "Plato's allegory of the cave", domain: "Philosophy", difficulty: "beginner" },
  { id: "pl-04", topic: "The problem of induction (Hume)", domain: "Philosophy", difficulty: "expert", prerequisites: ["pl-02"] },
  { id: "pl-05", topic: "Kant's categorical imperative", domain: "Philosophy", difficulty: "intermediate", prerequisites: ["pl-01"] },
  { id: "pl-06", topic: "The ship of Theseus and personal identity", domain: "Philosophy", difficulty: "beginner" },
  { id: "pl-07", topic: "What makes an argument valid vs sound", domain: "Philosophy", difficulty: "beginner" },
  { id: "pl-08", topic: "The hard problem of consciousness (Chalmers)", domain: "Philosophy", difficulty: "expert", prerequisites: ["pl-02"] },
  { id: "pl-09", topic: "Free will vs determinism: compatibilism explained", domain: "Philosophy", difficulty: "intermediate", prerequisites: ["pl-01"] },
  { id: "pl-10", topic: "What makes something beautiful: theories of aesthetics", domain: "Philosophy", difficulty: "beginner" },
  { id: "pl-11", topic: "Rawls' veil of ignorance and the theory of justice", domain: "Philosophy", difficulty: "intermediate", prerequisites: ["pl-01"] },
  { id: "pl-12", topic: "The simulation argument: are we living in a computer simulation?", domain: "Philosophy", difficulty: "expert", prerequisites: ["pl-08"] },
  { id: "pl-13", topic: "Aristotle's virtue ethics and the concept of eudaimonia", domain: "Philosophy", difficulty: "beginner" },
  { id: "pl-14", topic: "The social contract: Hobbes, Locke, and Rousseau compared", domain: "Philosophy", difficulty: "intermediate", prerequisites: ["pl-01"] },
  { id: "pl-15", topic: "Epistemological scepticism: what can we know and how?", domain: "Philosophy", difficulty: "intermediate", prerequisites: ["pl-02", "pl-07"] },
  { id: "pl-16", topic: "Nietzsche's will to power and the revaluation of all values", domain: "Philosophy", difficulty: "expert", prerequisites: ["pl-01", "pl-05"] },

  // Psychology
  { id: "ps-01", topic: "Cognitive biases: anchoring and confirmation bias", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-02", topic: "How classical conditioning works (Pavlov)", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-03", topic: "The bystander effect and diffusion of responsibility", domain: "Psychology", difficulty: "intermediate", prerequisites: ["ps-01"] },
  { id: "ps-04", topic: "How working memory differs from long-term memory", domain: "Psychology", difficulty: "intermediate" },
  { id: "ps-05", topic: "The Dunning-Kruger effect", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-06", topic: "Kahneman's System 1 and System 2 thinking", domain: "Psychology", difficulty: "expert", prerequisites: ["ps-01", "ps-04"] },
  { id: "ps-07", topic: "What Milgram's obedience experiments revealed about authority", domain: "Psychology", difficulty: "intermediate", prerequisites: ["ps-02"] },
  { id: "ps-08", topic: "The psychology of intrinsic vs extrinsic motivation", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-09", topic: "How loss aversion shapes economic decisions", domain: "Psychology", difficulty: "intermediate", prerequisites: ["ps-01"] },
  { id: "ps-10", topic: "How sleep affects memory consolidation and learning", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-11", topic: "The psychology of habit formation: cue-routine-reward loops", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-12", topic: "How social identity theory explains group behaviour and prejudice", domain: "Psychology", difficulty: "intermediate", prerequisites: ["ps-03"] },
  { id: "ps-13", topic: "How cognitive dissonance shapes attitudes and motivates rationalisation", domain: "Psychology", difficulty: "beginner" },
  { id: "ps-14", topic: "Cialdini's six principles of persuasion and how they are exploited", domain: "Psychology", difficulty: "intermediate", prerequisites: ["ps-01"] },
  { id: "ps-15", topic: "How childhood attachment styles shape adult relationships and behaviour", domain: "Psychology", difficulty: "intermediate", prerequisites: ["ps-02"] },
  { id: "ps-16", topic: "The neuroscience of addiction: reward circuits, dopamine, and tolerance", domain: "Psychology", difficulty: "expert", prerequisites: ["ps-06"] },

  // Mathematics
  { id: "ma-01", topic: "What a derivative represents in calculus", domain: "Mathematics", difficulty: "beginner" },
  { id: "ma-02", topic: "The fundamental theorem of calculus", domain: "Mathematics", difficulty: "intermediate", prerequisites: ["ma-01"] },
  { id: "ma-03", topic: "How proof by induction works", domain: "Mathematics", difficulty: "intermediate" },
  { id: "ma-04", topic: "Euler's identity and why it connects five fundamental constants", domain: "Mathematics", difficulty: "expert", prerequisites: ["ma-02", "ma-03"] },
  { id: "ma-05", topic: "The Pythagorean theorem and its geometric proof", domain: "Mathematics", difficulty: "beginner" },
  { id: "ma-06", topic: "Bayes' theorem and conditional probability", domain: "Mathematics", difficulty: "intermediate" },
  { id: "ma-07", topic: "What infinity means in mathematics (Cantor's diagonalization)", domain: "Mathematics", difficulty: "expert", prerequisites: ["ma-03"] },
  { id: "ma-08", topic: "How graph theory models networks and connections", domain: "Mathematics", difficulty: "beginner" },
  { id: "ma-09", topic: "The pigeonhole principle and why it is so powerful", domain: "Mathematics", difficulty: "intermediate", prerequisites: ["ma-05"] },
  { id: "ma-10", topic: "What a prime number is and why primes are infinite (Euclid's proof)", domain: "Mathematics", difficulty: "beginner" },
  { id: "ma-11", topic: "How probability and expected value guide rational decisions", domain: "Mathematics", difficulty: "intermediate", prerequisites: ["ma-06"] },
  { id: "ma-12", topic: "What a vector space is and why linear algebra matters", domain: "Mathematics", difficulty: "expert", prerequisites: ["ma-01", "ma-02"] },
  { id: "ma-13", topic: "What a limit is and why calculus depends on it", domain: "Mathematics", difficulty: "beginner" },
  { id: "ma-14", topic: "How complex numbers extend the real number line", domain: "Mathematics", difficulty: "intermediate", prerequisites: ["ma-05"] },
  { id: "ma-15", topic: "How Fourier transforms decompose signals into frequencies", domain: "Mathematics", difficulty: "expert", prerequisites: ["ma-02", "ma-14"] },
  { id: "ma-16", topic: "What topology studies and why a donut equals a coffee cup", domain: "Mathematics", difficulty: "intermediate", prerequisites: ["ma-05"] },
];

export function getRandomTopic(domain?: string): Topic {
  const pool = domain
    ? FEYNMAN_TOPICS.filter((t) => t.domain === domain)
    : FEYNMAN_TOPICS;
  return pool[Math.floor(Math.random() * pool.length)];
}

/**
 * Returns a set of topic choices at mixed difficulty levels.
 * Tries to include one easier topic, one at-level, and one stretch topic.
 * Falls back gracefully when a difficulty tier has no topics in the chosen domain.
 */
export function getTopicChoices(
  domain?: string,
  count: number = 3
): Topic[] {
  const pool = domain
    ? FEYNMAN_TOPICS.filter((t) => t.domain === domain)
    : FEYNMAN_TOPICS;

  if (pool.length === 0) return [];
  if (pool.length <= count) return shuffle(pool);

  const byDifficulty: Record<string, Topic[]> = {
    beginner: [],
    intermediate: [],
    expert: [],
  };

  for (const t of pool) {
    byDifficulty[t.difficulty]?.push(t);
  }

  const picks: Topic[] = [];
  const tiers: Array<"beginner" | "intermediate" | "expert"> = [
    "beginner",
    "intermediate",
    "expert",
  ];

  // Pick one from each tier
  for (const tier of tiers) {
    if (picks.length >= count) break;
    const tierPool = byDifficulty[tier];
    if (tierPool.length > 0) {
      const idx = Math.floor(Math.random() * tierPool.length);
      picks.push(tierPool[idx]);
      tierPool.splice(idx, 1); // Don't double-pick
    }
  }

  // If we still need more (e.g., a tier was empty), fill from remaining pool
  if (picks.length < count) {
    const pickedIds = new Set(picks.map((p) => p.id));
    const remaining = pool.filter((t) => !pickedIds.has(t.id));
    const shuffled = shuffle(remaining);
    for (const t of shuffled) {
      if (picks.length >= count) break;
      picks.push(t);
    }
  }

  return shuffle(picks);
}

function shuffle<T>(arr: T[]): T[] {
  const result = [...arr];
  for (let i = result.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [result[i], result[j]] = [result[j], result[i]];
  }
  return result;
}