wordplay 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +15 -5
- data/examples/dictionary_scan.rb +22 -11
- data/lib/wordplay/version.rb +1 -1
- data/lib/wordplay.rb +9 -0
- data/spec/wordplay_spec.rb +14 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f59373476d4502c78581b46f6630f77d83936a5e
|
|
4
|
+
data.tar.gz: e17b85a029b667860b6bbe39c391b6b194cef457
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 57d85fabaaf80d7c8ea26c40efe536283cf8b24b52e4b7e22fd3b45c309bb860a7e87d9e0968dfed2f7865d895493f5f3702fd72d9dbbefcfd93103097fee7fa
|
|
7
|
+
data.tar.gz: ef01e328b4666c7f95cf41eac6dc9378d8b617ecb78743460b80bb1e8d13832d90b7cf8f37bf42b7d6302fc9310fc867802084a3ba0c296f9d04f13616362764
|
data/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# Wordplay
|
|
2
2
|
|
|
3
|
-
Some interesting methods to use on words.
|
|
4
|
-
letters in alphabetical or reverse alphabetical order.
|
|
3
|
+
Some interesting methods to use on words.
|
|
5
4
|
|
|
6
5
|
The idea came from a story about a boy, who, when asked to put words in
|
|
7
6
|
alphabetical order, ordered the letters of the word instead of the words
|
|
@@ -15,12 +14,23 @@ require "wordplay"
|
|
|
15
14
|
word = Wordplay.new("fox")
|
|
16
15
|
word.ordered_letters? # true
|
|
17
16
|
|
|
18
|
-
word = Wordplay.new("
|
|
19
|
-
word.reverse_ordered_letters #
|
|
17
|
+
word = Wordplay.new("pizza")
|
|
18
|
+
word.reverse_ordered_letters # zzpia
|
|
20
19
|
```
|
|
21
20
|
|
|
22
21
|
There is also an example which loops through `/usr/share/dict/words` to find
|
|
23
|
-
words with their letters sorted. I think my favorite is
|
|
22
|
+
words with their letters sorted. I think my favorite is "bijoux".
|
|
23
|
+
|
|
24
|
+
## New in Version 0.0.2
|
|
25
|
+
|
|
26
|
+
Match palindromes!
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require "wordplay"
|
|
30
|
+
|
|
31
|
+
words = Wordplay.new("Do geese see God?") # ignores spaces and punctuation
|
|
32
|
+
words.palindrome? # true
|
|
33
|
+
```
|
|
24
34
|
|
|
25
35
|
# Contributing
|
|
26
36
|
|
data/examples/dictionary_scan.rb
CHANGED
|
@@ -1,30 +1,41 @@
|
|
|
1
1
|
require_relative '../lib/wordplay'
|
|
2
2
|
|
|
3
|
+
# Determine the longest words in the supplied group.
|
|
4
|
+
#
|
|
5
|
+
# words - Array
|
|
6
|
+
#
|
|
7
|
+
# Returns an Array.
|
|
8
|
+
def longest_words(words)
|
|
9
|
+
words.sort_by { |w| w.size }.tap do |sorted|
|
|
10
|
+
longest = sorted.last.size
|
|
11
|
+
sorted.delete_if { |w| w.size < longest }
|
|
12
|
+
end.sort
|
|
13
|
+
end
|
|
14
|
+
|
|
3
15
|
SOURCE = '/usr/share/dict/words'
|
|
4
16
|
|
|
5
17
|
raise "Dictionary not available!" unless File.exists?(SOURCE)
|
|
6
18
|
|
|
7
|
-
match_count = 0
|
|
8
19
|
full_count = 0
|
|
9
|
-
|
|
20
|
+
ordered_words = []
|
|
21
|
+
palindromes = []
|
|
10
22
|
|
|
11
23
|
# Loop the source file for words to check
|
|
12
24
|
File.foreach(SOURCE, "\n") do |word|
|
|
13
25
|
word.strip! # remove the linefeeds
|
|
14
26
|
|
|
15
27
|
if word.size > 1 # exclude one-character words
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
end
|
|
28
|
+
wordplay = Wordplay.new(word)
|
|
29
|
+
|
|
30
|
+
ordered_words << word if wordplay.ordered_letters?
|
|
31
|
+
palindromes << word if wordplay.palindrome?
|
|
21
32
|
|
|
22
33
|
full_count += 1
|
|
23
34
|
end
|
|
24
35
|
end
|
|
25
36
|
|
|
26
|
-
|
|
27
|
-
longest_words
|
|
37
|
+
longest_ordered_words = longest_words(ordered_words)
|
|
38
|
+
longest_palindromes = longest_words(palindromes)
|
|
28
39
|
|
|
29
|
-
puts "
|
|
30
|
-
puts "
|
|
40
|
+
puts "Ordered words (#{ordered_words.size} of #{full_count}), longest: #{longest_ordered_words.join(', ')}"
|
|
41
|
+
puts "Palindromes (#{palindromes.size} of #{full_count}), longest: #{longest_palindromes.join(', ')}"
|
data/lib/wordplay/version.rb
CHANGED
data/lib/wordplay.rb
CHANGED
|
@@ -43,4 +43,13 @@ class Wordplay
|
|
|
43
43
|
def reverse_ordered_letters
|
|
44
44
|
ordered_letters.reverse
|
|
45
45
|
end
|
|
46
|
+
|
|
47
|
+
# Public: determines if the provided string is a palindome
|
|
48
|
+
# Only non-accented ASCII letters are accepted for now.
|
|
49
|
+
#
|
|
50
|
+
# Returns a Boolean
|
|
51
|
+
def palindrome?
|
|
52
|
+
formatted_word = @word.gsub(/[^a-z]/, "")
|
|
53
|
+
formatted_word == formatted_word.reverse
|
|
54
|
+
end
|
|
46
55
|
end
|
data/spec/wordplay_spec.rb
CHANGED
|
@@ -43,4 +43,18 @@ describe Wordplay do
|
|
|
43
43
|
.to eq("tpomliedcca")
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
|
+
|
|
47
|
+
describe "#palindrome?" do
|
|
48
|
+
["level", "race car", "Madam, I'm Adam"].each do |string|
|
|
49
|
+
it "#{string} is a palindrome" do
|
|
50
|
+
expect(described_class.new(string).palindrome?).to be true
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
["pizza", "quick brown fox", "Penguin Farm"].each do |string|
|
|
55
|
+
it "#{string} is not a palindrome" do
|
|
56
|
+
expect(described_class.new(string).palindrome?).to be false
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
46
60
|
end
|