#------------------------TD5------------------------#


#####################[Code 1]##########################

def longestConsecutive(A):
    _max, count = 1, 1
    for i in range(1, len(A)):
        if A[i] == (A[i-1] + 1):
            count += 1
        else:
            _max = max(_max, count)
            count = 1
    return _max

print( longestConsecutive( [1,2,4,5,6,7,11,12,17] ) )



#####################[Code 2]##########################

def longestWord(s):
    maxcount = 0
    maxword = ""

    currentcount = 0
    currentword = ""
    for i in range(0, len(s)):
        if s[i] == " " or i+1 == len(s):
            if currentcount > maxcount:
                maxcount = currentcount
                maxword = currentword
            currentcount = 0
            currentword = ""
        else:
            if (s[i] != "'" and s[i] != "?" and 
                s[i] != "." and s[i] != "," and s[i] != "!") : 
                currentcount = currentcount + 1
            currentword = currentword + s[i]
    return maxword
    

s = "Hello, how are you feeling today???"
print( longestWord(s) )



#####################[Code 3]##########################

def mysplit(A):
    B = []
    start = 0
    end = 0

    i = 0
    while True:
        while i < len(A) and A[i] == " ":
            i = i + 1
            
        if i == len(A):
            return B
               
        start = i
        while i < len(A) and A[i] != " ":
            i = i + 1
        end = i
        B.append(A[start:end])

C = mysplit("   asdf 8gas afbnn   a ")
print(C)



#####################[Code 4]##########################

import random

def partition(A, x):
    S = []
    B = []
    for i in range(0, len(A)):
        if A[i] <= x:
            S.append(A[i])
        else:
            B.append(A[i])
    return S, B

A = [ random.randint(0,50) for _ in range(0, 10) ]
print("A: ", A, "\nB, C: ", partition(A, 20))



#####################[Code 5]##########################

def to_binary_string(x):
    s = ''
    while x != 0:
        if x%2 == 1:
            s = '1' + s
        else:
            s = '0' + s
        x = x // 2
    return s

print( to_binary_string(11) )



#####################[Code 6]##########################

def addvectors(u,v):
    if len(u) != len(v):
        return
    
    output = [0] * len(u)
    for i in range(0, len(u)):
        output[i] = u[i] + v[i]
        
    return output



#####################[Code 7]##########################

def dotproduct(u,v):
    if len(u) != len(v):
        return
    
    output = 0
    for i in range(0, len(u)):
        output += u[i] * v[i]
        
    return output



#####################[Code 8]##########################

def multiplyvectors(M,v):
    output = []
    for i in range(0, len(M)):
        if len(M[i]) != len(v):
            return
        val = 0
        for j in range(0, len(M[i])):
            val += M[i][j] * v[j]
        output.append(val)
    return output



#####################[Code 9]##########################

def addmatrices(M1, M2):
    if len(M1) != len(M2):
        return
    
    output = []
    for i in range(0, len(M1)):
        output.append( [0]*len(M1[i]) )
        for j in range(0, len(M1[i])):
            output[i][j] = M1[i][j] + M2[i][j]
    return output



#####################[Code 10]##########################

def squareNumbers(A):
    pos = 0
    while pos < len(A) and A[pos] < 0:
        pos += 1

    neg = pos - 1

    B = []
    for _ in range(0, len(A)):
        if (pos >= len(A) or (neg >= 0 and pos < len(A) and A[neg]**2 <= A[pos]**2)):
            B.append(A[neg]**2)
            neg -= 1
        elif (neg < 0 or (neg >= 0 and pos < len(A) and A[neg]**2 > A[pos]**2)):
            B.append(A[pos]**2)
            pos += 1
        else:
            print("ERROR: this case should never happen!")

    return B


A = [-4,-1,0,3,10]
print( squareNumbers(A) )



#####################[Code 11]##########################

def squareNumbers(A):
    s, e = 0, len(A)-1
    B = [0]*len(A)

    i = len(A)-1
    while i >= 0:
        if A[e]**2 > A[s]**2:
            B[i] = A[e]**2
            e -= 1
        else:
            B[i] = A[s]**2
            s += 1
        i -= 1

    return B

A = [-4,-1,0,3,10]
print( squareNumbers(A) )



#####################[Code 12]##########################

import random

def findTwoFast(A, x):
    s, e = 0, len(A)-1

    while s != e:
        if A[s]+A[e] == x:
            return s, e
        elif A[s]+A[e] < x:
            s += 1
        else:
            e -= 1
    return None

n = 20
x = 19
A = sorted([ random.randint(0,50) for _ in range(0, n) ])

print("A: ", A)
print("x: ", x)
print("findTwoFast: ", findTwoFast(A,x))



