anagram-brytiuk 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: 968ca576863b84a188e14a706a1cbce070c71c66
4
+ data.tar.gz: cddc2f7452bc8ae628b1c525b455787837238308
5
+ SHA512:
6
+ metadata.gz: 305d622ec1645c58353452d5b26f328c42786bcdcd48492c7f676c04cbf2159be1fbc17a67839044f247715391808b21311e3f431a0f02c64c118a9fda3f503f
7
+ data.tar.gz: 246ebff50a3d79290f8920b9ec5e8bed7d8d71e3f42789c1ae013b249d579a87084cc36330bd9ad046ca86e5424de03b650b3e351582c99cc9b97c19321b05ed
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "anagram-brytiuk"
3
+ s.summary = "Find anagrams of words supplied on the command line"
4
+ s.description = 'Hello'
5
+ s.requirements = [ 'An installed dictionary (most Unix systems have one)' ]
6
+ s.version = "0.0.1"
7
+ s.author = "Dave Thomas"
8
+ s.email = "dave@pragprog.com"
9
+ s.homepage = "http://pragdave.pragprog.com"
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.has_rdoc = false
16
+ end
@@ -0,0 +1,4 @@
1
+ require 'anagram/runner'
2
+
3
+ runner = Anagram::Runner.new(ARGV)
4
+ runner.run
@@ -0,0 +1,25 @@
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 = Hash.new
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
@@ -0,0 +1,37 @@
1
+ require 'optparse'
2
+
3
+ module Anagram
4
+ class Options
5
+ DEFAULT_DICTIONARY = "/usr/share/dict/words"
6
+ attr_reader :dictionary, :words_to_find
7
+
8
+ def initialize(argv)
9
+ @dictionary = DEFAULT_DICTIONARY
10
+ parse(argv)
11
+ @words_to_find = argv
12
+ end
13
+
14
+ private
15
+
16
+ def parse(argv)
17
+ OptionParser.new do |opts|
18
+ opts.banner = "Usage: anagram [ options ] word..."
19
+ opts.on("-d", "--dict path", String, "Path to dictionary") do |dict|
20
+ @dictionary = dict
21
+ end
22
+
23
+ opts.on("-h", "--help", "Show this message") do
24
+ puts opts
25
+ exit
26
+ end
27
+ begin
28
+ argv = ["-h"] if argv.empty?
29
+ opts.parse!(argv)
30
+ rescue OptionParser::ParseError => e
31
+ STDERR.puts e.message, "\n", opts
32
+ exit(-1)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,29 @@
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
8
+ |word, signature|
9
+ should "be #{signature} for #{word}" do
10
+ assert_equal signature, Anagram::Finder.signature_of(word)
11
+ end
12
+ end
13
+ end
14
+ context "lookup" do
15
+ setup do
16
+ @finder = Anagram::Finder.new(["cat", "wombat"])
17
+ end
18
+ should "return word if word given" do
19
+ assert_equal ["cat"], @finder.lookup("cat")
20
+ end
21
+ should "return word if anagram given" do
22
+ assert_equal ["cat"], @finder.lookup("act")
23
+ assert_equal ["cat"], @finder.lookup("tca")
24
+ end
25
+ should "return nil if no word matches anagram" do
26
+ assert_nil @finder.lookup("wibble")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
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
+ context "specifying a dictionary" do
13
+ should "return it" do
14
+ opts = Anagram::Options.new(["-d", "mydict", "someword"])
15
+ assert_equal "mydict", opts.dictionary
16
+ end
17
+ end
18
+ context "specifying words and no dictionary" do
19
+ should "return the words" do
20
+ opts = Anagram::Options.new(["word1", "word2"])
21
+ assert_equal ["word1", "word2"], opts.words_to_find
22
+ end
23
+ end
24
+ context "specifying words and a dictionary" do
25
+ should "return the words" do
26
+ opts = Anagram::Options.new(["-d", "mydict", "word1", "word2"])
27
+ assert_equal ["word1", "word2"], opts.words_to_find
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anagram-brytiuk
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: 2014-03-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Hello
14
+ email: dave@pragprog.com
15
+ executables:
16
+ - anagram
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - anagram-brytiuk.gemspec
21
+ - bin/anagram
22
+ - lib/anagram/finder.rb
23
+ - lib/anagram/options.rb
24
+ - lib/anagram/runner.rb
25
+ - test/test_finder.rb
26
+ - test/test_options.rb
27
+ homepage: http://pragdave.pragprog.com
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '1.9'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements:
45
+ - An installed dictionary (most Unix systems have one)
46
+ rubyforge_project:
47
+ rubygems_version: 2.2.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Find anagrams of words supplied on the command line
51
+ test_files:
52
+ - test/test_finder.rb
53
+ - test/test_options.rb