Skip to content

Instantly share code, notes, and snippets.

@lucasrcezimbra
Created September 27, 2016 18:47
Show Gist options
  • Save lucasrcezimbra/00d242fa35e8725c9219f4018b4771af to your computer and use it in GitHub Desktop.
Save lucasrcezimbra/00d242fa35e8725c9219f4018b4771af to your computer and use it in GitHub Desktop.
Josephus in Ruby - tests with RSpec
source 'https://rubygems.org'
ruby '1.9.3'
# RSpec
gem 'rspec-rails'
class Main
def calculo(n, xs)
result_final = []
xs.each do |x|
base_x4 = (x / 4).floor
result_final << (x + base_x4 + (((( x + base_x4 ) / 4 ).floor ) - base_x4))
end
return result_final
end
def calculo2(n, xs)
result_final = []
n_resto_4 = n % 4
ultimo = 4 - n_resto_4
pulos = ((5 - n_resto_4)/4).floor
xs.each do |x|
base_x4 = (x / 4).floor
result1 = x + base_x4 + (((( x + base_x4 ) / 4 ).floor ) - base_x4)
#recomecar = n * ((x/4).ceil - (x/4).floor)
result_final << (result1 + ultimo + pulos) % n
end
return result_final
end
end
require_relative 'main'
RSpec.describe Main do
it { expect( Main.new.calculo2(5, [1,2,3]) ).to eq( [5,1,2] ) }
it { expect( Main.new.calculo2(6, [1,2,3]) ).to eq( [3,5,6] ) }
it { expect( Main.new.calculo2(7, [5,1,2]) ).to eq( [7,2,3] ) }
it { expect( Main.new.calculo(8, [3,5,6]) ).to eq( [3,6,7] ) }
it { expect( Main.new.calculo2(9, [3,5,6]) ).to eq( [7,1,2] ) }
it { expect( Main.new.calculo2(10, [7,2,3]) ).to eq( [1,5,6] ) }
it { expect( Main.new.calculo2(11, [3,6,7]) ).to eq( [5,9,10] ) }
it { expect( Main.new.calculo(12, [7,1,2]) ).to eq( [9,1,2] ) }
it { expect( Main.new.calculo2(13, [7,1,2]) ).to eq( [13,5,6] ) }
it { expect( Main.new.calculo2(14, [1,5,6]) ).to eq( [3,9,10] ) }
it { expect( Main.new.calculo2(15, [5,9,10]) ).to eq( [7,13,14] ) }
it { expect( Main.new.calculo(16, [9,1,2]) ).to eq( [11,1,2] ) }
it { expect( Main.new.calculo2(17, [9,1,2]) ).to eq( [15,5,6] ) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment