##Background - the Collatz Conjecture:

Imagine you are given a positive integer, `n`, then:

* if `n` is even, calculate: `n / 2`
* if `n` is odd, calculate: `3 * n + 1`

Repeat until your answer is `1`. The Collatz conjecture states that performing this operation repeatedly, you will always eventually reach `1`.

You can try creating Collatz sequences with [this](http://www.codewars.com/kata/5286b2e162056fd0cb000c20) kata. For further information, see the [wiki page](https://en.wikipedia.org/wiki/Collatz_conjecture).

##Now! Your task:

**Given an array of positive integers, return the integer whose Collatz sequence is the longest.**

Example:

```python
longest_collatz([2, 4, 3])==3
```

Explanation: The Collatz sequence for `2` has a length of `1`, the sequence for `4` has a length of `2`, and the sequence for `3` has a length of `7`. So from our array, the integer `3` is the one with the longest Collatz sequence.

Hence, your function should return `3`.

##Note:

There may be more than one answer, i.e. two or more integers produce the longest Collatz sequence, because they happen to have sequences of the same length. **In this case, your function should return the integer that appears first in the array.**

Example:
Given an array: `[2, 5, 32]`, both `5` and `32` have Collatz sequences of length 5. These are also the longest sequences from our array.

In this case, our function returns `5`, because `5` comes before `32` in our array.

Difficulty: introductory

Starter code:

def longest_collatz(input_array):
	

Solutions:
AVAILABLE_METHODS = {
    'even' : lambda x: x / 2,
    'odd': lambda x: 3 * x + 1
}

def generator(x):
    temp = x
    temp_num = 0
    while temp > 1:
        if temp % 2 == 0:
            temp = AVAILABLE_METHODS['even'](temp)
        else:
            temp = AVAILABLE_METHODS['odd'](temp)
        temp_num += 1
        yield temp_num

def longest_collatz(input_array):
    answer_list = [sum([item for item in generator(dig)]) for dig in input_array]
    print(max(answer_list), answer_list)
    return input_array[(answer_list.index(max(answer_list)))]

def collatz(n, t=1):
    return t if n == 1 else collatz(n / 2 if n % 2 == 0 else 3 * n + 1, t + 1)


def longest_collatz(input_array):
    return sorted(list(zip(list(map(collatz, input_array)), input_array)))[-1][1]

def longest_collatz(arr):
    return max([(gen_s(e),e) for  e in arr])[1]
    
def gen_s(n, c=1):
    return c + gen_s((n//2, n*3+1)[n%2], c+1) if n != 1 else 1

from functools import lru_cache

@lru_cache(maxsize=None)
def collatz(n):
    return n != 1 and 1 + collatz(3*n+1 if n&1 else n>>1)

def longest_collatz(input_array):
    return max(input_array, key=collatz)

def longest_collatz(a):
    def f(n):
        r = 1
        while n > 1:
            n = n * 3 + 1 if n % 2 else n // 2
            r += 1
        return r
    return max(a, key=f)

def longest_collatz(lst):
    return max(lst, key=seq_len)

def seq_len(n):
    count = 0
    while n > 1:
        n = (3 * n + 1) if n % 2 else (n // 2)
        count += 1
    return count
    
# two-liner
#seq_len = lambda n: 0 if n == 1 else 1 + seq_len(3 * n + 1 if n % 2 else n // 2)
#longest_collatz = lambda lst: max(lst, key=seq_len)




def collatz_len(n):
    result = 1
    while n != 1:
        if n % 2:
            n = 3 * n + 1
        else:
            n //= 2
        result += 1
    return result

def longest_collatz(input_array):
    return max(input_array, key=collatz_len)

def longest_collatz(input_array):
    results = {1: 1}
    
    for n in range(2, max(input_array) + 1):
        count = 0
        x = n
        while x not in results:
            x = x * 3 + 1 if x % 2 else x // 2
            count += 1
        
        count += results[x]
        results[n] = count
    
    return max(input_array, key=results.get)

def collatz(n):
    d = {1:1}
    count = 0
    while n != 1:    
        if n in d:
            count += d[n]
            n = 1
        else:
            if n % 2 == 0:
                n /= 2
                count += 1
            else:
                n = (3*n + 1)/2
                count += 2      
    return count 

def longest_collatz(input_array):
    return max(input_array, key=collatz)

def longest_collatz(input_array):
    def collatz_len(n):
        ctr = 0
        while n > 1:
            ctr, n = ctr + 1, (n // 2 if n % 2 == 0 else 3*n + 1)
        return ctr
    return max((n for n in input_array), key=collatz_len)