palindromes 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28d0bf69cb60f6459614c44e39d8f166cf54eca4
4
+ data.tar.gz: 93b9ac3029a4f9976a1603ca08e35b1307065da3
5
+ SHA512:
6
+ metadata.gz: 4297bd9b83e21f0b5d28d69b2fb7afd357a61246aaab3a120edf4e7b23425c53573dcaf80e922666cf8c400889f193bd6e878abd5a7a9b79c2a81da83084594b
7
+ data.tar.gz: de653134023ef8ac446f05588bad7b938cea26e842349dbedac2c3ae959fb2fca84f44bb1efbadc5146a23fa6610539a922fd2dcb64dc25100bd1f1d921b354d
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ *.png
3
+ .DS_Store
4
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # palindromes
2
+ This tool takes in an text file as input and outputs palindromes and histogram
3
+
4
+ ### Requirements
5
+ - ImageMagic `OSX: brew install imagemagick`
6
+ - Ruby (tested on 2.1.2p95)
7
+
8
+ ### Installation
9
+ ```bash
10
+ # Gem Installation
11
+ $ gem install palindromes
12
+
13
+ - OR -
14
+
15
+ # Bundle Installation
16
+ $ bundle install
17
+ $ alias palindromes="bundle exec ./bin/palindromes"
18
+ ```
19
+
20
+
21
+ ### Usage
22
+ ```bash
23
+
24
+ # Usage Help
25
+ palindromes --help
26
+
27
+ # default search (output to stdout and chart to ./histogram.png)
28
+ $ palindromes find <input_file>
29
+
30
+ # to redirect output(file and chart)
31
+ # palindromes find -o <output_file> -c <output_chart_file> <input_file>
32
+ $ palindromes find -o /tmp/output.txt -c /tmp/histogram.png /tmp/input.txt
33
+
34
+
35
+
36
+ # default run example/output
37
+ $ palindromes find sample/input.txt
38
+ -- palindromes --
39
+ b > 11
40
+ a > 10
41
+ aaa > 9
42
+ c > 4
43
+ radar > 4
44
+ 101 > 3
45
+ i > 3
46
+ lol > 3
47
+ aaaa > 2
48
+ d > 2
49
+ mom > 2
50
+ s > 2
51
+ aa > 1
52
+ bbbb > 1
53
+ beeb > 1
54
+ ddd > 1
55
+ did > 1
56
+ e > 1
57
+ f > 1
58
+ g > 1
59
+ h > 1
60
+ j > 1
61
+ k > 1
62
+ l > 1
63
+ m > 1
64
+ n > 1
65
+ o > 1
66
+ p > 1
67
+ peep > 1
68
+ q > 1
69
+ r > 1
70
+ t > 1
71
+ u > 1
72
+ v > 1
73
+ w > 1
74
+ x > 1
75
+ y > 1
76
+ z > 1
77
+ -- histogram --
78
+ {"a"=>10, "radar"=>4, "did"=>1, "peep"=>1, "mom"=>2, "s"=>2, "i"=>3, "beeb"=>1, "101"=>3, "b"=>11, "c"=>4, "lol"=>3, "aaa"=>9, "v"=>1, "w"=>1, "x"=>1, "y"=>1, "z"=>1, "t"=>1, "u"=>1, "o"=>1, "p"=>1, "q"=>1, "r"=>1, "k"=>1, "l"=>1, "m"=>1, "n"=>1, "g"=>1, "h"=>1, "j"=>1, "d"=>2, "e"=>1, "f"=>1, "aa"=>1, "aaaa"=>2, "bbbb"=>1, "ddd"=>1}
79
+
80
+ -- histogram chart --
81
+ generating chart... histogram.png
82
+ ```
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/palindromes ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cli'
4
+ Palindromes::Cli.run
data/lib/cli.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'clamp'
2
+ require 'palindromes'
3
+
4
+ module Palindromes
5
+ class Cli < Clamp::Command
6
+ subcommand 'find', 'find palindromes in input file...' do
7
+ option ["-o", "--output"], "OUTPUT FILE", "required: Output File", :required => false
8
+ option ["-c", "--chart"], "CHART FILE", "required: Location For Chart Output", :required => false
9
+
10
+ parameter "command", "command to run", :attribute_name => :command
11
+
12
+ def execute
13
+ Palindromes.run(command, output, chart)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,53 @@
1
+ require 'palindromes/base'
2
+ require 'palindromes/chart'
3
+
4
+ module Palindromes
5
+ class << self
6
+
7
+ def run(input_file, output_file, chart_file)
8
+ data = read_file(input_file)
9
+ chart_file = chart_file ? chart_file : 'histogram.png'
10
+
11
+ p = Palindromes::Base::new
12
+ p.find(data)
13
+
14
+ output = String.new
15
+ output << "\n-- palindromes --\n"
16
+ output << format(p.histograms).join("\n")
17
+ output << "\n-- histogram --\n"
18
+ output << p.histograms.to_s
19
+
20
+ if output_file
21
+ puts "generating output... #{output_file}"
22
+ File.write(output_file, output)
23
+ else
24
+ puts output
25
+ end
26
+
27
+ # generate histogram chart
28
+ puts "\n-- histogram chart --\n"
29
+ puts "generating chart... #{chart_file}"
30
+ Palindromes::Chart.generate(p.histograms, chart_file)
31
+ end
32
+
33
+ # read file and remove unwanted characters
34
+ def read_file(file)
35
+ File.read(file).downcase.split(/[^a-zA-Z0-9]/).delete_if { |word| word == "" }
36
+ end
37
+
38
+ def format(content)
39
+ # group palindrome with the same count together
40
+ grouped = content.group_by { |word, count| count }
41
+
42
+ # sort the grouped palindrome by word and print
43
+ output = []
44
+ grouped.sort.reverse.each do |gkey, gvalue|
45
+ gvalue.sort_by { |k,v| k }.each do |word, count|
46
+ output << "#{word} > #{count}"
47
+ end
48
+ end
49
+ output
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,27 @@
1
+ module Palindromes
2
+ class Base
3
+ attr_reader :palindromes, :histograms
4
+
5
+ # search for palindromes
6
+ def find(data)
7
+ @palindromes = data.select do |word|
8
+ palindrome?(word)
9
+ end
10
+ histogram
11
+ end
12
+
13
+ private
14
+
15
+ # to determine palindrome word must be eq to its reverse
16
+ def palindrome?(word)
17
+ word == word.reverse
18
+ end
19
+
20
+ # get histogram
21
+ def histogram
22
+ @histograms = Hash.new(0)
23
+ @palindromes.each { |word| @histograms[word] += 1 }
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ require 'gruff'
2
+
3
+ module Palindromes
4
+ class Chart
5
+ class << self
6
+ def generate(data, chart_file)
7
+ g = Gruff::Bar.new
8
+ g.title = "histogram"
9
+
10
+ c = 0
11
+ data.each do |key, value|
12
+ g.labels.merge( c => key )
13
+ g.data(key, [value])
14
+ c += 1
15
+ end
16
+
17
+ g.minimum_value = 0
18
+ g.write(chart_file)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'palindromes'
5
+ s.version = File.read 'VERSION'
6
+ s.authors = %w'mmoghadas'
7
+ s.email = %w'mike.moghadas@gmail.com'
8
+ s.description = %q{palindromes: diag tool}
9
+ s.summary = %q{palindromes: diag tool}
10
+ s.homepage = ''
11
+ s.license = 'MIT'
12
+
13
+ s.files = `git ls-files`.split($/)
14
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+ s.require_paths = %w'lib'
17
+
18
+ s.add_dependency 'clamp'
19
+ s.add_dependency 'gruff'
20
+ end
data/sample/input.txt ADDED
@@ -0,0 +1,49 @@
1
+ A cat sat radar on the mat and did not peep for a single moment. Mom’s phone rang and I ran to the door but Beeb went the radar at 101 miles an hour 101 a b b b c 101.
2
+
3
+ lol lol lol
4
+
5
+ aaa aaa aaa aaa
6
+ how about we go fishing at the radar lake. a ok?
7
+
8
+ one time for the b camp!
9
+
10
+
11
+ v
12
+ w
13
+ x
14
+ y
15
+ z
16
+
17
+ s
18
+ t
19
+ u
20
+
21
+ o
22
+ p
23
+ q
24
+ r
25
+
26
+ k
27
+ l
28
+ m
29
+ n
30
+
31
+ g
32
+ h
33
+ i
34
+ j
35
+
36
+ d
37
+ e
38
+ f
39
+
40
+ a
41
+ b
42
+ c
43
+
44
+ .mom#I$%AA
45
+ aaaa bbbb radaR$world!
46
+ &&aaaa**.a
47
+
48
+
49
+ a b c d a b A c ddd aaa aaa AAA abc AAA aaa b a b b b
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: palindromes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mmoghadas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: clamp
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gruff
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: 'palindromes: diag tool'
42
+ email:
43
+ - mike.moghadas@gmail.com
44
+ executables:
45
+ - palindromes
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - README.md
52
+ - VERSION
53
+ - bin/palindromes
54
+ - lib/cli.rb
55
+ - lib/palindromes.rb
56
+ - lib/palindromes/base.rb
57
+ - lib/palindromes/chart.rb
58
+ - palindromes.gemspec
59
+ - sample/input.txt
60
+ homepage: ''
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: 'palindromes: diag tool'
84
+ test_files: []
85
+ has_rdoc: