Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.4K Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Constructing a string pattern

Posted 2 years ago

I am working on a helper function to use with the New York Times online puzzle called Spelling Bee. I have been going in circles for a while, but I can't seem to get the syntax right. Here is what I want to do.

Input: A string of English letters. Exact length doesn't matter. Assume 7. For example "wdleano"

Output: A variable containing a string pattern, such as I would obtain by explicitly typing

z := "w" | "d" | "l" | "e" | "a" | "n" | "o"

Then I can construct a selection function such as

StringMatchQ[#, z ..] &

to apply to a list of words (such as Wordlist[]).

Conceptually, I want to Riffle Characters[the input word] with the | operator, but I can't get the quotes to appear around the individual characters. I tried

Style[Characters["wdleano"], ShowStringCharacters -> True]

but then I get something with a head of Style rather than List, so I can't feed it to Riffle. And I'm not sure how to handle the | operator in Riffle.

I'm sure I'm overlooking something simple, but I'm new to the Wolfram language. Please help.

Thanks. ...RM

5 Replies
Posted 2 years ago

It is a good practice to separate input/output from computation. That permits more flexibility e.g. suppose you want to perform the computation using a string read from a file or some other source rather than user input, or write an automated test case. So I would modify the function to take letters as an argument

ClearAll@beeCandidates;
beeCandidates[letters_String] := Module[{pattern, firstCharacter},
  pattern = Alternatives @@ Characters[letters];
  firstCharacter = StringTake[letters, 1];
  Select[WordList[], StringMatchQ[#, pattern ..] && StringContainsQ[#, firstCharacter] && StringLength[#] > 3 &]]

beeCandidates["fetbind"]
POSTED BY: Rohit Namjoshi

For anyone interested, here is the final version. It's my first Wolfram Language function, so I'm sure it can be improved.

bee[] := Module[ {letters, z, k},

  letters = InputString[];

  z = Alternatives @@ Characters[letters];

  k = StringTake[letters, 1];

  Select[WordList[],     StringMatchQ[#, z ..] && StringContainsQ[#, k] && 

 StringLength[#] > 3 &]

    ]

Here is what the output looks like if the input string is fetbind:

In:= bee[]

Out = {"beef", "befit", "benefit", "biff", "defend", "define", "defined", \ "definite", "deft", "diffident", "edified", "effendi", "effete", \ "feed", "feint", "fend", "fete", "fetid", "fief", "fiend", "fife", \ "fifteen", "find", "fine", "finite", "fitted", "identified", \ "indefinite", "infinite", "tiff"}

Posted 2 years ago
Select[
  WordList[], 
  And[StringMatchQ[#, z ..], StringContainsQ[#, "w"], StringLength[#] > 3] &]
POSTED BY: Eric Rimbey

Hi, Eric -

Your suggestion seems to work. Here is how I am using it:

In: z = Alternatives @@ Characters["wdleano"]

Out: "w" | "d" | "l" | "e" | "a" | "n" | "o"

In: Select [Select[Select[WordList[], StringMatchQ[#, z ..] &],    StringMatchQ[#, ___ ~~ "w" ~~ ___] &], StringLength[#] > 3 &]

Out: {"allow", "anew", "awed", "dawdle", "dawn", "deadwood", "dowel", \ "down", "download", "dwell", "endow", "endowed", "lawn", "lowland", \ "newel", "owned", "waddle", "wade", "wale", "wall", "wallow", "wand", \ "wane", "weal", "wean", "weaned", "wedded", "weed", "weld", "well", \ "wend", "woad", "wold", "wood", "wooded", "wooden", "woodland", \ "wool", "woolen"}

I constructed the second StringMatch selector manually, but I think it's a small step to automate that.

Thanks. ...RM

Posted 2 years ago
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard