Class: Algorithmable::Puzzles::JosephusProblem

Inherits:
Object
  • Object
show all
Includes:
DataStructs
Defined in:
lib/algorithmable/puzzles/josephus_problem.rb

Instance Method Summary collapse

Methods included from DataStructs

#new_bag, #new_deque_queue, #new_fifo_queue, #new_lifo_queue, #new_max_priority_queue, #new_min_priority_queue, #new_ordered_symbol_table, #new_priority_queue

Methods included from DataStructs::Tree

#new_ordered_binary_tree

Methods included from DataStructs::LinkedList

#new_doubly_linked_list, #new_singly_linked_list

Constructor Details

#initialize(prisoners, every) ⇒ JosephusProblem


6
7
8
9
# File 'lib/algorithmable/puzzles/josephus_problem.rb', line 6

def initialize(prisoners, every)
  @prisoners = prisoners
  @every = every
end

Instance Method Details

#solveObject


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/algorithmable/puzzles/josephus_problem.rb', line 11

def solve
  queue = new_fifo_queue
  @prisoners.times { |item| queue.enqueue(item) }
  positions = []
  until queue.empty?
    (@every - 1).times do
      queue.enqueue queue.dequeue
    end
    positions << queue.dequeue
  end
  positions
end