Group Abstract Group Abstract

Message Boards Message Boards

0
|
7K Views
|
4 Replies
|
1 Total Like
View groups...
Share
Share this post:

Function vs one line, What am I missing?

Posted 10 years ago

rose2 := {{z, 0, 1, 2, 3, 4}, {y, j, k, l, m, 5}, {x, i, b, c, n, 6}, {w, h, a, d, o, 7}, {v, g, f, e, p, 8}, {u, t, s, r, q, 9}};

roseEncode[str_] := Module[{strIn := str, toEn, cLst, rosePos}, toEn := StringCases[ToLowerCase[strIn], RegularExpression["[a-z,0,9]+"]];

cLst := Characters[StringJoin[toEn]];

rosePos := FirstPosition[rose2, #] &/@ cLst];

Return[Partition[rosePos, 2]]; ];

Print["Rose Encode: ", roseEncode["safe"]];

bark := FirstPosition[rose2, #] - 1 & /@ {s, t, a, r, t};

Print["Line Encode: ", bark]

Output: Rose Encode: {{Missing[NotFound],Missing[NotFound]},{Missing[NotFound],Missing[NotFound]}} Line Encode: {{5,2},{5,1},{3,2},{5,3},{5,1}}

The one liner works but the function doesn't. What am I missing? I have printed out cLst in the function and it is {s,t,a,r,t} just like in the one liner, I've tried replacing /@ with Map[rose2,cLst] with the same result. I also tried to Debug but have not figured out how to step through the Map and see each variable. Any help would be appreciated.

Mark

Attachments:
POSTED BY: Mark Henwood
4 Replies
Posted 10 years ago

Mark, it's not clear to me whether you understood my comment. The reason your one-liner works and your roseEncode method doesn't is that your rose2 structure contains no strings. Your one-liner is applied to Symbols, and rose2 does contain some matching Symbols. Your roseEncode function is applied to a String, and rose2 contains no Strings (no Characters), so it reports Missing. In other words, both your one-liner and your function are working.

To simplify your function, let's start with your hard-coded one-liner (slightly simplified for clarity):

FirstPosition[rose2, #] & /@ {s, t, a, r, t}

The algorithm is clear, we just need to replace the specific, hard-coded values with variables. Basically, you're trying to create a FirstPosition function that works with a list of patterns (FirstPosition would look for an actual list if you passed it a list as a pattern). Let's name it FirstPositions.

FirstPositions[structure_, patterns_List] := FirstPosition[structure, #] & /@ patterns

By calling out _List this way, I can create an alternate version that works with a string.

FirstPositions[structure_, patterns_String] := FirstPositions[structure, Characters[patterns]]

We can check how FirstPositions works:

In[8]:= FirstPositions[rose2, {s, t, a, r, t}]
Out[8]= {{6, 3}, {6, 2}, {4, 3}, {6, 4}, {6, 2}}

In[9]:= FirstPositions[rose2, "start"]
Out[9]= {Missing["NotFound"], Missing["NotFound"], 
 Missing["NotFound"], Missing["NotFound"], Missing["NotFound"]}

In[10]:= FirstPositions[Map[ToString, rose2, {-1}], "start"]
Out[10]= {{6, 3}, {6, 2}, {4, 3}, {6, 4}, {6, 2}}

From here, you can add the checking you want (ignoring spaces, normalizing to lowercase, etc).

POSTED BY: Eric Rimbey
Posted 10 years ago

Thanks Eric, You were right on the money. When I quoted the characters it worked. Thanks again for the help. Mark

POSTED BY: Mark Henwood
Posted 10 years ago
POSTED BY: Mark Henwood
Posted 10 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