Created
August 27, 2017 15:57
-
-
Save redtachyons/d836e00d380aa5e645e7949d54fe7565 to your computer and use it in GitHub Desktop.
This file contains 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
require 'rspec' | |
require 'prime' | |
class SocChallenge | |
def self.call(*args) | |
new(*args) | |
end | |
def initialize(number) | |
@number = number | |
end | |
def to_s | |
[smaller_prime, number, larger_prime].join(',') | |
end | |
private | |
def smaller_prime | |
number.pred.downto(1).detect(&:prime?) | |
end | |
def larger_prime | |
(number.next..Float::INFINITY).lazy.detect(&:prime?) | |
end | |
attr_reader :number | |
end | |
RSpec.describe SocChallenge do | |
it 'returns correct value for given inputs' do | |
expect(SocChallenge.call(137).to_s).to eq('131,137,139') | |
expect(SocChallenge.call(256).to_s).to eq('251,256,257') | |
end | |
it 'works for big numbers' do | |
expect(SocChallenge.call(22_801_763_513).to_s).to eq('22801763489,22801763513,22801763527') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment