This documentation is automatically generated by online-judge-tools/verification-helper

:warning: spec/collection/compress_spec.cr

Depends on

Code

require "spec"
require "../../src/collection/compress"

describe Array do
  it "#compress" do
    a = [0, 3, 7, 0, 5]
    a.compress.should eq [0, 1, 3, 0, 2]
    a.compress(index: 1).should eq [1, 2, 4, 1, 3]
    a.compress([0, 1, 3, 5, 7]).should eq [0, 2, 4, 0, 3]
    a.compress([0, 1, 3, 5, 7], index: 1).should eq [1, 3, 5, 1, 4]
  end
end
require "spec"

# require "../../src/collection/compress"
class Array(T)
  def compress(values : Array(T), *, index : Int = 0)
    map do |x|
      index + values.bsearch_index { |y| y >= x }.not_nil!
    end
  end

  def compress(*, index : Int = 0) : Array(Int32)
    compress(uniq.sort!, index: index)
  end
end

describe Array do
  it "#compress" do
    a = [0, 3, 7, 0, 5]
    a.compress.should eq [0, 1, 3, 0, 2]
    a.compress(index: 1).should eq [1, 2, 4, 1, 3]
    a.compress([0, 1, 3, 5, 7]).should eq [0, 2, 4, 0, 3]
    a.compress([0, 1, 3, 5, 7], index: 1).should eq [1, 3, 5, 1, 4]
  end
end
Back to top page