Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Last active December 27, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chischaschos/7260060 to your computer and use it in GitHub Desktop.
Save chischaschos/7260060 to your computer and use it in GitHub Desktop.
Exercises
class Solver
def self.solution(x, y, d)
( (y - x) * 1.0 / d).ceil
end
end
describe Solver do
specify do
expect(Solver.solution 10, 85 ,30).to eq 3
end
specify do
expect(Solver.solution 10, 10 ,30).to eq 0
end
end
source 'http://rubygems.org'
gem 'rspec'
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.2.4)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.3)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
PLATFORMS
ruby
DEPENDENCIES
rspec
class Peaks
def initialize mountains
@mountains = mountains
@peaks = []
end
def peaks_number
@mountains.size.times do |index|
@peaks << index if is_peak?(index)
end
@peaks
end
def is_peak?(index)
@mountains[index - 1] < @mountains[index] &&
@mountains[index + 1] < @mountains[index]
end
def max_flags
peaks_number
result = 0
(1..@peaks.size).to_a.each do |max_flag_n|
achieved_flags = @peaks.reduce([@peaks.first]) do |flags_ary, flag|
flag - flags_ary.last >= max_flag_n &&
flags_ary.push(flag) || flags_ary
end
result = max_flag_n if max_flag_n <= achieved_flags.size
end
result
end
end
describe Peaks do
let(:example) { [1,5,3,4,3,4,1,2,3,4,6,2] }
subject do
Peaks.new example
end
it 'should find the max number of flags' do
expect(subject.max_flags).to eq 3
end
specify do
expect(subject.peaks_number).to eq [1, 3, 5, 10]
end
it 'should find the peaks' do
expect(subject.is_peak? 1).to be_true
expect(subject.is_peak? 2).to be_false
end
end
class Tape
def self.solution(a)
p1 = a[0]
p2 = a[1...a.length].reduce(&:+)
(1...a.length).map do |index|
p1 += a[index]
p2 -= a[index]
(p1 - p2).abs
end.sort.first
end
end
describe Tape do
let(:example) { [3,1,2,4,3] }
let(:example2) { [1,1,1,1,1,1] }
let(:example3) do
Array.new(1_000_000).tap do |ary|
ary.map! {|n| rand n }
end
end
specify do
expect(Tape.solution(example)).to eq 1
end
specify do
expect(Tape.solution(example2)).to eq 0
end
specify do
expect(Tape.solution(example3)).to eq 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment