Group Abstract Group Abstract

Message Boards Message Boards

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

Constructing a string pattern

Posted 2 years ago
5 Replies

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

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
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years ago

The expression

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

is shorthand for

Alternatives["w", "d", "l", "e", "a", "n", "o"]

Here is a way to get there:

input = "wdleano";
chars = Characters[input];(* {"w", "d", "l", "e", "a", "n", "o"} *)
pattern = Alternatives @@ chars;(* "w" | "d" | "l" | "e" | "a" | "n" | "o" *)

But at this point I didn't understand how you wanted to apply this, because this pattern will match individual characters, but not longer words. So, I'd need more context to help with the next step.

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