anagram-learnruby 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.
- checksums.yaml +7 -0
- data/README +2 -0
- data/anagram.gemspec +17 -0
- data/bin/anagram +7 -0
- data/lib/anagram/finder.rb +26 -0
- data/lib/anagram/options.rb +41 -0
- data/lib/anagram/runner.rb +23 -0
- data/test/test_finder.rb +33 -0
- data/test/test_options.rb +34 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f9f3b06dbafef2eb6d39a36a5bb9ffe589ddd3a9c1e5cd3bf32439def09d7bc7
|
4
|
+
data.tar.gz: 213071a51901d3d0f54861ca18a5a4e4d9af6c9358d8ce47a4fcc9b42bdb3de3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bcbbd945f96091b43412bddcbf8cf47e54df6bb2ae03415f61d157a96de00782c9a5c06510f1357cb2b0d898687fc4dba82b12bf848872ea0251bbfc6a861819
|
7
|
+
data.tar.gz: 52c17c48520b35f9000efa247e19bf81d9828e5ab4c4c0687c6dcfc36f84d280a943a66256a1c0f16e1e8f1c56a176997f687ebb62ebaf37982291be077be07b
|
data/README
ADDED
data/anagram.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "anagram-learnruby"
|
3
|
+
s.summary = "Find anagrams of words supplied on the command line"
|
4
|
+
s.description = File.read(File.join(File.dirname(__FILE__), 'README'))
|
5
|
+
s.requirements = ['An installed dictionary (most Unix systems have one)' ]
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.author = "WongHiuYeung"
|
8
|
+
s.email = "wonghiuyeung@163.com"
|
9
|
+
s.homepage = "http://github.com/wonghiuyeung/anagram.git"
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.required_ruby_version = ">=1.9"
|
12
|
+
s.files = Dir['**/**']
|
13
|
+
s.executables = ['anagram']
|
14
|
+
s.test_files = Dir['test/test*.rb']
|
15
|
+
s.licenses = "MIT"
|
16
|
+
end
|
17
|
+
|
data/bin/anagram
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Anagram
|
2
|
+
class Finder
|
3
|
+
def self.from_file(file_name)
|
4
|
+
new(File.readlines(file_name))
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(dictionary_words)
|
8
|
+
@signatures = {}
|
9
|
+
dictionary_words.each do |line|
|
10
|
+
word = line.chomp
|
11
|
+
signature = Finder.signature_of(word)
|
12
|
+
(@signatures[signature] ||= []) << word
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def lookup(word)
|
17
|
+
signature = Finder.signature_of(word)
|
18
|
+
@signatures[signature]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.signature_of(word)
|
22
|
+
word.unpack("c*").sort.pack("c*")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module Anagram
|
4
|
+
class Options
|
5
|
+
DEFAULT_DICTIONARY = '/usr/share/dict/words'
|
6
|
+
|
7
|
+
attr_reader :dictionary, :words_to_find
|
8
|
+
|
9
|
+
def initialize(argv)
|
10
|
+
@dictionary = DEFAULT_DICTIONARY
|
11
|
+
parse(argv)
|
12
|
+
@words_to_find = argv
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse(argv)
|
18
|
+
OptionParser.new do |o|
|
19
|
+
o.banner = "Usage: anagram [OPTION]... WORD..."
|
20
|
+
|
21
|
+
o.on("-d", "--dict PATH", String, "Path to dictionary") do |d|
|
22
|
+
@dictionary = d
|
23
|
+
end
|
24
|
+
|
25
|
+
o.on("-h", "--help", "Show this message") do
|
26
|
+
puts o
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
argv = ["-h"] if argv.empty?
|
32
|
+
o.parse!(argv)
|
33
|
+
rescue OptionParser::ParseError => e
|
34
|
+
STDERR.puts e.message, "\n", o
|
35
|
+
exit -1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'finder'
|
2
|
+
require_relative 'options'
|
3
|
+
|
4
|
+
module Anagram
|
5
|
+
class Runner
|
6
|
+
def initialize(argv)
|
7
|
+
@options = Options.new(argv)
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
finder = Finder.from_file(@options.dictionary)
|
12
|
+
@options.words_to_find.each do |word|
|
13
|
+
anagrams = finder.lookup(word)
|
14
|
+
if anagrams
|
15
|
+
puts "Anagrams of #{word}: #{anagrams.join(', ')}"
|
16
|
+
else
|
17
|
+
puts "No anagrams of #{word} in #{@options.dictionary}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
data/test/test_finder.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "shoulda"
|
3
|
+
require_relative "../lib/anagram/finder"
|
4
|
+
|
5
|
+
class TestFinder < Test::Unit::TestCase
|
6
|
+
context "signature" do
|
7
|
+
{ "cat" => "act", "act" => "act", "wombat" => "abmotw" }.each do |word, signature|
|
8
|
+
should "be #{signature} for #{word}" do
|
9
|
+
assert_equal signature, Anagram::Finder.signature_of(word)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "lookup" do
|
15
|
+
setup do
|
16
|
+
@finder = Anagram::Finder.new(["cat", "wombat"])
|
17
|
+
end
|
18
|
+
|
19
|
+
should "return word if word given" do
|
20
|
+
assert_equal ["cat"], @finder.lookup("cat")
|
21
|
+
end
|
22
|
+
|
23
|
+
should "return word if anagram given" do
|
24
|
+
assert_equal ["cat"], @finder.lookup("act")
|
25
|
+
assert_equal ["cat"], @finder.lookup("tca")
|
26
|
+
end
|
27
|
+
|
28
|
+
should "return nil if no word matches anagram" do
|
29
|
+
assert_nil @finder.lookup("wibble")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "shoulda"
|
3
|
+
require_relative "../lib/anagram/options"
|
4
|
+
|
5
|
+
class TestOptions < Test::Unit::TestCase
|
6
|
+
context "specifying no dictionary" do
|
7
|
+
should "return default" do
|
8
|
+
opts = Anagram::Options.new(["someword"])
|
9
|
+
assert_equal Anagram::Options::DEFAULT_DICTIONARY, opts.dictionary
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "specifying a dictionary" do
|
14
|
+
should "return it" do
|
15
|
+
opts = Anagram::Options.new(["-d", "mydict", "someword"])
|
16
|
+
assert_equal "mydict", opts.dictionary
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "specifying words and no dictionary" do
|
21
|
+
should "return the words" do
|
22
|
+
opts = Anagram::Options.new(["word1", "word2"])
|
23
|
+
assert_equal ["word1", "word2"], opts.words_to_find
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "specifying words and a dictionary" do
|
28
|
+
should "return the words" do
|
29
|
+
opts = Anagram::Options.new(["-d", "mydict", "word1", "word2"])
|
30
|
+
assert_equal ["word1", "word2"], opts.words_to_find
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anagram-learnruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- WongHiuYeung
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: "### Anagram\n\n"
|
14
|
+
email: wonghiuyeung@163.com
|
15
|
+
executables:
|
16
|
+
- anagram
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README
|
21
|
+
- anagram.gemspec
|
22
|
+
- bin/anagram
|
23
|
+
- lib/anagram/finder.rb
|
24
|
+
- lib/anagram/options.rb
|
25
|
+
- lib/anagram/runner.rb
|
26
|
+
- test/test_finder.rb
|
27
|
+
- test/test_options.rb
|
28
|
+
homepage: http://github.com/wonghiuyeung/anagram.git
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements:
|
47
|
+
- An installed dictionary (most Unix systems have one)
|
48
|
+
rubygems_version: 3.2.3
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: Find anagrams of words supplied on the command line
|
52
|
+
test_files:
|
53
|
+
- test/test_finder.rb
|
54
|
+
- test/test_options.rb
|