Categories:Viewed: 5 - Published at: a few seconds ago

Write a recursive python function that performs merge sort on the given list or tuple or string and returns the new sorted sequence

def sort_and_merge(l1, l2):
    new_list = []
    i = 0
    j = 0
    l1_len = len(l1)
    l2_len = len(l2)
    while (i <= l1_len-1) and (j <= l2_len-1):
        if l1[i] < l2[j]:
            new_list.append(l1[i])
            i +=1
        else:
            new_list.append(l2[j])
            j +=1
    if i <= (l1_len-1):
        new_list += l1[i:]
    if j <= (l2_len-1):
        new_list += l2[j:]
    return new_list

def recursive_merge_sort(list_to_be_sorted):
    final_list = []
    first = 0
    last = len(list_to_be_sorted)
    if last <= 1:
        final_list.extend( list_to_be_sorted )
    else:
        mid = last // 2
        l1 = recursive_merge_sort( list_to_be_sorted[:mid] )
        l2 = recursive_merge_sort( list_to_be_sorted[mid:] )
        final_list.extend( sort_and_merge( l1, l2 ) )
    return final_list

Write a function to return the mean of numbers in a list

def cal_mean(num_list:list)->float:
    if num_list:
        return sum(num_list)/len(num_list)
    else:
        return None

Write a function to return the median of numbers in a list

def cal_median(num_list:list)->float:
    if num_list:
        if len(num_list)%2 != 0:
            return sorted(num_list)[int(len(num_list)/2) - 1]
        else:
            return (sorted(num_list)[int(len(num_list)/2) - 1] + sorted(num_list)[int(len(num_list)/2)])/2
    else:
        return None

Write a function to return the area of triangle by heros formula

def cal_triangle_area(a:float,b:float,c:float)->float:
    if a or b or c:
        s = (a+b+c)/2
        if s>a and s>b and s>c:
            area = (s*(s-a)*(s-b)*(s-c))**(1/2)
            return round(area,2)
        else:
            return None
    return None

Write a function to return the area of a equilateral triangle

def cal_eq_triangle_area(a:float)->float:
    if a:
        return (3**(1/2))*(a**2)/4
    else:
        return None

Write a function to return the area of a right angle triangle

def cal_rt_triangle_area(base:float,height:float)->float:
    if base and height:
        return (base*height)/2
    else:
        return None

Write a function to return the cartisian distance of a point from origin

def cal_dist_from_orign(x:float,y:float)->float:
    return (x**2+y**2)**(1/2)

Write a function to return the cartisian distance between two points

def cal_cart_distance(x1:float,y1:float,x2:float,y2:float)->float:
    return ((x1-x2)**2+(y1-y2)**2)**(1/2)

Write a function to return the type roots of a quadratic equation ax**2 + bx + c = 0

def root_type(a:float,b:float,c:float):
    if b**2-4*a*c >= 0:
        return 'real'
    else:
        return 'imaginary'

Write a function to return the sum of the roots of a quadratic equation ax**2 + bx + c = 0

def sum_of_roots(a:float,c:float):
    if a:
        return c/a
    else:
        return None