wordplay 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2805071a9f07e75892b75599c51c7e92ac7d4b24
4
- data.tar.gz: 8d165947bf1fe11d869b689e716520740e0b9656
3
+ metadata.gz: f59373476d4502c78581b46f6630f77d83936a5e
4
+ data.tar.gz: e17b85a029b667860b6bbe39c391b6b194cef457
5
5
  SHA512:
6
- metadata.gz: eff6179d2cec65fbcfd95dadcb14f54a7c1e0064ccfc15fa5d513b9c13b7861bc0f8c41ae7221d65b7d76429eeaa1cfaf4836ad8187e8ad30c7d9e3e3d4734d0
7
- data.tar.gz: fd786a30697dd0506cf66be98be62425390134945897304c37169dcdd93735bab0a2a029175ef5ff218b159ece142bee51c45ce247e45c9c548cdeeb5236ddf3
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. Currently can match words with their
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("loop")
19
- word.reverse_ordered_letters # pool
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 *bijoux*.
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
 
@@ -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
- longest_words = [""]
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
- if Wordplay.new(word).ordered_letters?
17
- puts word
18
- match_count += 1
19
- longest_words << word if word.size >= longest_words.last.size
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
- longest = longest_words.last.size
27
- longest_words.delete_if { |w| w.size < longest }
37
+ longest_ordered_words = longest_words(ordered_words)
38
+ longest_palindromes = longest_words(palindromes)
28
39
 
29
- puts "\nMatching words: #{match_count} out of #{full_count}"
30
- puts "Longest words: #{longest_words.join(', ')}"
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(', ')}"
@@ -1,3 +1,3 @@
1
1
  class Wordplay
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordplay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Boone