wordplay 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 +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +7 -0
- data/examples/dictionary_scan.rb +30 -0
- data/lib/wordplay/version.rb +3 -0
- data/lib/wordplay.rb +46 -0
- data/spec/wordplay_spec.rb +46 -0
- metadata +71 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 2805071a9f07e75892b75599c51c7e92ac7d4b24
|
|
4
|
+
data.tar.gz: 8d165947bf1fe11d869b689e716520740e0b9656
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: eff6179d2cec65fbcfd95dadcb14f54a7c1e0064ccfc15fa5d513b9c13b7861bc0f8c41ae7221d65b7d76429eeaa1cfaf4836ad8187e8ad30c7d9e3e3d4734d0
|
|
7
|
+
data.tar.gz: fd786a30697dd0506cf66be98be62425390134945897304c37169dcdd93735bab0a2a029175ef5ff218b159ece142bee51c45ce247e45c9c548cdeeb5236ddf3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The following license applies to all files which aren't used verbatim from
|
|
2
|
+
Rails:
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2015 Michael Boone
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Wordplay
|
|
2
|
+
|
|
3
|
+
Some interesting methods to use on words. Currently can match words with their
|
|
4
|
+
letters in alphabetical or reverse alphabetical order.
|
|
5
|
+
|
|
6
|
+
The idea came from a story about a boy, who, when asked to put words in
|
|
7
|
+
alphabetical order, ordered the letters of the word instead of the words
|
|
8
|
+
themselves. I was intrigued by "fox" which was already sorted in that manner.
|
|
9
|
+
|
|
10
|
+
## Example:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
require "wordplay"
|
|
14
|
+
|
|
15
|
+
word = Wordplay.new("fox")
|
|
16
|
+
word.ordered_letters? # true
|
|
17
|
+
|
|
18
|
+
word = Wordplay.new("loop")
|
|
19
|
+
word.reverse_ordered_letters # pool
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
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*.
|
|
24
|
+
|
|
25
|
+
# Contributing
|
|
26
|
+
|
|
27
|
+
If you have an interesting word method or a bugfix, please submit a pull
|
|
28
|
+
request.
|
|
29
|
+
|
|
30
|
+
# Contact
|
|
31
|
+
|
|
32
|
+
[@boonedocks](https://twitter.com/boonedocks) on Twitter
|
data/Rakefile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require_relative '../lib/wordplay'
|
|
2
|
+
|
|
3
|
+
SOURCE = '/usr/share/dict/words'
|
|
4
|
+
|
|
5
|
+
raise "Dictionary not available!" unless File.exists?(SOURCE)
|
|
6
|
+
|
|
7
|
+
match_count = 0
|
|
8
|
+
full_count = 0
|
|
9
|
+
longest_words = [""]
|
|
10
|
+
|
|
11
|
+
# Loop the source file for words to check
|
|
12
|
+
File.foreach(SOURCE, "\n") do |word|
|
|
13
|
+
word.strip! # remove the linefeeds
|
|
14
|
+
|
|
15
|
+
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
|
|
21
|
+
|
|
22
|
+
full_count += 1
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
longest = longest_words.last.size
|
|
27
|
+
longest_words.delete_if { |w| w.size < longest }
|
|
28
|
+
|
|
29
|
+
puts "\nMatching words: #{match_count} out of #{full_count}"
|
|
30
|
+
puts "Longest words: #{longest_words.join(', ')}"
|
data/lib/wordplay.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
class Wordplay
|
|
2
|
+
# Public: creates a class instance
|
|
3
|
+
# word - a String
|
|
4
|
+
def initialize(word)
|
|
5
|
+
@word = word.downcase # always work with lowercase
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# Public: determines if the component letters of the provided word are
|
|
9
|
+
# in alphabetical order.
|
|
10
|
+
# E.g.: "fox" has its letters in order, "car" does not.
|
|
11
|
+
#
|
|
12
|
+
# Returns a Boolean
|
|
13
|
+
def ordered_letters?
|
|
14
|
+
# we could just run `@word == ordered_letters` but this is faster
|
|
15
|
+
1.upto(@word.length - 1) do |i|
|
|
16
|
+
return false unless @word[i] >= @word[i - 1]
|
|
17
|
+
end
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Public: determines if the component letters of the provided word are
|
|
22
|
+
# in reverse alphabetical order.
|
|
23
|
+
# E.g.: "pig" has its letters in reverse order, "car" does not.
|
|
24
|
+
#
|
|
25
|
+
# Returns a Boolean
|
|
26
|
+
def reverse_ordered_letters?
|
|
27
|
+
1.upto(@word.length - 1) do |i|
|
|
28
|
+
return false unless @word[i] <= @word[i - 1]
|
|
29
|
+
end
|
|
30
|
+
true
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Public: orders the letters of a word in alphabetical order.
|
|
34
|
+
#
|
|
35
|
+
# Returns a String
|
|
36
|
+
def ordered_letters
|
|
37
|
+
@word.chars.sort.join
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Public: orders the letters of a word in reverse alphabetical order.
|
|
41
|
+
#
|
|
42
|
+
# Returns a String
|
|
43
|
+
def reverse_ordered_letters
|
|
44
|
+
ordered_letters.reverse
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require "rspec"
|
|
2
|
+
require "wordplay"
|
|
3
|
+
|
|
4
|
+
describe Wordplay do
|
|
5
|
+
describe "#ordered_letters?" do
|
|
6
|
+
it "returns true for words with ordered letters" do
|
|
7
|
+
%w(a ab bijoux fox nosy tux).each do |word|
|
|
8
|
+
expect(described_class.new(word).ordered_letters?).to eq(true)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "returns false for words without ordered letters" do
|
|
13
|
+
%w(bad car foxhole zigzag).each do |word|
|
|
14
|
+
expect(described_class.new(word).ordered_letters?).to eq(false)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe "#reverse_ordered_letters?" do
|
|
20
|
+
it "returns true for words with reverse ordered letters" do
|
|
21
|
+
%w(a fed solid toffee).each do |word|
|
|
22
|
+
expect(described_class.new(word).reverse_ordered_letters?).to eq(true)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "returns false for words without reverse ordered letters" do
|
|
27
|
+
%w(bad car foxhole zigzag).each do |word|
|
|
28
|
+
expect(described_class.new(word).ordered_letters?).to eq(false)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
describe "#ordered_letters" do
|
|
34
|
+
it "sorts the letters" do
|
|
35
|
+
expect(described_class.new("complicated").ordered_letters)
|
|
36
|
+
.to eq("accdeilmopt")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe "#reverse_ordered_letters" do
|
|
41
|
+
it "sorts the letters in reverse" do
|
|
42
|
+
expect(described_class.new("complicated").reverse_ordered_letters)
|
|
43
|
+
.to eq("tpomliedcca")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: wordplay
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mike Boone
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-03-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rspec
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.2'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 3.2.0
|
|
23
|
+
type: :development
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - "~>"
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '3.2'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 3.2.0
|
|
33
|
+
description: Some interesting methods to use on words.
|
|
34
|
+
email: mike@boonedocks.net
|
|
35
|
+
executables: []
|
|
36
|
+
extensions: []
|
|
37
|
+
extra_rdoc_files: []
|
|
38
|
+
files:
|
|
39
|
+
- Gemfile
|
|
40
|
+
- LICENSE.txt
|
|
41
|
+
- README.md
|
|
42
|
+
- Rakefile
|
|
43
|
+
- examples/dictionary_scan.rb
|
|
44
|
+
- lib/wordplay.rb
|
|
45
|
+
- lib/wordplay/version.rb
|
|
46
|
+
- spec/wordplay_spec.rb
|
|
47
|
+
homepage: https://github.com/boone/wordplay
|
|
48
|
+
licenses:
|
|
49
|
+
- MIT
|
|
50
|
+
metadata: {}
|
|
51
|
+
post_install_message:
|
|
52
|
+
rdoc_options: []
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '0'
|
|
65
|
+
requirements: []
|
|
66
|
+
rubyforge_project:
|
|
67
|
+
rubygems_version: 2.4.5
|
|
68
|
+
signing_key:
|
|
69
|
+
specification_version: 4
|
|
70
|
+
summary: Methods for words.
|
|
71
|
+
test_files: []
|