Find pattern with Position
Clash 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.
list-manipulation pattern-matching
add a comment |Â
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.
list-manipulation pattern-matching
add a comment |Â
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.
list-manipulation pattern-matching
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
list-manipulation pattern-matching
asked 1 hour ago
SuperCiocia
347210
347210
add a comment |Â
add a comment |Â
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.
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 57 mins ago
answered 1 hour ago
bill s
50.8k373144
50.8k373144
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
SequenceCases[a, L, S]
L, S, L, S
SequencePosition[a, L, S]
1, 2, 4, 5
SequenceCount[a, L, S]
2
answered 49 mins ago
Henrik Schumacher
37.8k249108
37.8k249108
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password