anagrams 0.0.1
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/Rakefile +8 -0
- data/lib/anagrams.rb +78 -0
- metadata +62 -0
data/Rakefile
ADDED
data/lib/anagrams.rb
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require 'set'
|
|
2
|
+
require 'color_text'
|
|
3
|
+
|
|
4
|
+
class NotValidDictionaryError < StandardError
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
class InvalidSeedError < StandardError
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Anagrams
|
|
11
|
+
def initialize(dictionary)
|
|
12
|
+
@dictionary = dictionary
|
|
13
|
+
@read_words_from_dictionary = @dictionary.is_a?(Array) ? @dictionary : read_words_from_dictionary
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def find_words(seed)
|
|
17
|
+
if seed.index(/\s/)
|
|
18
|
+
raise InvalidSeedError
|
|
19
|
+
else
|
|
20
|
+
@seed = seed
|
|
21
|
+
split_seed = split_words_to_letters(@seed)
|
|
22
|
+
split_words = split_anagrams_to_letters(@read_words_from_dictionary)
|
|
23
|
+
match_word = find_anagrams(split_seed, split_words)
|
|
24
|
+
result = merge_letters_back_to_words(match_word)
|
|
25
|
+
result = result.delete_if { |seed| seed == @seed }
|
|
26
|
+
result
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def read_words_from_dictionary
|
|
33
|
+
words = []
|
|
34
|
+
File.open(@dictionary, 'r') do |f|
|
|
35
|
+
while line = f.gets
|
|
36
|
+
if !line.chomp.index(/\s/) # Check if the words in dictionary are not phrases
|
|
37
|
+
words << line.chomp
|
|
38
|
+
else
|
|
39
|
+
raise NotValidDictionaryError
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
words
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def split_anagrams_to_letters(anagrams)
|
|
47
|
+
split_words = anagrams.map do |anagram|
|
|
48
|
+
split_words_to_letters(anagram)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def split_words_to_letters(word)
|
|
53
|
+
split_word = []
|
|
54
|
+
word.length.times do |i|
|
|
55
|
+
split_word << word.slice(i)
|
|
56
|
+
end
|
|
57
|
+
split_word
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def find_anagrams(split_seed, split_words)
|
|
61
|
+
find_this = Set.new(split_seed)
|
|
62
|
+
split_words.select do |word|
|
|
63
|
+
word if find_this == Set.new(word)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def merge_letters_back_to_words(match_word)
|
|
68
|
+
back_to_word = ""
|
|
69
|
+
match_word.inject([]) do |result, word|
|
|
70
|
+
word.length.times do |i|
|
|
71
|
+
back_to_word += word[i]
|
|
72
|
+
end
|
|
73
|
+
result << back_to_word
|
|
74
|
+
back_to_word = ""
|
|
75
|
+
result
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: anagrams
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Swathi Kantharaja
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-10-24 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: color_text
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 0.0.3
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 0.0.3
|
|
30
|
+
description: Returns a list of anagrams from a given list of words or from a dictionary
|
|
31
|
+
email:
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files: []
|
|
35
|
+
files:
|
|
36
|
+
- Rakefile
|
|
37
|
+
- lib/anagrams.rb
|
|
38
|
+
homepage: http://www.swathik.com/2012/10/finding-anagrams-in-ruby.html
|
|
39
|
+
licenses: []
|
|
40
|
+
post_install_message:
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
none: false
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ! '>='
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubyforge_project:
|
|
58
|
+
rubygems_version: 1.8.24
|
|
59
|
+
signing_key:
|
|
60
|
+
specification_version: 3
|
|
61
|
+
summary: Find a list of anagrams from the dictionary
|
|
62
|
+
test_files: []
|