Find pattern with Position

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have a list a = L, S, L, L, S and I can find the position of of either L or S by:



Position[a, L, S]


But what if I want to find IF the a segment L,S exists?



Ideally, I would like to know how many such patterns exist and how many elements separate them.










share|improve this question

























    up vote
    3
    down vote

    favorite












    I have a list a = L, S, L, L, S and I can find the position of of either L or S by:



    Position[a, L, S]


    But what if I want to find IF the a segment L,S exists?



    Ideally, I would like to know how many such patterns exist and how many elements separate them.










    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I have a list a = L, S, L, L, S and I can find the position of of either L or S by:



      Position[a, L, S]


      But what if I want to find IF the a segment L,S exists?



      Ideally, I would like to know how many such patterns exist and how many elements separate them.










      share|improve this question













      I have a list a = L, S, L, L, S and I can find the position of of either L or S by:



      Position[a, L, S]


      But what if I want to find IF the a segment L,S exists?



      Ideally, I would like to know how many such patterns exist and how many elements separate them.







      list-manipulation pattern-matching






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      SuperCiocia

      347210




      347210




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          One way to approach this would be to partition the sequence into pairs and then find the positions of the pairs:



          ls = Position[Partition[a, 2, 1], L, S]


          which shows that the two L, Ss occur at the first and fourth positions. To find the separation between the occurrences:



          Differences[ls]


          To get the number of occurrences:



          Length[ls] 


          If there are no occurrences, then you will get 0.






          share|improve this answer





























            up vote
            2
            down vote













            SequenceCases[a, L, S]



            L, S, L, S




            SequencePosition[a, L, S]



            1, 2, 4, 5




            SequenceCount[a, L, S]



            2







            share|improve this answer




















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "387"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181951%2ffind-pattern-with-position%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              One way to approach this would be to partition the sequence into pairs and then find the positions of the pairs:



              ls = Position[Partition[a, 2, 1], L, S]


              which shows that the two L, Ss occur at the first and fourth positions. To find the separation between the occurrences:



              Differences[ls]


              To get the number of occurrences:



              Length[ls] 


              If there are no occurrences, then you will get 0.






              share|improve this answer


























                up vote
                2
                down vote













                One way to approach this would be to partition the sequence into pairs and then find the positions of the pairs:



                ls = Position[Partition[a, 2, 1], L, S]


                which shows that the two L, Ss occur at the first and fourth positions. To find the separation between the occurrences:



                Differences[ls]


                To get the number of occurrences:



                Length[ls] 


                If there are no occurrences, then you will get 0.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  One way to approach this would be to partition the sequence into pairs and then find the positions of the pairs:



                  ls = Position[Partition[a, 2, 1], L, S]


                  which shows that the two L, Ss occur at the first and fourth positions. To find the separation between the occurrences:



                  Differences[ls]


                  To get the number of occurrences:



                  Length[ls] 


                  If there are no occurrences, then you will get 0.






                  share|improve this answer














                  One way to approach this would be to partition the sequence into pairs and then find the positions of the pairs:



                  ls = Position[Partition[a, 2, 1], L, S]


                  which shows that the two L, Ss occur at the first and fourth positions. To find the separation between the occurrences:



                  Differences[ls]


                  To get the number of occurrences:



                  Length[ls] 


                  If there are no occurrences, then you will get 0.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 57 mins ago

























                  answered 1 hour ago









                  bill s

                  50.8k373144




                  50.8k373144




















                      up vote
                      2
                      down vote













                      SequenceCases[a, L, S]



                      L, S, L, S




                      SequencePosition[a, L, S]



                      1, 2, 4, 5




                      SequenceCount[a, L, S]



                      2







                      share|improve this answer
























                        up vote
                        2
                        down vote













                        SequenceCases[a, L, S]



                        L, S, L, S




                        SequencePosition[a, L, S]



                        1, 2, 4, 5




                        SequenceCount[a, L, S]



                        2







                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          SequenceCases[a, L, S]



                          L, S, L, S




                          SequencePosition[a, L, S]



                          1, 2, 4, 5




                          SequenceCount[a, L, S]



                          2







                          share|improve this answer












                          SequenceCases[a, L, S]



                          L, S, L, S




                          SequencePosition[a, L, S]



                          1, 2, 4, 5




                          SequenceCount[a, L, S]



                          2








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 49 mins ago









                          Henrik Schumacher

                          37.8k249108




                          37.8k249108



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181951%2ffind-pattern-with-position%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke