Skip to content

Instantly share code, notes, and snippets.

@doctorpangloss
Last active November 23, 2023 19:13
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save doctorpangloss/13ab29abd087dc1927475e560f876797 to your computer and use it in GitHub Desktop.
Save doctorpangloss/13ab29abd087dc1927475e560f876797 to your computer and use it in GitHub Desktop.
Supermemo 2 Algorithm, Unobscured (Python 3)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def supermemo_2(x: [int], a=6.0, b=-0.8, c=0.28, d=0.02, assumed_score=2.5, min_score=1.3, theta=1.0) -> float:
"""
Returns the number of days until seeing a problem again based on the
history of answers x to the problem, where the meaning of x is:
x == 0: Incorrect, Hardest
x == 1: Incorrect, Hard
x == 2: Incorrect, Medium
x == 3: Correct, Medium
x == 4: Correct, Easy
x == 5: Correct, Easiest
@param x The history of answers in the above scoring.
@param theta When larger, the delays for correct answers will increase.
"""
assert all(0 <= x_i <= 5 for x_i in x)
correct = [x_i >= 3 for x_i in x]
# If you got the last question incorrect, just return 1
if not correct[-1]:
return 1.0
# Calculate the latest consecutive answer streak
r = 0
for c_i in reversed(correct):
if c_i:
r+=1
else:
break
return a*(max(min_score, assumed_score + sum(b+c*x_i+d*x_i*x_i for x_i in x)))**(theta*r)
function daysTillNextTestAlgorithm(recent, x, a = 6.0, b = -0.8, c = 0.28, d = 0.02, theta = 0.2) {
  if (recent < 4) {
    return 1
  }
  const history = [recent, ...x]
  // Calculate latest correctness streak
  let streak = 0
  for (let i = 0; i < history.length; i++) {
    if (history[i] > 3) {
      streak++
    } else {
      break
    }
  }
  // Sum up the history
  const historySum = history.reduce(
    (prev, val) => prev + (b + (c * val) + (d * val * val)),
    0
  )
  return a * Math.pow(Math.max(1.3, 2.5 + historySum), theta * streak)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment