summary helperfunction calculates the eulidean distance between vector x and y arguments x tuple vector y tuple vector summary implements the nearest neighbor algorithm arguments x tupel vector tset dict training set returns type result of the andfunction summary helper function calculates the eulidean distance between vector x and y arguments x tuple vector y tuple vector summary implements the nearest neighbor algorithm arguments x tupel vector tset dict training set returns type result of the and function
import math

def distance(x,y):
    
    assert len(x) == len(y), "The vector must have same length"
    result = ()
    sum = 0
    for i in range(len(x)):
        result += (x[i] -y[i],)
    for component in result:
        sum += component**2
    return math.sqrt(sum)


def nearest_neighbor(x, tSet):
    
    assert isinstance(x, tuple) and isinstance(tSet, dict)
    current_key = ()
    min_d = float('inf')
    for key in tSet:
        d = distance(x, key)
        if d < min_d:
            min_d = d
            current_key = key
    return tSet[current_key]