anagram-mine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a54db9b3b447b327a5606610e8f2f4cdbb5d5ac
4
+ data.tar.gz: b99eb8ecaf8590245456e5a21fc6e6306c09621c
5
+ SHA512:
6
+ metadata.gz: e143ff2bafffbe6ac70bb4269d4640cb284be7dba044ee8d1994303bfb7ca6acf02e3877b3b399d8e561f4c4cf44d1b16d672de98baaa88c8cefd781fd021e9f
7
+ data.tar.gz: 7662111a92381723f927b29ad86164480f741032779fae33cc2d0e36aa6585787e6cd0ef7d1e48778991a248ac2c8f298db973033989409d8f5722bb7e5077cf
data/INSTALL ADDED
File without changes
data/LICENCE ADDED
File without changes
data/README ADDED
File without changes
Binary file
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "anagram-mine"
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 =
6
+ [ 'An installed dictinoary (most Unix systems have one)' ]
7
+ s.version = "0.0.1"
8
+ s.author = "Dave Thomas"
9
+ s.email = "dave@pragprog.com"
10
+ s.homepage = "http://pragdave.pragprog.com"
11
+ s.platform = Gem::Platform::RUBY
12
+ s.required_ruby_version = '>=1.9'
13
+ s.files = Dir['**/**']
14
+ s.executables = [ 'anagram' ]
15
+ s.test_files = Dir["test/test*.rb"]
16
+ s.has_rdoc = false
17
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby
2
+ require 'anagram/runner'
3
+
4
+ runner = Anagram::Runner.new(ARGV)
5
+ runner.run
Binary file
Binary file
@@ -0,0 +1,26 @@
1
+ module Anagram
2
+ class Finder
3
+
4
+ def self.from_file(file_name)
5
+ new(File.readlines(file_name))
6
+ end
7
+
8
+ def initialize(dictionary_words)
9
+ @signatures = Hash.new
10
+ dictionary_words.each do |line|
11
+ word = line.chomp
12
+ signature = Finder.signature_of(word)
13
+ (@signatures[signature] ||= []) << word
14
+ end
15
+ end
16
+
17
+ def lookup(word)
18
+ signature = Finder.signature_of(word)
19
+ @signatures[signature]
20
+ end
21
+
22
+ def self.signature_of(word)
23
+ word.unpack("c*").sort.pack("c*")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'optparse'
2
+
3
+ module Anagram
4
+ class Options
5
+
6
+ DEFAULT_DICTIONARY = "/usr/share/dict/words"
7
+
8
+ attr_reader :dictionary
9
+ attr_reader :words_to_find
10
+
11
+ def initialize(argv)
12
+ @dictionary = DEFAULT_DICTIONARY
13
+ parse(argv)
14
+ @words_to_find = argv
15
+ end
16
+
17
+ private
18
+
19
+ def parse(argv)
20
+ OptionParser.new do |opts|
21
+ opts.banner = "Usage: anagram [ options ] word..."
22
+
23
+ opts.on("-d", "--dict path", String, "Path to dictionary") do |dict|
24
+ @dictionary = dict
25
+ end
26
+
27
+ opts.on("-h", "--help", "Show this message") do
28
+ puts opts
29
+ exit
30
+ end
31
+
32
+ begin
33
+ argv = ["-h"] if argv.empty?
34
+ opts.parse!(argv)
35
+ rescue OptionParser::ParseError => e
36
+ STDERR.puts e.message, "\n", opts
37
+ exit(-1)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'finder'
2
+ require_relative 'options'
3
+
4
+ module Anagram
5
+ class Runner
6
+
7
+ def initialize(argv)
8
+ @options = Options.new(argv)
9
+ end
10
+
11
+ def run
12
+ finder = Finder.from_file(@options.dictionary)
13
+ @options.words_to_find.each do |word|
14
+ anagrams = finder.lookup(word)
15
+ if anagrams
16
+ puts "Anagrams of #{word}: #{anagrams.join(', ')}"
17
+ else
18
+ puts "No anagrams of #{word} in #{@options.dictionary}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,35 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require_relative '../lib/anagram/finder'
4
+
5
+ class TestFinder < Test::Unit::TestCase
6
+
7
+ context "signature" do
8
+ { "cat" => "act", "act" => "act", "wombat" => "abmotw" }.each do
9
+ |word, signature|
10
+ should "be #{signature} for #{word}" do
11
+ assert_equal signature, Anagram::Finder.signature_of(word)
12
+ end
13
+ end
14
+ end
15
+
16
+ context "lookup" do
17
+ setup do
18
+ @finder = Anagram::Finder.new(["cat", "wombat"])
19
+ end
20
+
21
+ should "return word if word given" do
22
+ assert_equal ["cat"], @finder.lookup("cat")
23
+ end
24
+
25
+ should "return word if anagram given" do
26
+ assert_equal ["cat"], @finder.lookup("act")
27
+ assert_equal ["cat"], @finder.lookup("tca")
28
+ end
29
+
30
+ should "return nil if no word matches anagram" do
31
+ assert_nil @finder.lookup("wibble")
32
+ end
33
+ end
34
+
35
+ end
@@ -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
+
7
+ context "specifying no dictionary" do
8
+ should "return default" do
9
+ opts = Anagram::Options.new(["someword"])
10
+ assert_equal Anagram::Options::DEFAULT_DICTIONARY, opts.dictionary
11
+ end
12
+ end
13
+
14
+ context "specifying a dictionary" do
15
+ should "return it" do
16
+ opts = Anagram::Options.new(["-d", "mydict", "someword"])
17
+ assert_equal "mydict", opts.dictionary
18
+ end
19
+ end
20
+
21
+ context "specifying words and no dictionary" do
22
+ should "return the words" do
23
+ opts = Anagram::Options.new(["word1", "word2"])
24
+ assert_equal ["word1", "word2"], opts.words_to_find
25
+ end
26
+ end
27
+
28
+ context "specifying words and a dictionary" do
29
+ should "return the words" do
30
+ opts = Anagram::Options.new(["-d", "mydict", "word1", "word2"])
31
+ assert_equal ["word1", "word2"], opts.words_to_find
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anagram-mine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Thomas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: dave@pragprog.com
15
+ executables:
16
+ - anagram
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - INSTALL
21
+ - LICENCE
22
+ - README
23
+ - anagram-0.0.1.gem
24
+ - anagram.gemspec
25
+ - bin/anagram
26
+ - latest_specs.4.8
27
+ - latest_specs.4.8.gz
28
+ - lib/anagram/finder.rb
29
+ - lib/anagram/options.rb
30
+ - lib/anagram/runner.rb
31
+ - prerelease_specs.4.8
32
+ - prerelease_specs.4.8.gz
33
+ - specs.4.8
34
+ - specs.4.8.gz
35
+ - test/test_finder.rb
36
+ - test/test_options.rb
37
+ homepage: http://pragdave.pragprog.com
38
+ licenses: []
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '1.9'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements:
55
+ - An installed dictinoary (most Unix systems have one)
56
+ rubyforge_project:
57
+ rubygems_version: 2.5.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Find anagrams of words supplied on the command line
61
+ test_files:
62
+ - test/test_finder.rb
63
+ - test/test_options.rb