Sha256: 79ffec906dffc85bc85c2ab40e835c71bd7bed2b19fb00db38210cfdf667bb22

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module ActiveRecall
  class FibonacciSequence
    def self.right(box:, times_right:, times_wrong:, current_time: Time.current)
      new(
        box: box,
        current_time: current_time,
        times_right: times_right,
        times_wrong: times_wrong
      ).right
    end

    def self.wrong(box:, times_right:, times_wrong:, current_time: Time.current)
      new(
        box: box,
        current_time: current_time,
        times_right: times_right,
        times_wrong: times_wrong
      ).wrong
    end

    def initialize(box:, times_right:, times_wrong:, current_time: Time.current)
      @box = box
      @current_time = current_time
      @times_right = times_right
      @times_wrong = times_wrong
    end

    def right
      {
        box: box + 1,
        last_reviewed: current_time,
        next_review: next_review,
        times_right: times_right + 1,
        times_wrong: times_wrong
      }
    end

    def wrong
      {
        box: [0, box - 1].max,
        last_reviewed: current_time,
        next_review: nil,
        times_right: times_right,
        times_wrong: times_wrong + 1
      }
    end

    private

    attr_reader :box, :current_time, :times_right, :times_wrong

    SEQUENCE = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765].freeze

    def fibonacci_number_at(index)
      if (0...SEQUENCE.length).cover?(index)
        SEQUENCE[index]
      else
        fibonacci_number_at(index - 1) + fibonacci_number_at(index - 2)
      end
    end

    def next_review
      current_time + fibonacci_number_at(box + 1).days
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_recall-1.8.3 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-1.8.0 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-1.6.4 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-1.6.3 lib/active_recall/algorithms/fibonacci_sequence.rb