Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added some repetition detection #5

Merged
merged 4 commits into from

2 participants

@dlom

Fixes #4

Also, the whole require 'shamazing' in the bin script thing wasn't working out for me, which I think has to do with having shamazing installed alongside the developer version. I wasn't sure what to do there, so I suppose you can cherry-pick around that if you want.

@holman holman merged commit a30dbb9 into holman:master
@holman
Owner

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
32 README.md
@@ -5,8 +5,9 @@ It's **sha**-mazing. Almost as shamazing as that pun.
## Areas of shamazingness
-- Longest string (9c2**cddfaedaea**9689a22e376aa20191041554fe8)
-- Longest integer (9c2cddfaedaea9689a22e376aa**20191041554**fe8)
+- Longest string (9c2**cddfaedaea**9689a22e37aaa20191041554fe8)
+- Longest integer (9c2cddfaedaea9689a22e37aaa**20191041554**fe8)
+- Longest repeating sequence (9c2cddfaedaea9689a22e37**aaa**20191041554fe8)
## Install
@@ -17,22 +18,29 @@ It's **sha**-mazing. Almost as shamazing as that pun.
### Command Line Interface
```sh
-$ shamazing 9c2cddfaedaea9689a22e376aa20191041554fe8
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8
Longest string: cddfaedaea
Longest integer: 20191041554
+Longest repeating: aaa
-$ shamazing 9c2cddfaedaea9689a22e376aa20191041554fe8 --string
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8 --string
cddfaedaea
-$ shamazing 9c2cddfaedaea9689a22e376aa20191041554fe8 -s
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8 -s
cddfaedaea
-$ shamazing 9c2cddfaedaea9689a22e376aa20191041554fe8 --integer
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8 --integer
20191041554
-$ shamazing 9c2cddfaedaea9689a22e376aa20191041554fe8 -i
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8 -i
20191041554
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8 --repeating
+aaa
+
+$ shamazing 9c2cddfaedaea9689a22e37aaa20191041554fe8 -r
+aaa
+
# Search through the shas using `git log --format="%H"` in the current directory.
$ shamazing -s
cddfaedaea
@@ -41,6 +49,7 @@ cddfaedaea
$ shamazing --full
Longest string: 17705a5a37fbd11017f0d5e053b474dabbbd4022
Longest integer: 0c4b61fc2c5e7dd5566d42d0de1c431984899ddf
+Longest repeating: 17705a5a37fbd11017f0d5e053b474dabbbd4022
```
### Ruby Interface
@@ -48,7 +57,7 @@ Longest integer: 0c4b61fc2c5e7dd5566d42d0de1c431984899ddf
```ruby
require 'shamazing'
-sha = '9c2cddfaedaea9689a22e376aa20191041554fe8'
+sha = '9c2cddfaedaea9689a22e37aaa20191041554fe8'
Shamazing.string(sha)
# => cddfaedaea
@@ -56,16 +65,21 @@ Shamazing.string(sha)
Shamazing.integer(sha)
# => 20191041554
+Shamazing.repeating(sha)
+# => aaa
+
shas = %w(
fdb31214c2cca29e4f723ad676cddb043bd73986
0c4b61fc2c5e7dd5566d42d0de1c431984899ddf
- 9c2cddfaedaea9689a22e376aa20191041554fe8
+ 9c2cddfaedaea9689a22e37aaa20191041554fe8
f1b4c270f6746cbfff99bbf0f5a2388f4e509943
)
Shamazing.string_from_array(shas)
# => cddfaedaea
Shamazing.integer_from_array(shas)
# => 20191041554
+Shamazing.repeating_from_array(shas)
+# => fff
```
## An Holman Project
View
28 bin/shamazing
@@ -18,15 +18,17 @@
# $ shamazing 9c2cddfaedaea9689a22e376aa20191041554fe8 -i
# 20191041554
+$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
+
require 'optparse'
-require 'rubygems'
require 'shamazing'
require 'rugged'
options = {
- :string => false,
- :integer => false,
- :full => false
+ :string => false,
+ :integer => false,
+ :repeating => false,
+ :full => false
}
parser = OptionParser.new do |opts|
@@ -46,6 +48,10 @@ parser = OptionParser.new do |opts|
options[:integer] = true
end
+ opts.on("-r", "--repeating", "The longest repeating string of characters in this SHA") do
+ options[:repeating] = true
+ end
+
opts.on("-f", "--full", "Display full SHAs instead of snippets") do
options[:full] = true
end
@@ -63,23 +69,29 @@ if ARGV.empty?
repo = Rugged::Repository.new('.')
shas = repo.walk(repo.head.target).map(&:oid)
- if options[:string] && !options[:integer]
+ if options[:string] && !options[:integer] && !options[:repeating]
puts Shamazing.string_from_array(shas,options[:full])
- elsif options[:integer]
+ elsif options[:integer] && !options[:repeating]
puts Shamazing.integer_from_array(shas,options[:full])
+ elsif options[:repeating]
+ puts Shamazing.repeating_from_array(shas,options[:full])
else
puts "Longest string: #{Shamazing.string_from_array(shas,options[:full])}"
puts "Longest integer: #{Shamazing.integer_from_array(shas,options[:full])}"
+ puts "Longest repeating: #{Shamazing.repeating_from_array(shas,options[:full])}"
end
else
sha = ARGV.first
- if options[:string] && !options[:integer]
+ if options[:string] && !options[:integer] && !options[:repeating]
puts Shamazing.string(sha)
- elsif options[:integer]
+ elsif options[:integer] && !options[:repeating]
puts Shamazing.integer(sha)
+ elsif options[:repeating]
+ puts Shamazing.repeating(sha)
else
puts "Longest string: #{Shamazing.string(sha)}"
puts "Longest integer: #{Shamazing.integer(sha)}"
+ puts "Longest repeating: #{Shamazing.repeating(sha)}"
end
end
View
29 lib/shamazing.rb
@@ -53,4 +53,33 @@ def self.integer_from_array(shas,full=false)
first.to_i
full ? shas.find{|sha| sha.match(/#{longest}/)} : longest
end
+
+ # Finds the longest repeating string of characters in a SHA.
+ #
+ # sha - The String SHA to analyze.
+ #
+ # Returns a String.
+ def self.repeating(sha)
+ sha.
+ downcase.
+ scan(/(\w)(\1+)/).
+ collect{|a| a.join}.
+ sort{|a,b| b.length <=> a.length}.
+ first
+ end
+
+ # Finds the longest repeating string of characters in an Array of SHAs.
+ #
+ # sha - The String SHA to analyze.
+ # full - A Boolean: should we return the full SHA instead of the snippet?
+ #
+ # Returns the longest String.
+ def self.repeating_from_array(shas,full=false)
+ longest = shas.
+ collect{|sha| repeating(sha)}.
+ delete_if{|sha| sha.nil?}.
+ sort{|a,b| b.length <=> a.length}.
+ first
+ full ? shas.find{|sha| sha.match(/#{longest}/)} : longest
+ end
end
View
17 test/test_shamazing.rb
@@ -3,7 +3,7 @@
class TestShamazing < Test::Unit::TestCase
def setup
- @sha = '9c2cddfaedaea9689a22e376aa20191041554fe8'
+ @sha = '9c2cddfaedaea9689a22e37aaa20191041554fe8'
@shas = %w(
fdb31214c2cca29e4f723ad676cddb043bd73986
0c4b61fc2c5e7dd5566d42d0de1c431984899ddf
@@ -32,11 +32,20 @@ def test_integer_from_array
assert_equal 20191041554, Shamazing.integer_from_array(@shas)
end
+ def test_repeating
+ assert_equal 'aaa', Shamazing.repeating(@sha)
+ end
+
+ def test_repeating_from_array
+ assert_equal 'fff', Shamazing.repeating_from_array(@shas)
+ end
+
def test_cli
output = `bin/shamazing #{@sha}`.chomp
assert_match /Longest string: cddfaedaea/, output
assert_match /Longest integer: 20191041554/, output
+ assert_match /Longest repeating: aaa/, output
end
def test_cli_string
@@ -49,8 +58,14 @@ def test_cli_integer
assert_equal '20191041554', `bin/shamazing #{@sha} --integer`.chomp
end
+ def test_cli_repeating
+ assert_equal 'aaa', `bin/shamazing #{@sha} -r`.chomp
+ assert_equal 'aaa', `bin/shamazing #{@sha} --repeating`.chomp
+ end
+
def test_cli_git
assert_match /Longest string: /, `bin/shamazing`.chomp
assert_match /Longest integer: /, `bin/shamazing`.chomp
+ assert_match /Longest repeating: /, `bin/shamazing`.chomp
end
end
Something went wrong with that request. Please try again.