-
-
Save yasser-sobhy/e1496ecfee8738af9370b2cc6e9182ec to your computer and use it in GitHub Desktop.
Ruby solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_last_index(n, m, x) | |
return nil if x > m # nil if received invalid input | |
last_person_index = -1 | |
while m > 0 do | |
m -= x | |
m > x ? x += 1 : x = m | |
last_person_index == n-1 ? last_person_index = 0 : last_person_index += 1 | |
end | |
return last_person_index | |
end | |
describe "get_last_index " do | |
context "when passed a valid input" do | |
it "should return the expteced result" do | |
r = get_last_index(2, 11, 2) | |
expect(r).to eq 1 | |
end | |
it "should return the expteced result" do | |
r = get_last_index(3, 11, 2) | |
expect(r).to eq 0 | |
end | |
end | |
context "when passed invalid input" do | |
it "should return nil" do | |
r = get_last_index(2, 2, 11) | |
expect(r).to be nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment