Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Here are the changes I was talking about. #1

Open
wants to merge 1 commit into from

1 participant

James Strong
James Strong

Added more flexibility allowing for use of --contains without needing --at. Made --starts-with, --ends-with and --contains add to the letter pool instead of requiring letters to be present in the letter pool.

James Strong laspluviosillas Added more flexibility allowing for use of --contains without needing…
… --at. Made --starts-with, --ends-with and --contains add to the letter pool instead of requiring letters to be present in the letter pool.
b917bd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 09, 2011
James Strong laspluviosillas Added more flexibility allowing for use of --contains without needing…
… --at. Made --starts-with, --ends-with and --contains add to the letter pool instead of requiring letters to be present in the letter pool.
b917bd8
This page is out of date. Refresh to see the latest.
16 README.md
Source Rendered
@@ -40,11 +40,11 @@ You can, of course, combine these if you want:
40 40 This is pretty self explanatory. If you only want words that start with "th",
41 41 you would do something like this:
42 42
43   - $ scrabble-solver tehre --starts-with th
  43 + $ scrabble-solver ree --starts-with th
44 44
45 45 And the same goes for ends-with:
46 46
47   - $ scrabble-solver tehre --ends-with re
  47 + $ scrabble-solver teh --ends-with re
48 48
49 49 ## Blank tiles (wildcards)
50 50
@@ -61,11 +61,15 @@ will register fine wherever you put them and you can have as many as you want.
61 61 Say you've got a really sweet triple letter lined up that will only work if
62 62 your Z is the first letter in the word, check this out:
63 63
64   - $ scrabble-solver zloogsti --contains z --at 1
  64 + $ scrabble-solver loogsti --contains z --at 1
65 65
66   -The `--contains` and `--at` flags must be used in conjunction. They will not
67   -do anything if used separately. They will only return words that have the
68   -specified pattern at the specified location.
  66 +The `--contains` flag works without the `--at` flag as well (but not vice versa).
  67 +If you want to search for a word that contains a specific sequence of letters at
  68 +any position, use `--contains` without specifing `--at`.
  69 +
  70 + $ scrabble-solver pers --contains li
  71 +
  72 +Returns words like 'lisp' and 'pliers'.
69 73
70 74 # Word lists
71 75
26 lib/scrabble-solver/solver.rb
@@ -52,9 +52,14 @@ def self.word_list
52 52 # # => An array of words that the tiles t, h, e, r, e plus a blank tile
53 53 # # could make.
54 54 def self.words_for letters, options = Hash.new(nil)
55   - letters = letters.downcase.split(//)
  55 + letters = letters.downcase.chars.to_a
56 56 unknowns = letters.count "?"
57 57 letters.delete "?"
  58 +
  59 + # Add letters from options to the letter pool for word searching.
  60 + letters = letters + options[:starts_with].chars.to_a if options[:starts_with]
  61 + letters = letters + options[:ends_with].chars.to_a if options[:ends_with]
  62 + letters = letters + options[:contains].chars.to_a if options[:contains]
58 63
59 64 # Set a new word file if the option has been specified
60 65 if options[:word_file]
@@ -63,11 +68,11 @@ def self.words_for letters, options = Hash.new(nil)
63 68
64 69 words = word_list.keep_if do |word|
65 70 # Split the word into its letters.
66   - word = word.split(//)
  71 + word = word.chars.to_a
67 72
68 73 # Strip the letters that are in our hand from the word.
69 74 letters.each do |letter|
70   - unless word.index(letter).nil?
  75 + if word.index(letter)
71 76 word.delete_at word.index(letter)
72 77 end
73 78 end
@@ -76,7 +81,7 @@ def self.words_for letters, options = Hash.new(nil)
76 81 # of unknowns.
77 82 word.length == unknowns
78 83 end
79   -
  84 +
80 85 # Filter only words that start with a specific sequence.
81 86 if options[:starts_with]
82 87 words.keep_if { |word| word.start_with? options[:starts_with] }
@@ -97,10 +102,17 @@ def self.words_for letters, options = Hash.new(nil)
97 102 words.keep_if { |word| word.length > options[:longer_than].to_i }
98 103 end
99 104
100   - # Filter words that contain a specific sequence at a given 1-based index.
101   - if options[:contains] and options[:at]
  105 +
  106 + if options[:contains]
102 107 words.keep_if do |word|
103   - word[options[:at].to_i - 1, options[:contains].length] == options[:contains]
  108 + # If 'at' is specified, filter words that contain a specific sequence
  109 + # at a given 1-based index. Else, filter words that contain a specific
  110 + # sequence at - any - position.
  111 + if options[:at]
  112 + word[options[:at].to_i - 1, options[:contains].length] == options[:contains]
  113 + else
  114 + word.scan(options[:contains]).any?
  115 + end
104 116 end
105 117 end
106 118
14 spec/scrabble-solver/solver_spec.rb
@@ -107,17 +107,17 @@ module Scrabble
107 107 end
108 108
109 109 it "should be able to use a start_with argument successfully" do
110   - words = `#{executable} geg --starts-with eg`.split(/\n/)
  110 + words = `#{executable} g --starts-with eg`.split(/\n/)
111 111 words.should include "egg"
112 112 end
113 113
114 114 it "should be able to use an ends_with argument successfully" do
115   - words = `#{executable} pngi --ends-with ing`.split(/\n/)
  115 + words = `#{executable} spsi --ends-with ng`.split(/\n/)
116 116 words.should include "ping"
117 117 end
118 118
119 119 it "should be able to use a combination of starts_with and ends_with" do
120   - words = `#{executable} crcak --starts-with cr --ends-with k`.split(/\n/)
  120 + words = `#{executable} apcz --starts-with cr --ends-with k`.split(/\n/)
121 121 words.should include "crack"
122 122 end
123 123
@@ -153,7 +153,7 @@ module Scrabble
153 153
154 154 it "should be able to return only words that have a specific letter at " +
155 155 "a given index" do
156   - words = `#{executable} diegtlkwj --contains i --at 2`.split(/\n/)
  156 + words = `#{executable} degtlkwj --contains i --at 2`.split(/\n/)
157 157 words.each do |word|
158 158 word[1].should == "i"
159 159 end
@@ -171,6 +171,12 @@ module Scrabble
171 171 # Ensure that some words were actually checked in the above loop.
172 172 words.length.should be > 0, "No words scanned."
173 173 end
  174 +
  175 + it "should be able to return words that contain a sequence at any position" do
  176 + words = `#{executable} pers --contains li`.split(/\n/)
  177 + words.should include "pliers"
  178 + words.should include "lisp"
  179 + end
174 180
175 181 it "should be able to take a new word file if specified" do
176 182 words = `#{executable} ???? --word-file #{test_word_file}`.split(/\n/)

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.