Sha256: 64f1d3be2e69105e5b14b31f77d252e0768c6846298a2d89cb33aa114246ad92

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

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

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

    def initialize(box:, current_time: Time.current, times_right:, times_wrong:)
      @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

    def fibonacci_number_at(index)
      if (0..1).cover?(index)
        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.3.0 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-1.2.2 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-1.2.1 lib/active_recall/algorithms/fibonacci_sequence.rb
active_recall-1.2.0 lib/active_recall/algorithms/fibonacci_sequence.rb