rubyfish 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -42,4 +42,7 @@ Example Usage
42
42
  ruby-1.9.2-p0 > RubyFish::DamerauLevenshtein.distance("rubyfish", "rubyfihs")
43
43
  => 1
44
44
 
45
-
45
+ ruby-1.9.2-p0 > RubyFish::DoubleMetaphone.phonetic_code "prived"
46
+ => ["PRFT", nil]
47
+ ruby-1.9.2-p0 > RubyFish::DoubleMetaphone.phonetic_code "privet"
48
+ => ["PRFT", nil]
@@ -29,6 +29,53 @@ module RubyFish::LongestSubstring
29
29
  ans
30
30
  end
31
31
 
32
+ def longest_substring a, b
33
+ as = a.to_s
34
+ bs = b.to_s
35
+
36
+ rows = as.size
37
+ cols = bs.size
38
+
39
+ res = ""
40
+ len = 0
41
+ last_sub = 0
42
+
43
+ if rows == 0 || cols == 0
44
+ return res
45
+ end
46
+
47
+ num = ::RubyFish::MMatrix.new rows, cols
48
+
49
+ as.each_char.with_index do |ac, i|
50
+ bs.each_char.with_index do |bc, j|
51
+ unless ac == bc
52
+ num[i, j] = 0
53
+ else
54
+ (i == 0 || j == 0)? num[i, j] = 1 : num[i, j] = 1 + num[i-1, j-1]
55
+ if num[i, j] > len
56
+ len = num[i, j]
57
+ this_sub = i
58
+ this_sub -= num[i-1, j-1] unless num[i-1, j-1].nil?
59
+ if last_sub == this_sub
60
+ res += as[i,1]
61
+ else
62
+ last_sub = this_sub
63
+ res = as[last_sub, (i+1) - last_sub]
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ res
71
+ end
72
+
73
+ def longest_substring_index(a, b)
74
+ a.index(longest_substring(a, b))
75
+ end
76
+
32
77
  module_function :distance
78
+ module_function :longest_substring
79
+ module_function :longest_substring_index
33
80
 
34
81
  end
@@ -1,3 +1,3 @@
1
1
  module RubyFish
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Yury Korolev
@@ -57,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
- hash: 3683456947558493036
60
+ hash: 3404657759252333384
61
61
  segments:
62
62
  - 0
63
63
  version: "0"