Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter out consecutive repeats in a sequence?

Tags:

sequence

raku

I'm trying to generate a Seq of random options where consecutive repeats are not allowed:

> (<F R U>.roll(*).grep(* ne *))[^10]
((R F) (F U) (R F) (F R) (U R) (R F) (R F) (U F) (R U) (U R))

Why does using grep in this way result in nested pairs? And how can I achieve what I'm looking for? Using .flat above will still allow consecutive repeats.

( R U F U F U R F R U ... )
like image 371
Zaid Avatar asked May 16 '21 12:05

Zaid


People also ask

How to filter out duplicates in a list and keep unique values?

Save 50% of your time, and reduce thousands of mouse clicks for you every day! You can apply the Advanced Filter feature to filter out duplicates in a list and only keep the unique values. Please do as follows. 1. Select the list you need to filter out duplicates, then click Data > Advanced. See screenshot: 2.

How do you find the start and end of a sequence?

Q: Given a sequence of random numbers, find the start and end positions of runs of five or more consecutive numbers that are greater than zero. A: Use the rle () function. For example, let’s apply rle () to the following sequence of numbers. seq3 = c(2,2,2,2,2,5,3,7,7,7,2,2,5,5,5,3,3,3) ( rle.seq3 = rle(seq3) )

What are the limitations of the filter function?

There are no limitations there. We do have dedicated training section on the FILTER function, including multiple criteria, in our Elevate Excel Training Program. One issue that will eventually arise with this solution is when columns are added or deleted from the source data table.

How do I filter out duplicates in Excel 2016?

Excel Filter out Duplicates 1 Select the list you need to filter out duplicates, then click Data > Advanced . 2 In the Advanced Filter dialog box, select Filter the list, in-place option in the Action section, check the Unique records only box, and then click the OK button. See More....


Video Answer


2 Answers

how can I achieve what I'm looking for?

I believe the squish method will do what you want:

say <F R U>.roll(*).squish.head(20)

This produces:

(U R F U F R F R F U F R U R F U R U R F)

Why does using grep in this way result in nested pairs?

Because grep (and map too) on something with an arity greater than 1 will work by breaking the list into chunks of that size. Thus given A B C D, the first call to the grep block gets A B, the second C D, and so on. What's really needed in this case, however, is a lagged value. One could use a state variable in the block passed to grep to achieve this, however for this problem squish is a much simpler solution.

like image 116
Jonathan Worthington Avatar answered Oct 23 '22 07:10

Jonathan Worthington


You could employ a simple regex using the <same> zero-width matcher. Below in the Raku REPL:

> my $a = (<F R U>.roll(*))[^20].comb(/\w/).join; say $a;
RRFUURFFFFRRFRFUFUUU

> say $/ if  $a ~~ m:g/ \w <!same> /;
(「R」 「F」 「U」 「R」 「F」 「R」 「F」 「R」 「F」 「U」 「F」 「U」)

https://docs.raku.org/language/regexes#Predefined_Regexes

like image 42
jubilatious1 Avatar answered Oct 23 '22 06:10

jubilatious1