# Task

Given an array of integers, sum consecutive even numbers and consecutive odd numbers. Repeat the process while it can be done and return the length of the final array.

# Example

For `arr = [2, 1, 2, 2, 6, 5, 0, 2, 0, 5, 5, 7, 7, 4, 3, 3, 9]`
 
The result should be `6`.

```
[2, 1, 2, 2, 6, 5, 0, 2, 0, 5, 5, 7, 7, 4, 3, 3, 9]  -->
         2+2+6       0+2+0     5+5+7+7       3+3+9
[2, 1,   10,    5,    2,        24,     4,   15   ] -->
                               2+24+4
[2, 1,   10,    5,             30,           15   ]
The length of final array is 6
```

# Input/Output

- `[input]` integer array `arr`

  A non-empty array, 

  `1 ≤ arr.length ≤ 1000`

  `0 ≤ arr[i] ≤ 1000`


- `[output]` an integer

  The length of the final array

Source: codewars | Difficulty: EASY
Tags: Data structures

Starter code: def sum_groups(arr):
	

Solutions:
from itertools import groupby

def sum_groups(arr):
	newarr = [sum(j) for (i, j) in groupby(arr, key=lambda x: x % 2 == 0)]
	return len(newarr) if newarr == arr else sum_groups(newarr)


def sum_groups(arr):
	(lenArr, arr) = (-1, arr[:])
	while lenArr != len(arr):
		(parity, lenArr, next) = (-1, len(arr), [])
		while arr:
			val = arr.pop()
			if parity != val % 2:
				next.append(val)
				parity = val % 2
			else:
				next[-1] += val
		arr = next
	return len(arr)


from itertools import groupby

def sum_groups(lst):
	l = -1
	while l != len(lst):
		(l, lst) = (len(lst), [sum(g) for (_, g) in groupby(lst, key=lambda n: n % 2)])
	return l


from itertools import groupby

def sum_groups(arr):
	reduced = [sum(gp) for (_, gp) in groupby(arr, key=lambda x: x % 2)]
	return len(reduced) if reduced == arr else sum_groups(reduced)


from itertools import groupby

def sum_groups(arr):
	while True:
		n = len(arr)
		arr = [sum(grp) for (key, grp) in groupby(arr, key=lambda x: x % 2)]
		if len(arr) == n:
			return n


from itertools import groupby

def sum_groups(arr):
	nums = list(arr)
	while True:
		tmp = [sum(g) for (_, g) in groupby(nums, key=lambda a: a % 2)]
		if nums == tmp:
			return len(nums)
		nums = tmp


from itertools import groupby, dropwhile, tee, islice

def sum_groups(arr):

	def it(a):
		while 1:
			yield a
			a = [sum(g) for (_, g) in groupby(a, key=lambda k: k % 2)]
	(it1, it2) = tee(it(arr))
	return len(next(dropwhile(lambda x: x[0] != x[1], zip(it1, islice(it2, 1, None))))[1])


from itertools import groupby

def sum_groups(arr):
	while True:
		res = [sum(i) for (_, i) in groupby(arr, lambda x: x & 1)]
		if arr == res:
			return len(arr)
		arr = res


import itertools

def sum_groups(arr):
	prev_len = 1
	while prev_len != len(arr):
		prev_len = len(arr)
		arr = [sum(g) for (k, g) in itertools.groupby(arr, key=lambda x: x % 2 == 0)]
	return len(arr)


import re

def sum_groups(arr):
	return len(re.sub('0{2,}', '0', re.sub('1+', lambda match: '1' if len(match.group(0)) % 2 else '0', ''.join((str(elem % 2) for elem in arr)))))