Sha256: b1a664e342c1857e1b2d878c137c8fe1391c42ee3819fb49a667ead941875f36

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module ActiveRecall
  class FibonacciSequence
    def self.required_attributes
      REQUIRED_ATTRIBUTES
    end

    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.type
      :binary
    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,
        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

    REQUIRED_ATTRIBUTES = [:box, :times_right, :times_wrong].freeze
    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-2.1.0 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-2.0.2 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-2.0.1 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-2.0.0 lib/active_recall/algorithms/fibonacci_sequence.rb