-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdid_you_mean.rb
64 lines (52 loc) · 1.66 KB
/
did_you_mean.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
###
# This was a fun Kata that could be useful for real-world projects.
#
# For my solution, I simply shifted/aligned the words to find the optimal (min)
# difference of changes.
#
# @author Jonathan Bradley Whited
# @see https://www.codewars.com/kata/did-you-mean-dot-dot-dot/ruby
# @rank 5 kyu
###
class Dictionary
def initialize(words)
@words = words
end
def find_most_similar(term)
most_similar = @words.first
most_similar_diff = diff(most_similar,term)
for i in 1...@words.length
word = @words[i]
diff = diff(word,term)
if diff < most_similar_diff
most_similar = word
most_similar_diff = diff
end
end
puts "#{term} => #{most_similar}"
most_similar
end
def diff(word,term,index=0)
min,max = (word.length < term.length) ? [word,term] : [term,word]
return max.length if (index + min.length) > max.length
result = max.length - min.length
for i in 0...min.length
result += 1 if min[i] != max[index + i]
end
[result,diff(word,term,index + 1)].min
end
end
languages = Dictionary.new(['javascript','java','ruby','php','python','coffeescript'])
languages.find_most_similar('heaven') # 'java'
languages.find_most_similar('fun') # 'ruby' of course ;)
words=['cherry','peach','pineapple','melon','strawberry','raspberry','apple','coconut','banana']
test_dict=Dictionary.new(words)
test_dict.find_most_similar('strawbery') # 'strawberry'
test_dict.find_most_similar('berry') # 'cherry'
test_dict.find_most_similar('aple') # 'apple'
puts unless ARGV.empty?
ARGV.each do |arg|
languages.find_most_similar(arg)
test_dict.find_most_similar(arg)
end