#Unflatten a list (Easy)

There are several katas like "Flatten a list".
These katas are done by so many warriors, that the count of available list to flattin goes down!

So you have to build a method, that creates new arrays, that can be flattened!

#Shorter: You have to unflatten a list/an array.

You get an array of integers and have to unflatten it by these rules:
```
- You start at the first number.
- If this number x is smaller than 3, take this number x direct 
  for the new array and continue with the next number.
- If this number x is greater than 2, take the next x numbers (inclusive this number) as a 
  sub-array in the new array. Continue with the next number AFTER this taken numbers.
- If there are too few numbers to take by number, take the last available numbers.
```

The given array will always contain numbers. There will only be numbers > 0.

Example:
```
 [1,4,5,2,1,2,4,5,2,6,2,3,3] -> [1,[4,5,2,1],2,[4,5,2,6],2,[3,3]]

Steps: 
1. The 1 is added directly to the new array.
2. The next number is 4. So the next 4 numbers (4,5,2,1) are added as sub-array in the new array.
3. The 2 is added directly to the new array.
4. The next number is 4. So the next 4 numbers (4,5,2,6) are added as sub-array in the new array.
5. The 2 is added directly to the new array.
6. The next number is 3. So the next 3 numbers would be taken. There are only 2, 
   so take these (3,3) as sub-array in the new array.
```

There is a harder version of this kata!
Unflatten a list (Harder than easy)

Have fun coding it and please don't forget to vote and rank this kata! :-) 

I have created other katas. Have a look if you like coding and challenges.

Source: codewars | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: def unflatten(flat_array):
	

Solutions:
def unflatten(flat_array):
	arr = flat_array[:]
	for (i, v) in enumerate(arr):
		if v > 2:
			(arr[i], arr[i + 1:i + v]) = (arr[i:i + v], [])
	return arr


def unflatten(flat):
	if not flat:
		return []
	if flat[0] < 3:
		return [flat[0]] + unflatten(flat[1:])
	else:
		return [flat[0:flat[0]]] + unflatten(flat[flat[0]:])


from itertools import islice

def unflatten(a):
	it = iter(a)
	return [x if x < 3 else [x] + list(islice(it, 0, x - 1)) for x in it]


def unflatten(flat_array):
	result = []
	i = 0
	while i < len(flat_array):
		num = flat_array[i]
		if num <= 2:
			result.append(num)
			i += 1
		else:
			result.append(flat_array[i:i + num])
			i += num
	return result


def unflatten(flat_array):
	new_array = []
	i = 0
	while i < len(flat_array):
		if flat_array[i] < 3:
			new_array.append(flat_array[i])
			i += 1
		else:
			new_array.append(flat_array[i:i + flat_array[i]])
			i += flat_array[i]
	return new_array


def unflatten(flat_array):
	result = []
	index = 0
	while index < len(flat_array):
		value = flat_array[index]
		if value < 3:
			result.append(value)
			index += 1
		else:
			result.append(flat_array[index:index + value])
			index += value
	return result


def unflatten(flat_array):
	dex = 0
	result = []
	while dex < len(flat_array):
		current = flat_array[dex]
		if current > 2:
			result.append(flat_array[dex:dex + current])
			dex += current
		else:
			result.append(current)
			dex += 1
	return result


def unflatten(flat):
	a = []
	while flat:
		(a, flat) = (a + [flat[0]], flat[1:]) if flat[0] < 3 else (a + [flat[:flat[0]]], flat[flat[0]:])
	return a


def unflatten(arr):
	j = 0
	res = []
	for x in arr:
		if j > 0:
			res[-1].append(x)
			j -= 1
		elif x < 3:
			res.append(x)
		else:
			res.append([x])
			j = x - 1
	return res