Skip to content

Instantly share code, notes, and snippets.

@godfat
Created July 31, 2014 07:46
Show Gist options
  • Save godfat/27ee75da3c0c48ed4b1f to your computer and use it in GitHub Desktop.
Save godfat/27ee75da3c0c48ed4b1f to your computer and use it in GitHub Desktop.
Tags = %w[ruby haskell c++ scala java c# js php agda idris julia ocaml]
module BestMatch
module_function
def distance_tags a, b
sum(a & b) / sum(a)
end
def sum tags
tags.inject(0.0){ |r, t| r + tag_score(t) }
end
def tag_score tag
(1.0 / count(tag)) ** 0.5
end
def count tag
Tags.index(tag) + 1
end
end
if $PROGRAM_NAME == __FILE__
require 'pork'
require 'rubyqc'
RubyQC.default_times = 20
Pork.report_at_exit
Pork::API.describe BestMatch do
include BestMatch
extend RubyQC::API
check(1..5, 1..12, 1..12) do |x, y, z|
check(someof(x, Tags), someof(y, Tags), someof(z, Tags)) do |*abc|
would 'distance_tags' do
a, b, c = abc
ab = distance_tags(a, b)
ac = distance_tags(a, c)
abs = (a & b).size
acs = (a & c).size
if abs > acs
ab.should(abc).gte ac
elsif abs < acs
ab.should(abc).lte ac
end
(0..1.0).should.include? ab
(0..1.0).should.include? ac
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment