Use a Scala collection method to help convert a list of [0,0,0,1,1,1,1,0,0,1,1] into [3,4,2,2]

Phil

So I have a list of 0's and 1's, I want to find the count of each element and output this to a list. I can think of a recursive way to do it with functions but is there any helper functions which can help to convert this?

I believe groupBy could be useful but it seems to group all the elements into one partition or another, not into the way I want.

I want to have a list of the count of numbers until each transition from 0 to 1 and 1 to 0. ie, if we have 0,0,0, .. ok we counted 3 zeros so remember 3, then we have 1,1,1,1 so we counted 4 1's, so we remember 4, so far we have a list of [3,4...] and so on

tiran

This might be a little complicated. but I'd go with it.

scala> implicit class ListHelper[A](ls:List[A]) {
         def partitionBy(f: (A, A) => Boolean) = if (ls.isEmpty) List.empty[Int] 
           else (ls zip (ls.head :: ls)).foldLeft(List.empty[Int]){
             case (Nil, _) => List(1)
             case (x :: xs, (a, b)) => if (a == b) (x + 1) :: xs else 1 :: x :: xs
         }.reverse
       }
defined class ListHelper

scala> List(0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1).partitionBy(_ == _)
res27: List[Int] = List(3, 4, 2, 2)

This is based on the clojure function partition-by

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python convert list [0, 1, 2, 3, 4, 5] to [0, 1, 2], [1,2,3], [2,3,4]

How can I convert this [['1 0'], ['2 0'], ['3 1 2']] into an adjacency list in python

This is the inputlist input_list =[1,0,3,2,0,1,0] and I want output_list as [1,1,2,3,0,0,0] How to solve this?

Convert a list that intervals start and stop with [1,0,-1] into a step function [0, 1]

Given a list of random numbers [0, 1]: can I use the standard library to convert to a specific distribution?

Is there library method to convert String "1, 2, 3, 4" or "1 2 3 4" to array of integers or List<Integer>?

Produce the infinite list [0, 1, -1, 2, -2, ... in Haskell

Considering a list a=["boo",[1,2,3]], why does print(a[0:2][0:1]) prints ['boo'] whereas print(a[0:2][0:2]) prints ['boo',[1,2,3]]?

How to use Python 'in' operator to check my list/tuple contains each of the integers 0, 1, 2?

Bitwise operator to convert 0 to 1 and 1 to 0

Convert String to Map Ex - String str = "{Topic0 : 0, Topic1 :1, Topic2 : 0}";

Convert list of lists of 1 and 0 in black and white image in Python

how to convert list of values of a coulmn and assign it 0 or 1

convert list element to 1 and 0 based on conditions in python

Compressing list[0], list[1], list[2],... into a simple statement

Converting (0,1,0, 0, 1, 1, 1) to (0, 0, 0, 1, 0, 1, 2) in R

Why does the Unix permission system use 1 2 3 4... values instead of 1 or 0?

Convert dataframe to 1 and 0

Convert genotypes to 0/1

get index at 0,1,list/2,(list/2+1),2,3

convert 0/1 matrix to a 2D grid graph in python

How to use replace method on multiple columns col0,col1,col2 with multiple conditons

How to get ['1 0', '2 0', '3 1 2'] to be [[1, 0], [2,0] , [3,1] , [ 3,2] ] in so I can unpack the values. Python. Adjacency list

Most computationally efficient way to build a Python list with repeating and iterating numbers like [0, 0, 0, 1, 1, 1, 2, 2, 2 . . . ,etc]

Swift 4 - Convert 1 or 0 from mysql to bool

indices = 2 is not in [0, 1)

Create the sequence 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4 with seq()

How to output an array with a[0] - a[0]a[1] - a[0]a[1]a[2]

Is there any reason to use (nr & 1 == 0) over (nr % 2 == 0) to check for parity?