word_scramble 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +40 -0
- data/Rakefile +2 -0
- data/lib/word_scramble/descrambler.rb +25 -0
- data/lib/word_scramble/scrambled_word.rb +38 -0
- data/lib/word_scramble/version.rb +3 -0
- data/lib/word_scramble.rb +34 -0
- data/spec/descrambler_spec.rb +31 -0
- data/spec/scrambled_word_spec.rb +16 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/word_checker_spec.rb +27 -0
- data/spec/word_scramble_spec.rb +11 -0
- data/word_scramble.gemspec +19 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Declan Frye
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# WordScramble
|
2
|
+
|
3
|
+
This gem is for generating real words from scrambled strings.
|
4
|
+
I wrote it to solve a puzzle that they passed out in the Virgin
|
5
|
+
Airlines waiting lounge when the flight was delayed.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'word_scramble'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install word_scramble
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You can use it from the command line, like this
|
24
|
+
|
25
|
+
descramble <word>
|
26
|
+
|
27
|
+
Or you can call the Descrambler library directly
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
descrambler = WordScramble::Descrambler.new("realapin")
|
31
|
+
descrambler.matching_words # => ["airplane", ... ]
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class WordScramble::Descrambler
|
2
|
+
|
3
|
+
def initialize(scrambled_word)
|
4
|
+
@scrambled_word = WordScramble::ScrambledWord.new(scrambled_word)
|
5
|
+
@matching_words = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def matching_words
|
9
|
+
unless @already_descrambled
|
10
|
+
descramble
|
11
|
+
end
|
12
|
+
@matching_words
|
13
|
+
end
|
14
|
+
|
15
|
+
def descramble
|
16
|
+
WordScramble::DICTIONARY.each do |word|
|
17
|
+
if @scrambled_word.can_make?(word)
|
18
|
+
@matching_words.push(word)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@matching_words.sort! { |a,b| b.length <=> a.length }
|
22
|
+
@already_descrambled = true
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class WordScramble::ScrambledWord
|
2
|
+
|
3
|
+
def initialize(scrambled_word)
|
4
|
+
@scrambled_word = scrambled_word
|
5
|
+
end
|
6
|
+
|
7
|
+
def can_make(word)
|
8
|
+
WordChecker.new(@scrambled_word, word).matches?
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :can_make?, :can_make
|
12
|
+
|
13
|
+
class WordChecker
|
14
|
+
def initialize(scrambled_word, word)
|
15
|
+
@scrambled_letters = scrambled_word.downcase.split('').sort.inject({}) do |frequency_hash, l|
|
16
|
+
frequency_hash[l] ||= 0
|
17
|
+
frequency_hash[l] += 1
|
18
|
+
frequency_hash
|
19
|
+
end
|
20
|
+
@word_letters = word.downcase.split('').sort.inject({}) do |frequency_hash, l|
|
21
|
+
frequency_hash[l] ||= 0
|
22
|
+
frequency_hash[l] += 1
|
23
|
+
frequency_hash
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def matches?
|
28
|
+
@word_letters.each do |letter, count|
|
29
|
+
scrambled_count = @scrambled_letters[letter]
|
30
|
+
return false if scrambled_count.nil? or scrambled_count < count
|
31
|
+
end
|
32
|
+
return true
|
33
|
+
end
|
34
|
+
|
35
|
+
alias_method :matches, :matches?
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "word_scramble/version"
|
2
|
+
|
3
|
+
#######################
|
4
|
+
#
|
5
|
+
# Word Scramble
|
6
|
+
#
|
7
|
+
# A game played in the Virgin Airlines waiting lounge.
|
8
|
+
#
|
9
|
+
# Input is a scrambled string.
|
10
|
+
# You try to make a word (or words) from those letters.
|
11
|
+
#
|
12
|
+
# ===== Examples
|
13
|
+
# "valesages" => "las vegas"
|
14
|
+
# "realapin" => "airplane"
|
15
|
+
# "liopt" => "pilot"
|
16
|
+
#
|
17
|
+
# ==== Usage
|
18
|
+
#
|
19
|
+
# descrambler = WordScramble::Descrambler.new("realapin")
|
20
|
+
# descrambler.matching_words # => ["airplane", ... ]
|
21
|
+
|
22
|
+
module WordScramble
|
23
|
+
DICTIONARY_PATH = '/etc/dictionaries-common/words'
|
24
|
+
DICTIONARY = File.open(DICTIONARY_PATH) do |f|
|
25
|
+
dictionary = []
|
26
|
+
f.each_line do |line|
|
27
|
+
dictionary.push(line.strip)
|
28
|
+
end
|
29
|
+
dictionary
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
require File.expand_path('../word_scramble/scrambled_word', __FILE__)
|
34
|
+
require File.expand_path('../word_scramble/descrambler', __FILE__)
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WordScramble::Descrambler do
|
4
|
+
let(:descrambled) { WordScramble::Descrambler.new(@scrambled).matching_words }
|
5
|
+
|
6
|
+
it "descrambles leloh" do
|
7
|
+
@scrambled = "leloh"
|
8
|
+
descrambled.should include("hello")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "descrambles valesages" do
|
12
|
+
@scrambled = "valesages"
|
13
|
+
descrambled.should include("salvages")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "descrambles liopt" do
|
17
|
+
@scrambled = "liopt"
|
18
|
+
descrambled.should include("pilot")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "descrambles realapin" do
|
22
|
+
@scrambled = "realapin"
|
23
|
+
descrambled.should include("airplane")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "orders the matching words by length" do
|
27
|
+
@scrambled = "liopt"
|
28
|
+
descrambled.first.should == "pilot"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WordScramble::ScrambledWord do
|
4
|
+
describe "valesags" do
|
5
|
+
let(:sw) { WordScramble::ScrambledWord.new("valesags") }
|
6
|
+
|
7
|
+
it "can make 'salvages'" do
|
8
|
+
sw.can_make("salvages").should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "cannot make 'hooligans'" do
|
12
|
+
sw.can_make("hooligans").should == false
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WordScramble::ScrambledWord::WordChecker do
|
4
|
+
describe "lelho" do
|
5
|
+
let(:scrambled_word) { "lelho" }
|
6
|
+
|
7
|
+
it "matches 'hello'" do
|
8
|
+
wc = WordScramble::ScrambledWord::WordChecker.new(scrambled_word, "hello")
|
9
|
+
wc.matches?.should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "doesn't match 'kittens'" do
|
13
|
+
wc = WordScramble::ScrambledWord::WordChecker.new(scrambled_word, "kittens")
|
14
|
+
wc.matches.should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "'princess' doesn't match 'princes'" do
|
18
|
+
wc = WordScramble::ScrambledWord::WordChecker.new("princes", "princess")
|
19
|
+
wc.matches.should be_false
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is case insensitive" do
|
23
|
+
wc = WordScramble::ScrambledWord::WordChecker.new("chicago", "Chicago")
|
24
|
+
wc.matches.should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/word_scramble/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Declan Frye"]
|
6
|
+
gem.email = ["deckleberryfrye@gmail.com"]
|
7
|
+
gem.description = %q{Take a string of scrambled characters and find all the words that can be made from those characters.}
|
8
|
+
gem.summary = %q{Written to solve the Word Scramble game that they give out on Virgin Airlines when the flight is delayed.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "word_scramble"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = WordScramble::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: word_scramble
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Declan Frye
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &77673310 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *77673310
|
25
|
+
description: Take a string of scrambled characters and find all the words that can
|
26
|
+
be made from those characters.
|
27
|
+
email:
|
28
|
+
- deckleberryfrye@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/word_scramble.rb
|
39
|
+
- lib/word_scramble/descrambler.rb
|
40
|
+
- lib/word_scramble/scrambled_word.rb
|
41
|
+
- lib/word_scramble/version.rb
|
42
|
+
- spec/descrambler_spec.rb
|
43
|
+
- spec/scrambled_word_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/word_checker_spec.rb
|
46
|
+
- spec/word_scramble_spec.rb
|
47
|
+
- word_scramble.gemspec
|
48
|
+
homepage: ''
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.10
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Written to solve the Word Scramble game that they give out on Virgin Airlines
|
72
|
+
when the flight is delayed.
|
73
|
+
test_files:
|
74
|
+
- spec/descrambler_spec.rb
|
75
|
+
- spec/scrambled_word_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
- spec/word_checker_spec.rb
|
78
|
+
- spec/word_scramble_spec.rb
|