1

I want to make a module in Mathematica that return true if the first list (L1) is in the second list (L2) assuming that the length of L2 is greater than the length of L1. I did it in this way, the problem is that always return False and I don't know why. EDIT: I have solved a problem: I wrote "if" instead of "If". Now I get an infinitely loop.

isSegment[L1_List, L2_List] := Module[{i, j},   For[i = 1, i + Length[L1] - 1 <= Length[L2],
       For[j = 1, j <= Length[L1],
    If[L2[[i + j - 1]] != L1[[j]], Break;];
    j++;
    ];
       If[j == Length[L1] + 1,
    Return[ True];];
       i++;    ];   Return [False];   ]
Bart
  • 19,692
  • 7
  • 68
  • 77
tuket
  • 3,232
  • 1
  • 26
  • 41

2 Answers2

4

This is likely to be one of the cleanest and fastest general methods available:

isSegment[{L1__}, L2_List] := MatchQ[L2, {___, L1, ___}]

isSegment[{3, 4, 5}, Range@10]
True

For a list of all Real or Integer values you could adapt the methods shown here for maximum speed.


user writes:

Thanks, it is a good way, but what I want is to correct my code not to have a new one because it is an exercise for school and in the statement it is said that we must use For loops.

It appears that there is confusion over the syntax of Break[] and the function of Return. To correct the code I replaced Break with Break[] and Return with Throw & Catch.

isSegment[L1_List, L2_List] :=
 Catch @ Module[{i, j},
   For[i = 1, i + Length[L1] - 1 <= Length[L2],
    For[j = 1, j <= Length[L1], 
     If[L2[[i + j - 1]] != L1[[j]], Break[];]; j++;];
    If[j == Length[L1] + 1, Throw[True];];
    i++;]; Throw[False];
   ]
Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • Thanks, it is a good way, but what I want is to correct my code not to have a new one because it is an exercise for school and in the statement it is said that we must use For loops. – tuket Oct 18 '12 at 11:05
  • @user1754322 If this is an exercise specifically with *Mathematica* that is a travesty as `For` loops are rarely the way to go in *Mathematica*. Please, in the future specify such restrictions within your question. – Mr.Wizard Oct 18 '12 at 11:08
  • @user1754322 I have appended what I believe is a corrected version of your code. I made no effort to check the algorithm. – Mr.Wizard Oct 18 '12 at 11:14
  • Thanks! that is rigth what i needed. I am sorry for not specifiying the restrictions of the problems I will do it in the future. One question, Return[] is only for functions or can be used also in modules? – tuket Oct 18 '12 at 11:28
  • @user1754322 I'm glad I could help. Since this appears to be your first question on StackOverflow I'll mention that it is polite to "Accept" an answer that is fully satisfactory by clicking the green tick-mark next to it, but also: (1) there is no obligation to Accept an answer (2) you can change or remove your Accept at any time (3) it often produces more or better answers to wait a day before Accepting one, unless you believe that no better answer is possible. – Mr.Wizard Oct 18 '12 at 11:31
  • I didn't know it but I think no better answer is possible. – tuket Oct 18 '12 at 11:41
1

I'd normally go about this problem using something like this:

SegmentQ[l1_List, l2_List] := MemberQ[Partition[l2, Length @ l1, 1], l1]
einbandi
  • 275
  • 1
  • 6