def sort_age(lst):
    n = len(lst)
    result = []
    while n != 0:
        test = []
        for counter in range(n):
            test.append(lst[counter][1])
        first = test(max)
        for counter in range(n):
            if lst[counter][1] == first:
                result.append(lst.pop(counter))
        n = len(lst)
    return result


def sort_age(lst):
    n = len(lst)
    result = []
    while n != 0:
        test = []
        for counter in range(n):
            test.append(lst[counter][1])
        first = max(test)
        for counter in range(n):
            if lst[counter][1] == first:
                result.append(lst.pop(counter))
        n = len(lst)
    return result

Task: Sorting Tuples

Can we sort items other than integers? For this question, you will be sorting tuples!
We represent a person using a tuple (<gender>, <age>). Given a list of people, write
a function sort_age that sorts the people and return a list in an order such that the older
people are at the front of the list. An example of the list of people is [("M", 23), ("F",
19), ("M", 30)]. The sorted list would look like [("M", 30), ("M", 23), ("F", 19)]. You
may assume that no two members in the list of people are of the same age.


def sort_age(lst):
    lst.sort(key=lambda x: x[1], reverse=True)
    return lst


def sort_age(lst):
    lst.sort(key=lambda x: x[1])
    return lst


def sort_age(lst):
    lst.sort(key=lambda x: x[1])
    lst.reverse()
    return lst


def sort_age(lst):
    lst.sort(key= lambda x: x[1])
    return lst


def sort_age(lst):
    lst.sort(key= lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    new_lst = []
    for i in range(len(lst)):
        max_num = max(lst)
        lst.remove(max_num)
        new_lst.append(max_num)
    return new_lst


def sort_age(lst):
  male = []
  female = []
  for i in range(len(lst)):
    if lst[i][0] == "M":
      male.append(lst[i])
    else:
      female.append(lst[i])
  male.sort()
  female.sort()
  combine = male[::-1] + female[::-1]
  return combine


def sort_age(lst):
    lst.sort(key = lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    a=[]
    while lst:
        for i in lst:
            if i==max(lst):
                a.append(i)
    return a


def sort_age(lst):
    a=[]
    for i in lst:
        if i==max(lst):
            a.append(i)
        continue
    return a


def sort_age(lst):
    a=[]
    for i in lst:
        if i[1]>lst[0][1]:
            a.append(i)
        continue
    return a


def sort_age(lst):
    a=[]
    for i in lst:
        if i[1]>lst[0][1]:
            a.append(i)
    return a


def sort_age(lst):
    a=[]
    while lst:
        biggest=lst[0]
        for i in lst:
            if i > biggest:
                biggest=i
        lst.remove(biggest)
        a.append(biggest)
    return a


def sort_age(lst):
    youngest = lst[0][1]
    for item in lst:
        if item[1] < youngest:
            youngest = item[1]
            lst.remove(item)
            lst = item + lst

    return lst


def sort_age(lst):
    youngest = lst[0][1]
    for item in lst:
        if item[1] < youngest:
            youngest = item[1]
            lst.remove(item)
            lst = [item,] + lst

    return lst


def sort_age(lst):
    oldest = lst[0][1]
    for item in lst:
        if item[1] > oldest:
            oldest = item[1]
            lst.remove(item)
            lst = [item,] + lst

    return lst


def sort_age(lst):
    a=[]
    while lst:
        biggest=lst[0]
        for i in lst:
            if i >= biggest:
                biggest=i
        lst.remove(biggest)
        a.append(biggest)
    return a


def sort_age(lst):
    compiled = []
    result = []
    for i in lst:
        compiled = compiled + [i[1]]
    compiled.sort()
    compiled.reverse()
    for i in compiled:
        for j in lst:
            if i == j[1]:
                result = result + [j]
    return result


def sort_age(lst):
    sort = []
    while lst: # a is not []
        smallest = (lst[0])[1]
        for element in lst:
            if element[1] < smallest:
                smallest = element
        lst.remove(smallest)
        sort.append(smallest)
    return lst


def sort_age(lst):
    sorted = []
    while lst:
        oldest = lst[0]
        for element in lst:
            if element[1] > oldest[1]:
                oldest = element
            lst.remove(oldest)
            sorted.append(oldest)
    return sorted


def sort_age(lst):
    return lst.sort(key=lambda x:x[1])


def sort_age(lst):
    def age(i):
        return i[1]

    def position(seq, ele):
        n = len(seq)
        for i in range(n):
            if seq[i] == ele:
                return i

    def largest_age(seq):
        largest = age(seq[0])
        largest_pos = 0
        for i in seq:
            if age(i) > largest:
                largest = age(i)
                largest_pos = position(seq,i)
        return seq[largest_pos]
    n = len(lst)
    if n ==0:
        return []
    elif n ==1:
        return lst
    else:
        return [largest_age(lst)]+[sort_age(lst[1:])]

    # Fill in your code here
    pass


def sort_age(lst):
    lst.sort(key=lambda x:x[1])
    lst.reverse()
    return lst


def sort_age(lst):
    sort = []
    while lst:
        oldest = list[0]
        for x in lst:
            if x[1] > oldest[1]:
                oldest = x
        a.remove(oldest)
        sort.append(oldest)
    return sort


def sort_age(lst):
    sort = []
    while lst:
        oldest = lst[0]
        for x in lst:
            if x[1] > oldest[1]:
                oldest = x
        a.remove(oldest)
        sort.append(oldest)
    return sort


def sort_age(lst):
    lst.sort(key=lambda x: x[1])
    lst.reverse()
    return lst


def sort_age(lst):
    list1 = ()
    i = 0
    smallest = lst[0][1]
    s = lst[0]
    for i in range(1,len(lst)):
        if lst[i][1] < smallest:
            smallest = lst[i][1]
            s = lst[i]
    list1 += s
    return list1


def sort_age(lst):
    list1 = []
    i = 0
    smallest = lst[0][1]
    s = lst[0]
    for i in range(1,len(lst)):
        if lst[i][1] < smallest:
            smallest = lst[i][1]
            s = lst[i]
    list1 += s
    return list1


def sort_age(lst):
    list1 = []
    i = 0
    smallest = lst[0][1]
    s = (lst[0],)
    for i in range(1,len(lst)):
        if lst[i][1] < smallest:
            smallest = lst[i][1]
            s = (lst[i],)
    list1 += s
    return list1


def sort_age(lst):
    list1 = []
    smallest = lst[0][1]
    s = (lst[0],)
    for i in range(1,len(lst)):
        if lst[i][1] < smallest:
            smallest = lst[i][1]
            s = (lst[i],)
    list1 += s
    return list1


def sort_age(lst):
    list1 = []
    smallest = lst[0][1]
    s = (lst[0],)
    for i in range(1,len(lst)):
        if lst[i][1] < smallest:
            smallest = lst[i][1]
            s = (lst[i],)
        list1 += s
    return list1


def sort_age(lst):
    list1 = []
    smallest = lst[0][1]
    s = (lst[0],)
    for j in range(1,len(lst)):

        for i in range(1,len(lst)-1):
            if lst[i][1] < smallest:
                smallest = lst[i][1]
                s = (lst[i],)
        list1 += s
    return list1


def sort_age(lst):
    list1 = []
    while lst:
        biggest = lst[0][1]
        b = lst[0]
        for i in range(1,len(lst)-1):
            if lst[i][1] > biggest:
                biggest = lst[i][1]
                s = (lst[i],)
        lst.remove(biggest)
        list1.append(biggest)
    return list1


def sort_age(lst):
    list1 = []
    while lst:
        biggest = lst[0][1]
        b = lst[0]
        for i in range(1,len(lst)):
            if lst[i][1] > biggest:
                biggest = lst[i][1]
                s = (lst[i],)
        lst.remove(biggest)
        list1.append(biggest)
    return list1


def sort_age(lst):
    list1 = []
    while lst:
        biggest = lst[0][1]
        b = lst[0]
        for i in range(1,len(lst)):
            if lst[i][1] > biggest:
                biggest = lst[i][1]
                b = (lst[i],)
        lst.remove(b)
        list1.append(b)
    return list1


def sort_age(lst):
    swap = True
    while swap:
        swap = False
        for tag in range(len(lst)-1):
            if lst[tag][1] < lst[tag+1][1]:
                lst[tag], lst[tag+1] = lst[tag+1], lst[tag]
                swap = True


def sort_age(lst):
    list1 = []
    while lst:
        biggest = lst[0]

        for i in range(1,len(lst)):
            if lst[i][1] > biggest[1]:
                biggest = lst[i][1]

        lst.remove(biggest)
        list1.append(biggest)
    return list1


def sort_age(lst):
    list1 = []
    while lst:
        biggest = lst[0]

        for i in range(1,len(lst)+1):
            if lst[i][1] > biggest[1]:
                biggest = lst[i][1]

        lst.remove(biggest)
        list1.append(biggest)
    return list1


def sort_age(lst):
    list1 = []
    while lst:
        biggest = lst[0]

        for i in lst:
            if i[1] > biggest[1]:
                biggest = i[1]

        lst.remove(biggest)
        list1.append(biggest)
    return list1


def sort_age(lst):
	lst.sort(key = lambda x: x[1], reverse = True)
	return lst


def sort_age(lst):
    newlst = []
    while lst:
        i = lst[0]
        for element in lst:
            if element[1] >= i[1]:
                i = n
        lst.remove(i)
        final.append(i)
    return newlst


def sort_age(lst):
    newlst = []
    while lst:
        i = lst[0]
        for element in lst:
            if element[1] >= i[1]:
                i = element
        lst.remove(i)
        final.append(i)
    return newlst


def sort_age(lst):
    a = list(set(lst))
    lst.clear()
    lst.append(a)
    return lst


def sort_age(lst):
    res = []
    age_list= []
    while lst:
        for i in range(len(lst)):
            age_list = age_list+ [lst[i][1]]
        for i in lst:
            if max(age_list) == i[1]:
                res= res + [i]
            else:
                res = res
        lst.remove(i)
        age_list.remove(i[1])
    return res


def sort_age(lst):
    lst.sort(key = lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    sort = []
    while lst:
        smallest = lst[0]
        for element in lst:
            if element[1] < smallest[1]:
                smallest = element
        lst.remove(smallest)
        sort.append(smallest)

    return sort


def sort_age(lst):
    lst.sort(key=lambda x:x[1],reverse=True)
    return lst


def sort_age(lst):
    s = []
    while lst:
        smallest = lst[0]
        for element in lst:
            if element[1]<smallest[1]:
                smallest = element
        lst.remove(smallest)
        s.append(smallest)
        s.reverse()
    return s


def sort_age(lst):
    # Fill in your code here
    return lst.sort(key=lambda x: x[1])


def sort_age(lst):
    lst.sort(key= lambda x: x[1], reverse=True)
    return lst


def sort_age(lst):
    for i in range(len(lst)-1):
        for a in range(i+1, len(lst)):
            if lst[i][1] < lst[a][1]:
                lst[i]= lst[a]
                lst[a]= lst[i]
    return lst


def sort_age(lst):
    while lst:
        smallest = lst[0][1]
        for x in lst:
            if x[1] < smallest:
                smallest = x
        lst.remove(smallest)
        sort.append(smallest)
        return lst


def sort_age(lst):
    # Fill in your code here
    sorted_list = []

    while a:
        oldest = lst[0]

        for element in lst:
            if element[1] > oldest:
                oldest = element[1]

        lst.remove(oldest)
        sorted_list.append(oldest)

    return sorted_list


def sort_age(lst):
    # Fill in your code here
    lst.sort(key=lambda x: x[1], reverse=True)
    return lst


def sort_age(lst):
    if len(lst) < 2:
        return lst
    midpoint = len(lst) // 2
    left = sort_age(lst[:midpoint])
    right = sort_age(lst[midpoint:])
    new_list = []
    while left and right:
        if left[0][1] < right[0][1]:
            new_list.append(right.pop(0))
        else:
            new_list.append(left.pop(0))
        new_list.extend(left)
        new_list.extend(right)
        return new_list


def sort_age(lst):
    if len(lst) < 2:
        return lst
    midpoint = len(lst) // 2
    left = sort_age(lst[:midpoint])
    right = sort_age(lst[midpoint:])
    new_list = []
    while left and right:
        if left[0][1] > right[0][1]:
            new_list.append(left.pop(0))
        else:
            new_list.append(right.pop(0))
        new_list.extend(left)
        new_list.extend(right)
        new_list.reverse
        return new_list


def sort_age(lst):
    new = []
    while lst:
        largest = lst[0]
        for ele in lst:
            if ele[1] > largest:
                largest = ele
        a.remove(largest)
        new.append(largest)
    return new


def sort_age(lst):
    new = []
    while lst:
        largest = lst[0]
        for ele in lst:
            if ele[1] > largest:
                largest = ele
        a.remove(largest)
        new.append(largest)
        return new


def sort_age(lst):
    sort=[]
    while lst:
        largest=lst[0][1]
        for i in lst:
            if i[1]>largest:
                largest=i[1]
        lst.remove(i)
        sort.append(i)
    return sort


def sort_age(lst):
    lst.sort(key=lambda x: x[1], reverse = True)
    return lst
    pass


def sort_age(lst):
    for i in range(len(lst)-1):
        if lst[i+1][1] < lst[i][1]:
            x = lst[i]
            lst[i] = lst[i+1]
            lst[i+1] = x
    return lst


def sort_age(lst):
    for i in range(len(lst)-1):
        if lst[i+1][1] > lst[i][1]:
            x = lst[i]
            lst[i] = lst[i+1]
            lst[i+1] = x
    return lst


def sort_age(lst):
    lst.sort(key = lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    for i in range(len(lst)-1):
        for j in range(len(lst)-1-i):
            if lst[j][1] > lst[j+1][1]:
                lst[j+1][1], lst[j][1] = lst[j][1], lst[j+1][1]
            else:
                continue
    return lst
    pass


def sort_age(lst):
    for i in range(len(lst)-1):
        for j in range(len(lst)-1-i):
            if lst[j][1] > lst[j+1][1]:
                lst[j][1], lst[j+1][1] = lst[j+1][1], lst[j][1]
            else:
                continue
    return lst
    pass


def sort_age(lst):
    for i in range(len(lst)-1):
        for j in range(len(lst)-1-i):
            if lst[j][1] < lst[j+1][1]:
                lst[j][1], lst[j+1][1] = lst[j+1][1], lst[j][1]
            else:
                continue
    return lst
    pass


def sort_age(lst):
    newlist = []
    while lst:
        oldest = lst[0]
        for i in lst:
            if i[1] > oldest:
                oldest = i
            else:
                continue
        lst.remove(oldest)
        newlist.append(oldest)
    return newlist


def sort_age(lst):
    lst.sort(key = lambda x: x[1], reverse = True)


def sort_age(lst):
    return lst.sort(key = lambda x: x[1], reverse = True)


def sort_age(lst):
    sort = []
    while lst:
        largest = a[0]
        for element in a:
            if element > smallest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    print(lst)


def sort_age(lst):
    new_list=[]
    largest=0
    while lst:
        for i in lst:
            if i[1]>largest:
                largest = i[1]
        new_list=new_list.append(i)
        lst.remove(i)
    return new_list


def sort_age(lst):
    new_list=[]
    largest=0
    while lst:
        for i in lst:
            if i[1]>largest:
                largest = i[1]
                count=i
        new_list=new_list.append(count)
        lst.remove(count)
        largest=0
    return new_list


def sort_age(lst):
    sort=[]
    while lst:
        biggest=lst[0][1]
        for i in lst:
            if i[1]>biggest:
                biggest=i[1]
        lst.remove(i)
        sort.append(i)
    return lst# Fill in your code here


def sort_age(lst):
    sort=[]
    while lst:
        biggest=lst[0][1]
        for i in lst:
            if i[1]>biggest:
                biggest=i[1]
        lst.remove(i)
        sort.append(i)
    return sort# Fill in your code here


def sort_age(lst):
    sort=[]
    while lst:
        biggest=0
        for i in lst:
            if i[1]>biggest:
                biggest=i[1]
        lst.remove(i)
        sort.append(i)
    return sort# Fill in your code here


def sort_age(lst):
    sort=[]
    while lst:
        biggest=lst[0][1]
        for i in lst:
            if i[1]>=biggest:
                biggest=i[1]
        lst.remove(i)
        sort.append(i)
    return sort# Fill in your code here


def sort_age(lst):
    sort=[]
    while lst:
        biggest=lst[0][1]
        for i in range(len(lst)):
            count=0
            if lst[i][1]>=biggest:
                biggest=lst[i][1]
            else:
                i+=1
                count+=1
        lst.remove(lst[i-count])
        sort.append(lst[i-count])
    return sort# Fill in your code here


def sort_age(lst):
    ages = []
    output = []
    for item in lst:
        ages.append(item[1])
    for item in lst:
        if min(ages) == item[1]:
            output.append(item)
            ages.remove(age)
    return output


def sort_age(lst):
    ages = []
    output = []
    for item in lst:
        ages.append(item[1])
    for item in lst:
        age = max(ages)
        if age == item[1]:
            output.append(item)
            ages.remove(age)
    return output


def sort_age(lst):
    a = []
    while lst:
        oldest = lst[0]
        for i in lst:
            if i[1] > oldest[1]:
                oldest = i
        lst.remove(oldest)
        a.append(oldest)
    pass


def sort_age(lst):
    return lst.sort(key=lambda x:x[1])


def sort_age(lst):
    lst.sort(key=lambda x:x[1])
    lst.reverse()
    print(lst)


def sort_age(lst):
    for i in range(len(lst)-1):
        for j in range(len(lst)-i):
            if lst[j][1] < lst[j+1][1]:
                lst[j][1],lst[j+1][1] = lst[j+1][1],lst[j][1]
            else:
                continue
    return lst
    pass


def sort_age(lst):
    sort =[]
    while lst:
        smallest = lst[0]
        for i in lst:
            if i< smallest:
                smallest = i
        lst.remove(smallest)
        sort.append(smallest)
    pass


def sort_age(lst):
    sort =[]
    while lst:
        smallest = lst[0]
        for i in lst:
            if i< smallest:
                smallest = i
        lst.remove(smallest)
        sort.append(smallest)
    return sort
    pass


def sort_age(lst):
    for j in range(len(lst)-1):
        for i in range(len(lst)-1-j):
            if lst[i][1]> lst[i+1][1]:
                lst[i],lst[i+1] = lst[i+1],lst[i]
    return lst


def sort_age(lst):
    for i in range(len(lst-1)):
        for j in range(len(lst)-1-i):
            if lst[j][1] < lst[j+1][1]:
                lst[j][1],lst[j+1][1] = lst[j+1][1],lst[j][1]
            else:
                continue
    return lst
    pass


def sort_age(lst):
    for i in range(len(lst)-1):
        for j in range(len(lst)-1-i):
            if lst[j][1] < lst[j+1][1]:
                lst[j][1],lst[j+1][1] = lst[j+1][1],lst[j][1]
            else:
                continue
    return lst
    pass


def sort_age(lst):
    lst.sort()
    lst.sort(key=lambda x:x[1],reverse=True)

    return lst
    pass


def sort_age(lst):
    lst.sort(key=lambda x: x[1])
    return lst


def sort_age(lst):
    lst.sort(key=lambda x: x[1],reverse = True)
    return lst


def sort_age(lst):
    lst1 = []

    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
    lst.remove(largest)
    lst1.append(largest)
    return lst1


def sort_age(lst):
    a = []
    for i in lst:
        a.append(i[1])
    print(a)
    sort = []
    while a:
        smallest = a[0]
        for element in a:
            if element < smallest:
                smallest = element
        a.remove(smallest)
        sort.append(smallest)
    print(sort)
    lst2 = []
    counter = 0
    for i in sort:
        for j in lst:
            if j[1] == i:
                lst2.append(j)
                counter += 1
    return lst2


def sort_age(lst):
    lst.sort(lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    lst.sort(key = lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    newlst=[]
    for i in lst:
        big=lst[0]
        for n in lst:
            if n[1]>big[1]:
                big=n
        lst.remove(big)
        newlst.append(big)
    return newlst


def sort_age(lst):
    lst.sort(key=lambda x: x[1], reverse=True)
    return lst


def sort_age(lst):
    new_lst = lst
    newnew = [new_lst[0]]
    for i in new_lst:
        for j in range(len(newnew)):
            if i[1]>newnew[j][1]:
                newnew.insert(j+1,i)
            elif i[1]<newnew[j][1]:
                newnew.insert(j,i)
            return newnew
        return newnew


def sort_age(lst):
    newnew = [lst[0]]
    for i in lst:
        for j in range(len(newnew)):
            if i[1]>newnew[j][1]:
                newnew.insert(j+1,i)
            elif i[1]<newnew[j][1]:
                newnew.insert(j,i)
            return newnew
        return newnew


def sort_age(lst):
    newnew = []
    for i in lst:
        for j in range(len(newnew)):
            if i[1]>newnew[j][1]:
                newnew.insert(j+1,i)
            elif i[1]<newnew[j][1]:
                newnew.insert(j,i)
            else:
                newnew.append(i)
            return newnew
        return newnew


def sort_age(lst):
    newnew = [lst[0]]
    for i in lst:
        for j in range(len(newnew)):
            if i[1]>newnew[j][1]:
                newnew.insert(j,i)
            elif i[1]<newnew[j][1]:
                newnew.insert(j+1,i)
            return newnew
        return newnew


def sort_age(lst):
    # Fill in your code here
    sorted = []
    while lst:
        oldest = lst[0]
        for i in range(len(lst)):
            if lst[i][1] > oldest[1]:
                oldest = lst[i]
        sorted.append(lst.pop(i))
    return sorted


def sort_age(lst):
    sort = []
    while lst:
        biggest = lst[0]
        for i in lst:
            if i[1] > biggest[1]:
                biggesr  - k
        lst.remove(biggest)
        sort.append(biggest)
    return sort


def sort_age(lst):
    sort = []
    while lst:
        biggest = lst[0]
        for i in lst:
            if i[1] > biggest[1]:
                biggesr  - i
        lst.remove(biggest)
        sort.append(biggest)
    return sort


def sort_age(lst):
    sort = []
    while lst:
        biggest = lst[0]
        for i in lst:
            if i[1] > biggest[1]:
                biggest  - i
        lst.remove(biggest)
        sort.append(biggest)
    return sort


def sort_age(lst):
    for i in range(1,len(lst)):
        while lst[i][1]<lst[i-1][1]:
            lst.pop(lst[i])
            lst.insert(lst[i],i-1)# Fill in your code here
    return lst.reverse()


def sort_age(lst):
    for i in range(1,len(lst)):
        while lst[i][1]<lst[i-1][1]:
            lst.pop(i)
            lst.insert(i-1,lst[i])# Fill in your code here
    return lst.reverse()


def sort_age(lst):
    if len(lst)==1:
        return lst
    else:
        temp=lst[0][1]
        for i in range(len(lst)):
            if lst[i][1]<temp:
                temp=lst[i][1]
                count=i
        result=[lst[count],]
        pop=lst.pop(count)
        return result+sort_age(lst)
    # Fill in your code here
    pass


def sort_age(lst):
    if len(lst)==1:
        return lst
    else:
        temp=lst[0][1]
        count=0
        for i in range(len(lst)):
            if lst[i][1]>temp:
                temp=lst[i][1]
                count=i
        result=[lst[count],]
        pop=lst.pop(count)
        return result+sort_age(lst)
    # Fill in your code here
    pass


def sort_age(lst):
    new_lst=[lst[0],]
    for x in lst[1:]:
        if x[1] > new_lst[-1][1]:
            new_lst += [x,]
        else:
            count=0
            while count<len(new_lst):
                if x[1] > new_lst[count][1]:
                    count+=1
                    continue
                else:
                    new_lst = new_lst[0:count]+[x,]+new_lst[count:]
                    break
    return new_lst[::-1]


def sort_age(lst):
    new_lst=[lst[0],]
    if lst==[]:
        return []
    for x in lst[1:]:
        if x[1] > new_lst[-1][1]:
            new_lst += [x,]
        else:
            count=0
            while count<len(new_lst):
                if x[1] > new_lst[count][1]:
                    count+=1
                    continue
                else:
                    new_lst = new_lst[0:count]+[x,]+new_lst[count:]
                    break
    return new_lst[::-1]


def sort_age(lst):
    if lst == []:
        return lst
    else:
        age_lst, new_lst = [], []
        for x in lst:
            age_lst.append(x[1])
        while age_lst:
            max_age = max(age_lst)
            for i in lst:
                if i[1] == max_age:
                    new_lst.append(i)
        return new_lst
    pass


def sort_age(lst):
    l = len(lst)
    for i in range(0, l):
        for j in range(0, l-i-1):
            if (lst[j][1] > lst[j + 1][1]):
                temp = lst[j]
                lst[j]= lst[j + 1]
                lst[j + 1]= temp
    return lst


def sort_age(lst):
    l = len(lst)
    for i in range(0, l):
        for j in range(0, l-i-1):
            if (lst[j][1] > lst[j + 1][1]):
                temp = lst[j]
                lst[j]= lst[j + 1]
                lst[j + 1]= temp
    return list.reverse(lst)


def sort_age(lst):
    lst.sort(key=lambda x: x[1], reverse=True)
    return lst


def sort_age(lst):
    return lst.sort(key=lambda x: x[1], reverse=True)


def sort_age(lst):
    current=0
    tup=()
    for i in lst:
        if i[1]>current:
            tup+=tuple(i)
            current=i[1]
        else:
            tuple(i)+tup
    return tup


def sort_age(lst):
    current=0
    tup=()
    for i in lst:
        if i[1]>current:
            tup+=tuple(i)
            current=i[1]
        else:
            tup=tuple(i)+tup
    return tup


def sort_age(lst):
    current=0
    tup=()
    for i in lst:
        if i[1]>current:
            tup+=tuple(i)
            current=i[1]
        else:
            tup=tuple(i)+tup
    return list(tup)


def sort_age(lst):
    current=0
    tup=()
    for i in lst:
        if i[1]>current:
            tup+=tuple(i)
            current=i[1]
        else:
            tup=tuple(i)+tup
    return [tup]


def sort_age(lst):
    current=0
    tup=[]
    for i in lst:
        if i[1]>current:
            tup.append(i)
        else:
            i.append(tup)
    return tup


def sort_age(lst):
    # Fill in your code here
    result=[]
    for i in lst:
        result+=[i[::-1],]
    result.sort()
    result.reverse()
    ans=[]
    for i in result:
        ans+=[i[::-1],]
    return ans


def sort_age(lst):
    lst.sort()
    new = []
    for i in range(len(lst)):
        j = len(lst) - i- 1
        new.append(lst[j])
    return new


def sort_age(lst):
    # lst.sort(key = lambda x: x[1], reverse= True)
    # return lst
    while True:
        changed = False
        for i in range (len(lst)-1):
            if lst[i][1] < lst[i+1][1]:
                lst[i], lst[i+1] = lst[i+1], lst[i]
                changed = True
        if not changed:
            break
    return lst


def sort_age(lst):
    # Fill in your code here
    A = map(lambda x:x[1],lst)
    a = []
    counter =0
    while counter<len(lst):
        for i in A:
            if i>a[0]:
                a = i.extend(a)
            elif i<a[-1]:
                a = a.extend(i)
        counter += 1

    b = []
    for i in a:
        for y in lst:
            if y[1] ==i:
                b.append(y)
    return b


def sort_age(lst):
    lst.sort(key = lambda x: x[1],reverse=True)
    return lst


def sort_age(lst):
    # Fill in your code here
    newlst=[]
    while lst:
        maximum = lst[0][1]
        for i in lst:
            if i[1]>maximum:
                maximum = i[1]
        newlst.append(i)
        lst.remove(i)

    print(newlst)


def sort_age(lst):
    # Fill in your code here
    newlst=[]
    while lst:
        maximum = lst[0][1]
        for i in lst:
            if i[1]>maximum:
                maximum = i[1]
        newlst.append(i)
        lst.remove(i)

    return newlst


def sort_age(lst):
    # Fill in your code here
    newlst=[]
    while lst:
        maximum = lst[0][1]
        for i in lst:
            if i[1]>maximum:
                maximum = i[1]
                newlst.append(i)
                lst.remove(i)

    return newlst


def sort_age(lst):
    def for_age(lst):
        for i in range(len(lst)):
            if i == 0: continue
            else:
                while i > 0:
                    if lst[i][1] < lst[i-1][1]:
                        lst[i], lst[i-1] = lst[i-1], lst[i]
                        i -= 1
                    else: i = 0
    for_age(lst).reverse
    return lst


def sort_age(lst):
    a=lst
    sort=[]
    while a:
        smallest=a[0]
        for element in a:
            if element[1]<smallest[1]:
                smallest=element
    a.remove(smallest)
    sort.append(smallest)
    return sort


def sort_age(lst):
	lst.sort(key=lambda x: x[1],reverse=True)
	print(lst)


def sort_age(lst):
    return lst.sort(key = lambda x: x[1], reverse = True)


def sort_age(lst):
    a = []
    b = []
    n = len(lst)
    for i in range(n):
        age = lst[i][1]
        a += [age]
        a.sort()
        a.reverse()
    for j in range(n):
        for k in range(n):
            if a[j] == lst[k][1]:
                b += [lst[k]]
            else:
                continue
    return b


def sort_age(lst):
    return lst.sort(key = lambda x: x[1])


def sort_age(lst):
    lst = lst.sort(key = lambda x: x[1], reverse=True)
    return lst


def sort_age(lst):
    holder=[]
    for x in lst:
        if holder==[]:
            holder=x
        elif x[1]>holder[1]:
            holder=x
    return holder+sort_age(lst[1:])


def sort_age(lst):
    lst.sort(key = lambda x: x[1])
    return lst


def sort_age(lst):
    for i in range(len(lst)-1):
        while lst[i][1] > lst[i+1][1]:
            temp = lst[i]
            del lst[i]
            lst += [temp]
    return lst


def sort_age(lst):
    for i in range(len(lst)-1):
        while lst[i][1] < lst[i+1][1]:
            temp = lst[i]
            del lst[i]
            lst += [temp]
    return lst


def sort_age(lst):
    for i in range(len(lst)-1):
        while lst[i][1] > lst[i+1][1]:
            temp = lst[i]
            del lst[i]
            lst += [temp]
    return lst.reverse()


def sort_age(lst):
    for i in range(len(lst)-1):
        while lst[i][1] > lst[i+1][1]:
            temp = lst[i]
            del lst[i]
            lst += [temp]
    lst.reverse()
    return lst


def sort_age(lst):
    sort1 = []
    while lst:
        largest = lst[0][1]
        if i[1] > largest:
            largest = i[1]
        lst.remove(i)
        sort1.append(i)
    return sort1


def sort_age(lst):
    # Fill in your code here
    lst.sort(key=lambda x : x[1], reverse=T)


def sort(lst):
    for i in range(len(lst)-1):
        if lst[i][1] > lst[i+1][1]:
            temp = lst[i]
            del lst[i]
            lst += [temp]
        else:
            continue
    return lst

def sort_age(lst):
    oldlist = tuple(lst)
    if tuple(sort(lst)) == oldlist:
        return lst
    else:
        sort(lst)
        return sort_age(lst)


def sort_age(lst):
    males = []
    females = []
    for i in lst:
        if lst[0][0] == "M":
            males = males + [lst[0],]
        elif lst[0][0] == "F":
            females = females + [lst[0],]
    return merge_sort(males) + merge_sort(females)

def merge(left, right):
    results = []
    while left and right:
        if left[0][0] < right[0][0]:
            results.append(left,pop(0))
        else:
            results.append(right.pop(0))
    results.extend(left)
    results.extend(right)
    return results

def merge_sort(lst):
    if len(lst) < 2:
        return lst
    mid = len(lst) // 2
    left = merge_sort(lst[:mid])
    right = merge_sort(lst[mid:])
    return merge(left, right)


def sort_age(lst):
    males = []
    females = []
    while len(lst) > 0:
        if lst[0][0] == "M":
            males = males + [lst[0],]
        elif lst[0][0] == "F":
            females = females + [lst[0],]
        lst = lst[1:]
    return merge(merge_sort(males), merge_sort(females))

def merge(left, right):
    results = []
    while left and right:
        if left[0][0] < right[0][0]:
            results.append(left.pop(0))
        else:
            results.append(right.pop(0))
    results.extend(left)
    results.extend(right)
    return results

def merge_sort(lst):
    if len(lst) < 2:
        return lst
    mid = len(lst) // 2
    left = merge_sort(lst[:mid])
    right = merge_sort(lst[mid:])
    return merge(left, right)


def sort_age(lst):
    males, females = [], []
    for i in lst:
        if i[0] == "M":
            males = males + [lst[0],]
        elif i[0] == "F":
            females = females + [lst[0],]
        lst = lst[1:]
    return merge(merge_sort(males), merge_sort(females))

def merge(left, right):
    results = []
    while left and right:
        if left[0] > right[0]:
            results.append(left.pop(0))
        else:
            results.append(right.pop(0))
    results.extend(left)
    results.extend(right)
    return results

def merge_sort(lst):
    if len(lst) < 2:
        return lst
    mid = len(lst) // 2
    left = merge_sort(lst[:mid])
    right = merge_sort(lst[mid:])
    return merge(left, right)


def sort_age(lst):
    return lst.sort(key = lambda x:x[1])


def sort_age(lst):
    return lst.sort(key = lambda x: x[1], reverse = True)


def sort_age(lst):
    lst.sort(key = lambda x: x[1])


def sort_age(lst):
    lst.sort(lambda x: x[1])
    return lst


def sort_age(lst):
    newlst = []
    ages = []
    for i in lst:
        ages.append(i[1])
    ages.sort()
    for x in ages[::-1]:
        for i in lst:
            if i[1] == x:
                newlst.append(i)
    return newlst


def sort_age(lst):
    lst.sort(key = lambda x: x[1], reverse = True)
    print(lst)


def sort_age(lst):
    new_lst = []
    for i in range(len(lst)):
        if lst[i][1]< lst[i+1][1]:
            new_lst.append(lst[i])
    lst.reverse()
    return lst

    pass


def sort_age(lst):
    new_lst = [()]
    for i in range(len(lst)):
        if lst[i][1]< lst[i+1][1]:
            new_lst.append(lst[i])
    lst.reverse()
    return lst

    pass


def sort_age(lst):
    new_lst = []
    for i in range(len(lst)):
        if lst[i][1]> lst[i+1][1]:
            new_lst.append(lst[i])
    return lst

    pass


def merge(one,two):
    new_tup = []
    while left and right:
        if one[0][1] < two[0][1]:
            new_tup.append(one.pop(0))
        else:
            new_tup.append(two.pop(0))
    return new_tup

def sort_age(lst):
    n = len(lst)
    if n <2:
        return lst
    left = lst[:n/2]
    right = lst[n/2:]
    return merge(left,right)


def merge(one,two):
    new_tup = []
    while one and two:
        if one[0][1] < two[0][1]:
            new_tup.append(one.pop(0))
        else:
            new_tup.append(two.pop(0))
    return new_tup

def sort_age(lst):
    n = len(lst)
    if n <2:
        return lst
    left = lst[:n/2]
    right = lst[n/2:]
    return merge(left,right)


def sort_age(lst):
    return lst.sort(key=lambda x: x[1], reverse=True)


def sort_age(lst):
    a=[]
    for i in lst:
        if i==max(lst):
            a.append(i)
        continue
    return a


def sort_age(lst):
    compiled = []
    result = []
    for i in lst:
        compiled = compiled + [i[1]]
    compiled.sort()
    compiled.reverse()
    for i in compiled:
        for j in lst:
            if i == j[1]:
                result = result + [j]
    return result


def sort_age(lst):
    return lst.sort(key=lambda x:x[1])


def sort_age(lst):
    def age(i):
        return i[1]

    def position(seq, ele):
        n = len(seq)
        for i in range(n):
            if seq[i] == ele:
                return i

    def largest_age(seq):
        largest = age(seq[0])
        largest_pos = 0
        for i in seq:
            if age(i) > largest:
                largest = age(i)
                largest_pos = position(seq,i)
        return seq[largest_pos]
    n = len(lst)
    if n ==0:
        return []
    elif n ==1:
        return lst
    else:
        return [largest_age(lst)]+[sort_age(lst[1:])]

    # Fill in your code here
    pass


def sort_age(lst):
    a = list(set(lst))
    lst.clear()
    lst.append(a)
    return lst


def sort_age(lst):
    # Fill in your code here
    return lst.sort(key=lambda x: x[1])


def sort_age(lst):
    return lst.sort(key = lambda x: x[1], reverse = True)


def sort_age(lst):
    lst.sort(key=lambda x:x[1])
    lst.reverse()
    print(lst)


def sort_age(lst):
    lst.sort(lambda x: x[1], reverse = True)
    return lst


def sort_age(lst):
    for i in range(1,len(lst)):
        while lst[i][1]<lst[i-1][1]:
            lst.pop(lst[i])
            lst.insert(lst[i],i-1)# Fill in your code here
    return lst.reverse()


def sort_age(lst):
    for i in range(1,len(lst)):
        while lst[i][1]<lst[i-1][1]:
            lst.pop(i)
            lst.insert(i-1,lst[i])# Fill in your code here
    return lst.reverse()


def sort_age(lst):
    new_lst=[lst[0],]
    for x in lst[1:]:
        if x[1] > new_lst[-1][1]:
            new_lst += [x,]
        else:
            count=0
            while count<len(new_lst):
                if x[1] > new_lst[count][1]:
                    count+=1
                    continue
                else:
                    new_lst = new_lst[0:count]+[x,]+new_lst[count:]
                    break
    return new_lst[::-1]


def sort_age(lst):
    # Fill in your code here
    result=[]
    for i in lst:
        result+=[i[::-1],]
    result.sort()
    result.reverse()
    ans=[]
    for i in result:
        ans+=[i[::-1],]
    return ans


def top_k(lst, k):
    ls = []
    for i in range(k):
        ls.append(max(lst))
        lst.remove(max(lst))
    return ls

Task: Top-K

Write a function top_k that accepts a list of integers as the input and returns the greatest
k number of values as a list, with its elements sorted in descending order. You may use
any sorting algorithm you wish, but you are not allowed to use sort and sorted.


def top_k(lst, k):
    res = []
    for i in range(k):
        res.append(max(lst))
        lst.remove(max(lst))
    return res


def top_k(lst, k):
    result = []
    for i in range(k):
        result.append(max(lst))
        lst.remove(max(lst))
    return result


def top_k(lst, k):
    ls=[]
    for i in range(k):
        ls.append(max(lst))
        lst.remove(max(lst))
    return ls


def sort_descending(lst):
    new_lst = []
    while lst:
        largest = lst[0]
        for i in range(len(lst)):
            if lst[i] > largest:
                largest = lst[i]
        lst.remove(largest)
        new_lst.append(largest)
    return new_lst

def top_k(lst, k):
    lst = sort_descending(lst)
    return lst[:k]


def top_k(lst, k):
    if k == 0:
        return []
    if lst == []:
        return lst
    sort = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        sort.append(biggest)
        if len(sort) == k:
            return sort
        else:
            continue


def top_k(lst, k):
    stop = False
    result = []
    while k>0:
        highest = 0
        index = 0
        for i in range(0,len(lst)):
            if lst[i]>highest:
                index = i
                highest = lst[i]
        result = result + [highest]
        lst.pop(index)
        k = k - 1
    return result


def top_k(lst, k):
    lst_res = lst
    sort = []
    while lst_res:
        largest = lst_res[0]
        for elements in lst_res:
            if elements > largest:
                largest = elements
        lst_res.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    sort = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        sort.append(biggest)
    return sort[:k]


def magus_sort(lst):
    if len(lst) == 0:
        return []
    elif len(lst) == 1:
        return lst
    elif len(lst) == 2:
        if lst[0] >= lst[1]:
            return lst
        else:
            return [lst[1], lst[0]]
    else:
        if lst[0] >= lst[1]:
            list1, list2 = [lst[0]], [lst[1]]
        else:
            list1, list2 = [lst[1]], [lst[0]]
        for i in lst[2:]:
            if i >= list1[0]:
                list1.append(i)
            else:
                list2.append(i)
        return magus_sort(list1) + magus_sort(list2)

def top_k(lst, k):
    return magus_sort(lst)[0:k]


def top_k(lst, k):
    if k == 1:
        return [max(lst)]
    else:
        answer = []
        for i in range(min(k, len(lst))):
            largest = max(lst)
            answer.append(largest)
            lst.remove(largest)
        return answer


def top_k(lst, k):
    new_lst = []
    while lst:
        largest = lst[0]
        for numbers in lst:
            if numbers > largest:
                largest = numbers
        new_lst.append(largest)
        lst.remove(largest)
    return new_lst[:k]


def top_k(lst, k):
    sort=[]
    for i in range (k):
        biggest=max(lst)
        lst.remove(biggest)
        sort.append(biggest)
    return sort


def top_k(lst, k):
    result = []
    while k > 0:
        big = max(lst)
        result.append(big)
        lst.remove(big)
        k -= 1
    return result


def top_k(lst, k):
    sort = []
    while lst:
        biggest = lst[0]
        for i in lst:
            if i > biggest:
                biggest = i
        lst.remove(biggest)
        sort.append(biggest)
    return sort[0:k]


def top_k(lst, k):
    if lst==[]:
        return []
    sort=[]
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]
    pass


def top_k(lst, k):
    counter=0
    new_lst=[]
    while counter<k:
        maxi=max(lst)
        new_lst.append(maxi)
        lst.remove(maxi)
        counter+=1
    return new_lst


def top_k(lst, k):
    product = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i>largest:
                largest = i
        lst.remove(largest)
        product.append(largest)
    return product[:k]


def top_k(lst, k):
    ret = []
    for i in range(k):
        max_int_pos = lst.index(max(lst))
        ret.append(lst.pop(max_int_pos))
    return ret


def top_k(lst, k):
    result=[]
    for i in range(0,k):
        max=lst[0]
        for j in lst:
            if j>max:
                max=j
        result.append(max)
        lst.remove(max)
    return result# Fill in your code here
    pass


def top_k(lst, k):
    max_lst =[]
    i = 0
    while i < k:
        max_lst.append(max(lst))
        lst.remove(max(lst))
        i += 1
    return max_lst


def top_k(lst, k):
    new_lst = []
    while len(new_lst) < k:
        maximum = max(lst)
        new_lst.append(maximum)
        lst.remove(maximum)

    return new_lst


def top_k(lst, k):
    result = []
    while len(result) < k:
        biggest = lst[0]
        for i in lst:
            if i > biggest:
                biggest = i
        lst.remove(biggest)
        result.append(biggest)
    return result


def top_k(lst, k):
    sortedlst = []
    while len(sortedlst) != k:
        largest = lst[0]
        for el in lst:
            if el > largest:
                largest = el
        sortedlst.append(largest)
        lst.remove(largest)
    return sortedlst


def top_k(lst, k):
    sort = []
    while lst:
        biggest = lst[0]
        for elem in lst:
            if elem > biggest:
                biggest = elem
        lst.remove(biggest)
        sort.append(biggest)
    return sort[:k]


def quicksort(lst, f, comparator):
    if len(lst) <= 1:
        return lst

    pivot_index = len(lst) // 2
    pivot = lst[pivot_index]
    flist, blist = [], []

    for i in range(len(lst)):
        if i == pivot_index:
            continue
        if comparator(f(lst[i]), f(pivot)):
            flist.append(lst[i])
        else:
            blist.append(lst[i])

    return quicksort(flist, f, comparator) + [pivot] + quicksort(blist, f, comparator)

def top_k(lst, k):
    sorted_lst = quicksort(lst, lambda x: x, lambda x, y: x > y)
    return sorted_lst[:k]


def top_k(lst, k):
    rs=[]
    for qwerty in range(0,k):
        biggest=lst[0]
        for k in lst:
            if biggest<k:
                biggest=k
        rs.append(biggest)
        lst.remove(biggest)
    return rs


def top_k(lst,k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if largest < element:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[0:k]


def top_k(lst, k):
    # Fill in your code here

    result = []
    while lst:
        minimum = lst[0]  # arbitrary number in list
        for x in lst:
            if x > minimum:
                minimum = x
        result.append(minimum)
        lst.remove(minimum)
    return result[:k]


def top_k(lst, k):
    new_lst = []
    while lst:
        maxi = lst[0]
        for item in lst:
            if item > maxi:
                maxi = item
        new_lst.append(maxi)
        lst.remove(maxi)
    return new_lst[0:k]# Fill in your code here


def top_k(lst, k):
    result = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i>largest:
                largest = i
        lst.remove(largest)
        result.append(largest)
    return result[:k]

#def sort_age(lst):
   # sort = []
   # while lst:
     #   oldest = lst[0]
     #   for person in lst:
         #   if person[1] >= oldest[1]:
            #    oldest = person
       # lst.remove(oldest)
      #  sort.append(oldest)
   # return sort


def top_k(lst, k):
    if lst == [] or k == 0:
        return []
    else:
        final = []
        while lst:
            element = max(lst)
            final += [element,]
            lst.remove(element)
            if len(final) == k:
                break
        return final


def top_k(lst, k):
    sort_list = []
    counter = 0
    while counter < k:
        sort_list.append(max(lst))
        lst.remove(max(lst))
        counter = counter + 1
    return sort_list


def top_k(lst, k):
    tmp = []
    while len(lst) > 0:
        tmp.append(max(lst))
        lst.remove(max(lst))
    return tmp[:k]


def top_k(lst, k):
    return merge_sort(lst)[:k]

def merge_sort(lst):
    if len(lst) < 2:
        return lst
    mid = len(lst) // 2
    left = merge_sort(lst[:mid])
    right = merge_sort(lst[mid:])
    return merge(left, right)

def merge(left, right):
    results = []
    while left and right:
        if left[0] > right[0]:
            results.append(left.pop(0))
        else:
            results.append(right.pop(0))
    results.extend(left)
    results.extend(right)
    return results


def top_k(lst,k):
    sort = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        sort.append(biggest)
    return sort[:k]


def sort_age(lst):
        new=[]
        while lst !=[]:
            big=lst[0]
            for i in lst:
                if i>big:
                    big=i
            lst.remove(big)
            new.append(big)
        return new
def top_k(lst, k):
    return sort_age(lst)[:k]
    pass


def top_k(lst, k):
    sort = []
    if k==0 or lst==[]:
        return []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element

        lst.remove(biggest)
        sort.append(biggest)
        if len(sort)==k:
            break
    return sort


def top_k(lst, k):
    result = []
    while k > 0:
        big = max(lst)
        result.append(big)
        lst.remove(big)
        k -= 1
    return result


def top_k(lst, k):
    lst_res = lst
    sort = []
    while lst_res:
        largest = lst_res[0]
        for elements in lst_res:
            if elements > largest:
                largest = elements
        lst_res.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    new_list = []
    while lst:
        maximum = lst[0]
        for i in lst:
            if i > maximum:
                maximum = i
        new_list.append(maximum)
        lst.remove(maximum)
    new_list_2 = []
    counter = 0
    for i in new_list:
        if counter < k:
            new_list_2.append(new_list[counter])
            counter = counter + 1
    return new_list_2


def top_k(lst, k):
    product = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i>largest:
                largest = i
        lst.remove(largest)
        product.append(largest)
    return product[:k]


def top_k(lst, k):
    new = []
    while lst:
        largest = lst[0]
        for a in lst:
            if a > largest:
                largest = a
        lst.remove(largest)
        new.append(largest)
    return new[0:k]


def top_k(lst, k):
    sort = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        sort.append(biggest)
    return sort[:k]


def top_k(lst, k):
    # Fill in your code here
    sort = []
    while lst: # a is not []
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, num):
    def insertion_sort(lst):
        for i in range(len(lst)):
            if i == 0: continue
            else:
                while i > 0:
                    if lst[i] < lst[i-1]:
                        lst[i], lst[i-1] = lst[i-1], lst[i]
                        i -= 1
                    else: i = 0

    insertion_sort(lst)
    lst.reverse()

    score, result = -1, []
    for i in lst:
        if i == score:
            result += (i,)
        elif len(result) >= num: break
        else:
            result += (i,)
            score = i
    return result


def top_k(lst, k):

    answer = []
    a = lst[0]

    while lst:

        for i in range(len(lst)):
            answer.append(max(lst))
            lst.remove(max(lst))

    return answer[:k]


def top_k(lst, k):
    ls=[]
    for i in range(k):
        ls.append(max(lst))
        lst.remove(max(lst))
    return ls


def top_k(lst, k):
    def merge_sort(lst):
        if len(lst) <= 1:
            return lst
        else:
            mid = len(lst) // 2
            lst1 = merge_sort(lst[:mid])
            lst2 = merge_sort(lst[mid:])
            result = []

            while lst1 and lst2:
                if lst1[0] < lst2[0]:
                    result.append(lst2.pop(0))
                else:
                    result.append(lst1.pop(0))

            result.extend(lst1)
            result.extend(lst2)

            return result

    lst = merge_sort(lst)
    return lst[:k]


def top_k(lst, k):
    sort = []
    while lst:
        biggest = lst[0]
        for i in lst[1:]:
            if i > biggest:
                biggest = i
        lst.remove(biggest)
        sort.append(biggest)

    n = 1
    sort_k = []
    while n <= k:
        sort_k.append(sort.pop(0))
        n += 1

    return sort_k


def top_k(lst, k):
    k=len(lst) if k>len(lst) else k
    l=[]
    while len(l)!=k:
        m=lst[0]
        for i in lst:
            if i>m:
                m=i
        l.append(m)
        lst.remove(m)
    return l


def top_k(lst, k):
    while lst:
        maximum=[]
        for a in range(len(lst)):
            maximum.append(max(lst))
            lst.remove(max(lst))

    return maximum[0:k]


def top_k(lst, k):
    sorted_lst = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sorted_lst.append(largest)
    return sorted_lst[:k]
    pass


def top_k(lst, k):
    num, result = max(lst), []
    while len(result) < k:
        if num in lst:
            result.append(num)
            lst.remove(num)
        elif num not in lst:
            num -= 1
            continue
    return result


def top_k(lst, k):
    new = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        new.append(largest)
        lst.remove(largest)
    if k > len(new):
        return new
    else:
        return new[:k]


def top_k(lst, k):
    newlst = []
    while lst:
        largest = lst[0]
        for number in lst:
            if number > largest:
                largest = number
        lst.remove(largest)
        newlst.append(largest)
    result = []
    for counter in range(k):
        result = result + [newlst[counter],]
    return result


def top_k(lst, k):
    lst=sort_list(lst)
    while len(lst)>k:
        lst.pop(-1)
    return lst

def sort_list(lst):
    sort=[]
    while lst:
        largest=lst[0]
        for i in lst:
            if i>largest:
                largest=i
        lst.remove(largest)   #1.indentation: under while loop
        sort+=[largest]
    return sort

#1. find the largest first, then remove it from the lst,
#then append the sort


def top_k(lst, k):
    sorted_list = []
    while lst:
        l = lst[0]
        for i in lst:
            if i > l:
                l = i
        lst.remove(l)
        sorted_list.append(l)
    return sorted_list[:k]


def top_k(lst, k):
    result = []
    while len(result) < k:
        result.append(lst.pop(lst.index(max(lst))))
    return result


def top_k(lst, k):
    result = [lst[0]]
    for i in lst[1:]:
        if i > result[0]:
            result.insert(0, i)
        elif i <= result[-1]:
            result.append(i)
        else:
            for j in range(1, len(result) - 1):
                if i <= result[j] and i >= result[j+1]:
                    result.insert(j+1, i)
                    break
    return result[:k]


def top_k(lst, k):
    sort = []
    counter = 0
    while counter != k:
        sort.append(max(lst))
        lst.remove(max(lst))
        counter += 1
    return sort


def top_k(lst, k):
    sort=[]
    for i in range (k):
        biggest=max(lst)
        lst.remove(biggest)
        sort.append(biggest)
    return sort


def top_k(lst, k):
    s = []
    for i in range(k):
        t = 0
        for r in lst:
            if r >= t:
                t = r
        lst.remove(t)
        s.append(t)
    return s


def top_k(lst, k):
    sort_list = []
    counter = 0
    while counter < k:
        sort_list.append(max(lst))
        lst.remove(max(lst))
        counter = counter + 1
    return sort_list


def top_k(lst,k):
    newlst=[]
    while lst:
        highest=lst[0] #first number
        for number in lst:
            if number>highest:
                highest=number
        for number in lst:
            if number==highest:
                newlst.append(number)
                lst.remove(number)
    return newlst[:k]


def top_k(lst, k):

    newlst = []
    for i in range(k):
        newlst.append(max(lst))
        lst.remove(max(lst))
    return newlst


def top_k(lst, k):
    a = lst.copy()
    b = lst.copy()
    lst.clear()
    for i in range(len(b)):
        lst.append(max(a))
        a.remove(max(a))
    lst = lst[:k]
    return lst# Fill in your code here
    pass


def top_k(lst, k):
    sort = []
    i = 0
    while i < k:
        smallest = lst[0]
        for element in lst:
            if element > smallest:
                smallest = element
        lst.remove(smallest)
        sort.append(smallest)
        i += 1

    return sort


def top_k(lst, k):
    def merge(left,right):
        merged = []
        while left and right:
            if left[0] > right[0]:
                merged.append(left.pop(0))
            else:
                merged.append(right.pop(0))
        return merged + left + right
    def merge_sort(lst):
        if len(lst) == 1:
            return lst
        if len(lst) == 2:
            if lst[0] > lst[1]:
                return lst
            else:
                return lst[::-1] #reverse list
        else:
            a = len(lst)//2
            return merge(merge_sort(lst[:a]),merge_sort(lst[a:]))
    lst = merge_sort(lst)
    return lst[:k]


def top_k(lst, k):
    def quick_sort(lst):
        def cmp(a,b):
            if a > b:
                return True
            else:
                return False

        if len(lst)==0:
            return []

        lst0=[]
        lst1=[]

        partition = lst[0]

        for item in lst[1:]:
            if cmp(item,partition):
                lst0.append(item)
            else:
                lst1.append(item)

        return quick_sort(lst0)+[partition]+quick_sort(lst1)

    return quick_sort(lst)[:k]


def top_k(lst,k): #k is the number of values in the lst
    lst2 = []
    while lst:
        a = max(lst)
        lst2.append(a)
        lst.remove(a)
    return lst2[0:k]


def top_k(lst, k):
    new = []
    for i in range(k):
        largest = max(lst)
        new += [largest]
        lst.remove(largest)
    return new


def top_k(lst, k):
    # Fill in your code here
    sorted = []

    while lst:
        smallest = lst[0] # smallest has to exist within this step
        for i in lst:
            if i < smallest:
                smallest = i # take note: you must not reverse this step

        sorted.append(smallest)
        lst.remove(smallest)

    #ends the while loop, when lst has no elements
    # and every element is sorted into sorted
    sorted.reverse()

    return sorted[0:k]


def top_k(lst, k):
    # Fill in your code here
    new = []
    for i in range(k):
        a = max(lst)
        lst.remove(a)
        new.append(a)
    return new


def top_k(lst, k):
    new_lst = []
    for i in range(k):
        new_lst.append(max(lst))
        lst.remove(max(lst))
    return new_lst
    pass


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst,k):
    a = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        a.append(biggest)
    return a[:k]


def top_k(lst, k):
    sorted_list = []
    biggest = []
    while lst:
        largest = 0
        for i in lst:
            if i >= largest:
                largest = i
        lst.remove(largest)
        sorted_list.append(largest) # from biggest to smallest
    for j in range (k):
        biggest.append(sorted_list[j])
    return biggest
    pass


def top_k(lst, k):
    new_lst = []
    while len(new_lst) != k:
        new_lst.append(max(lst))
        lst.remove(max(lst))
    return new_lst


def top_k(lst,k):
    n = len(lst)
    result = []
    while n !=0:
        test =[]
        for counter in range(n):
            test.append(lst[counter])
        first = max(test)
        for counter in range(n):
            if lst[counter] == first:
                result.append(lst.pop(counter))
                break
        n = len(lst)
    return result[:k]


def top_k(lst, k):
    final = []
    while len(final) != k:
        a = max(lst)
        final.append(a)
        lst.remove(a)
    return final


def top_k(lst, k):
    # Fill in your code here
    a = lst
    sort = []
    while a:
        largest = a[0]
        for item in a:
            if item>largest:
                largest = item
        a.remove(largest)
        sort.append(largest)
    return(sort)[:k]


def top_k(lst, k):
    result = []
    while lst:
        biggest = lst[0]
        for number in lst:
            if number > biggest:
                biggest = number
        lst.remove(biggest)
        result.append(biggest)
    return result[:k]


def top_k(lst, k):
    sorted_list = []
    while lst:
        smallest = lst[0]
        for element in lst:
            if element < smallest:
                smallest = element
        lst.remove(smallest)
        sorted_list.append(smallest)
    final = sorted_list[::-1]
    return final[:k]


def top_k(lst, k):
    s = []
    for i in range(k):
        t = 0
        for r in lst:
            if r >= t:
                t = r
        lst.remove(t)
        s.append(t)
    return s


def top_k(lst, k):
    # Fill in your code here
    sort = []
    while lst:
        largest = lst[0]
        for i in range(len(lst)):
            if lst[i] > largest:
                largest = lst[i]
        sort.append(largest)
        lst.remove(largest)
    return sort[:k]


def top_k(lst, k):
    descending_list=[]
    while lst:
        largest=lst[0]
        for i in lst:
            if i>largest:
                largest=i
        descending_list.append(largest)
        lst.remove(largest)
    return descending_list[:k]


def top_k(lst, k):
    lst = sorting(lst)
    new = []
    for i in range(k):
        new.append(lst[0])
        del lst[0]
    return new

def sorting(lst):
    sorted_lst = []
    while lst:
        smallest = lst[0]
        for i in lst:
            if i < smallest:
                smallest = i
        lst.remove(smallest)
        sorted_lst.append(smallest)
    sorted_lst.reverse()
    return sorted_lst


def top_k(lst, k):
    res =[]
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest =element
        lst.remove(largest)
        res.append(largest)
    return res[:k]


def top_k(lst,k):
    sort_list = []
    while lst:
        biggest = lst[0]
        for e in lst:
            if biggest < e:
                biggest = e
        lst.remove(biggest)
        sort_list.append(biggest)
    return sort_list[:k]


def top_k(lst, k):
    # Fill in your code here
    new = []
    for i in range(k):
        a = max(lst)
        lst.remove(a)
        new.append(a)
    return new


def top_k(lst, k):
    result = []
    while len(result) < k:
        biggest = lst[0]
        for i in lst:
            if i > biggest:
                biggest = i
        lst.remove(biggest)
        result.append(biggest)
    return result


def sort_age(lst):
        new=[]
        while lst !=[]:
            big=lst[0]
            for i in lst:
                if i>big:
                    big=i
            lst.remove(big)
            new.append(big)
        return new
def top_k(lst, k):
    return sort_age(lst)[:k]
    pass


def top_k(lst, k):
    klist = []
    while len(klist) < k:
        biggest = lst[0]
        for i in lst:
            if i > biggest:
                biggest = i
        klist.append(biggest)
        lst.remove(biggest)
    return klist


def top_k(lst, k):

    newlist = []
    counter = k

    while counter > 0:
        biggest = lst[0]
        for i in lst[1:]:
            if biggest > i:
                continue
            else:
                biggest = i
                continue

        newlist += [biggest]
        counter -= 1
        lst.remove(biggest)

    return newlist


def top_k(lst, k):
    result = [lst[0]]
    for i in lst[1:]:
        if i > result[0]:
            result.insert(0, i)
        elif i <= result[-1]:
            result.append(i)
        else:
            for j in range(1, len(result) - 1):
                if i <= result[j] and i >= result[j+1]:
                    result.insert(j+1, i)
                    break
    return result[:k]


def top_k(lst, k):
    new=[]
    for i in range(k):
        biggest=lst[0]
        for element in lst:
            if element>biggest:
                biggest=element
        new.append(biggest)
        lst.remove(biggest)
    return new


def top_k(lst, k):
    sotsot = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sotsot.append(largest)
    return sotsot[:k]


def top_k(lst, k):

    if lst == []:
        return lst
    def sorting(nlist):
        if nlist == []:
            return nlist
        lower = []
        higher = []
        plist = []
        pivot = nlist[0]
        for e in nlist:
            if e < pivot:
                lower.append(e)
            elif e == pivot:
                plist.append(e)
            elif e > pivot:
                higher.append(e)
        higher = sorting(higher)
        lower = sorting(lower)
        return lower + plist + higher

    sort_list = sorting(lst)
    sort_list = sort_list[::-1]

    return sort_list[:k]


def top_k(lst, k):
    arranged = []
    while k > 0:
        arranged.append(max(lst))
        lst.remove(max(lst))
        k = k-1
    return arranged
    pass


def top_k(lst, k):
    if k == 0 or lst == []:
        return []
    else:
        sort, output = [], []
        while lst:
            largest = lst[0]
            for i in lst:
                if i > largest:
                    largest = i
            lst.remove(largest)
            sort.append(largest)
        for j in sort:
            output.append(j)
            if len(output) == k:
                break
        return output


def top_k(lst, k):

    def sort(lst):

        new_lst=[]
        while lst:
            bigger=lst[0]
            for i in lst:
                if i>=bigger:
                    bigger=i
            lst.remove(bigger)
            new_lst.append(bigger)

        return new_lst

    sorted_lst=sort(lst)
    return sorted_lst[:k]


def top_k(lst, k):
    results = []
    counter = 0
    while counter < k:
        for i in range(-len(lst),0):
            if lst[i] == max(lst):
                results.append(lst.pop(i))
                break
        counter += 1
    return results


def merge_sort(lst):
    if len(lst) < 2:
        return lst
    mid = len(lst) // 2
    left = merge_sort(lst[:mid])
    right = merge_sort(lst[mid:])
    return merge(left, right)

def merge(left, right):
    results = []
    while left and right:
        if left[0] < right[0]:
            results.append(left.pop(0))
        else:
            results.append(right.pop(0))
    results.extend(left)
    results.extend(right)
    return results
def top_k(lst, k):
    result=merge_sort(lst)
    result.reverse()
    answer=[]
    for i in range(0,k):
        answer=answer+[result[i]]
    return answer
    pass


def top_k(lst,k):
    newlst=[]
    while lst:
        highest=lst[0] #first number
        for number in lst:
            if number>highest:
                highest=number
        for number in lst:
            if number==highest:
                newlst.append(number)
                lst.remove(number)
    return newlst[:k]


def top_k(lst, k):
    n = len(lst)
    new =[]
    for i in range(0,n):
        largest = max(lst)
        lst.remove(largest)
        new.append(largest)
    return new[:k]


def top_k(lst, k):
    ret = []
    for i in range(k):
        max_int_pos = lst.index(max(lst))
        ret.append(lst.pop(max_int_pos))
    return ret


def top_k(lst, k):
    sorted_lst = []
    while lst:
        biggest = lst[0]
        for n in range(len(lst)):
            if lst[n] >= biggest:
                biggest = lst[n]
        lst.remove(biggest)
        sorted_lst.append(biggest)
    return sorted_lst[0:k]


def top_k(lst, k):
    # Fill in your code here
    a = []
    while len(a)<k:
        a += [max(lst),]
        lst.remove(max(lst))
    return a


def sort(lst):
	newlst=[]
	while lst:

		minimum=lst[0]
		for x in lst:
			if x>minimum:
				minimum=x
		newlst.append(minimum)
		lst.remove(minimum)
	return newlst

def top_k(lst, k):
    lst=sort(lst)
    return lst[:k]
    # Fill in your code here


def top_k(lst, k):
    new_lst = []
    while len(new_lst) < k:
        maximum = max(lst)
        new_lst.append(maximum)
        lst.remove(maximum)
    return new_lst


def top_k(lst, k):
    def sorter(lst):
        newlst = []
        while lst:
            current = lst[0]
            for element in lst:
                if element > current:
                    current = element
            newlst.append(current)
            lst.remove(current)
        return newlst
    return sorter(lst)[:k]


def top_k(lst, k):
    sorted_list = []
    while lst:
        l = lst[0]
        for i in lst:
            if i > l:
                l = i
        lst.remove(l)
        sorted_list.append(l)
    return sorted_list[:k]


def top_k(lst, k):
    result = []
    while lst:
        biggest = lst[0]
        for number in lst:
            if number > biggest:
                biggest = number
        lst.remove(biggest)
        result.append(biggest)
    return result[:k]


def top_k(lst, k):
    # Fill in your code here
    sort = []
    while lst: # a is not []
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    # Fill in your code here
    def ins_sort(a):
        i = 1
        while i < len(a):
            j = i
            while j>0 and a[j-1] > a[j]:
                a[j], a[j-1] = a[j-1], a[j]
                j-=1
            i+=1
        return a[::-1]

    lst_sorted = ins_sort(lst)

    return lst_sorted[:k]


def top_k(lst, k):
    arranged = []
    while k > 0:
        arranged.append(max(lst))
        lst.remove(max(lst))
        k = k-1
    return arranged
    pass


def top_k(lst, k):
    results = []
    for i in range(k):
        results.append(max(lst))
        lst.remove(max(lst))
    return results


def top_k(lst, k):
    def mergesort(lst):
        if len(lst) > 1:
            mid = len(lst)//2
            leftlst = lst[:mid]
            rightlst = lst[mid:]
            mergesort(leftlst)
            mergesort(rightlst)
            i = 0
            j = 0
            k = 0
            while i < len(leftlst) and j < len(rightlst):
                if leftlst[i] >= rightlst[j]:
                    lst[k] = leftlst[i]
                    i = i + 1
                else:
                    lst[k] = rightlst[j]
                    j = j + 1
                k = k + 1
            while i < len(leftlst):
                lst[k] = leftlst[i]
                i = i + 1
                k = k + 1
            while j < len(rightlst):
                lst[k] = rightlst[j]
                j = j + 1
                k = k + 1
    mergesort(lst)
    return lst[:k]


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        sort.append(largest)
        lst.remove(largest)
    top = sort[:k]
    return top


def top_k(lst, k):
    new_lst = []
    while len(new_lst) != k:
        new_lst.append(max(lst))
        lst.remove(max(lst))
    return new_lst


def top_k(lst,k):
    sort = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        sort.append(biggest)
    return sort[:k]


def top_k(lst, k):
    klist = []
    while len(klist) < k:
        biggest = lst[0]
        for i in lst:
            if i > biggest:
                biggest = i
        klist.append(biggest)
        lst.remove(biggest)
    return klist


def top_k(lst, k):
	lst=sort(lst)
	lst=lst[:k]
	return lst

def sort(lst):
	for i in range(0,len(lst)-1):
		   for j in range(i+1,len(lst)):
			   if lst[i]<lst[j]:
				   lst[i],lst[j]=lst[j],lst[i]
	return lst


def top_k(lst, k):
    result = []
    while lst:
      biggest = lst[0]
      for e in lst:
        if e > biggest:
            biggest = e
      result.append(biggest)
      lst.remove(biggest)
    return result[:k]


def top_k(lst, k):
	new_lst = lst
	end = []
	for a in range(k):
		end.append(max(new_lst))
		new_lst.remove(max(new_lst))
	return end


def sort_by_value(lst):
	final=[]
	while lst:
		largest=lst[0]
		for i in lst:
			if largest<i:
				largest=i
		lst.remove(largest)
		final.append(largest)
	return final

def top_k(lst,k):
    return sort_by_value(lst)[:k]


def top_k(lst, k):
    rs=[]
    for qwerty in range(0,k):
        biggest=lst[0]
        for k in lst:
            if biggest<k:
                biggest=k
        rs.append(biggest)
        lst.remove(biggest)
    return rs


def top_k(lst, k):
    # Fill in your code here
    a = []
    while len(a)<k:
        a += [max(lst),]
        lst.remove(max(lst))
    return a


def top_k(lst, k):
    new_list = []
    while len(new_list) != k:
        largest = lst[0]
        for i in lst:
            if largest < i:
                largest = i
            else:
                continue
        new_list += [largest]
        lst.remove(largest)
    return new_list


def top_k(lst,k):
	def sort_numbers(lst):
		result=[]
		def lame(lst):
			if lst==[]:
				return result
			result.append(max(lst))
			lst.remove(max(lst))
			return lame(lst)
		return lame(lst)
	return sort_numbers(lst)[:k]


def top_k(lst, k):
    if k == 1:
        return [max(lst)]
    else:
        answer = []
        for i in range(min(k, len(lst))):
            largest = max(lst)
            answer.append(largest)
            lst.remove(largest)
        return answer


def top_k(lst, k):
    result = 0
    new_list = []
    while result < k:
        temp = max(lst)
        new_list.append(temp)
        lst.remove(temp)
        result = result + 1
    return new_list


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for e in lst:
            if e > largest:
                largest = e
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    # Fill in your code here
    sort_lst = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        sort_lst.append(biggest)
    return sort_lst[:k]


def top_k(lst, k):
    sort = []
    while lst:
        biggest = lst[0]
        for i in lst[1:]:
            if i > biggest:
                biggest = i
        lst.remove(biggest)
        sort.append(biggest)

    n = 1
    sort_k = []
    while n <= k:
        sort_k.append(sort.pop(0))
        n += 1

    return sort_k


def top_k(lst, k):
    if lst==[]:
        return []
    sort=[]
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]
    pass


def top_k(lst, k):
    def sortme(lst):
        sort=[]
        while lst:
            largest=lst[0]
            for i in lst:
                if i>largest:
                    largest=i
            lst.remove(largest)
            sort.append(largest)
        return sort
    return sortme(lst)[:k]


def top_k(lst, k):
    def merge_sort(lst):
        if len(lst) < 2:  # Base case!
            return lst
        mid = len(lst) // 2
        left = merge_sort(lst[:mid])  #sort left
        right = merge_sort(lst[mid:]) #sort right
        return merge(left, right)
    def merge(left, right):
        results = []
        while left and right:
            if left[0] > right[0]:
                results.append(left.pop(0))
            else:
                results.append(right.pop(0))
        results.extend(left)
        results.extend(right)
        return results
    lst = merge_sort(lst)
    return lst[:k]


def top_k(lst, k):
    sort = []
    while lst:
        oldest = lst[0]
        for x in lst:
            if x > oldest:
                oldest = x
        lst.remove(oldest)
        sort.append(oldest)
    return sort[0:k]


def top_k(lst, k):
    # Fill in your code here
    out = [lst[0],]
    for ele in lst[1:]:
        for indx in range(len(out)):
            if out[indx] <= ele:
                out.insert(indx, ele)
                break
            elif indx == len(out) - 1:
                out.append(ele)
    return out[0:k]


def top_k(lst, k):
    def merge_lists(left,right):
        new = []
        while left and right:
            if left[0] >= right[0]:
                new.append(left.pop(0))
            else:
                new.append(right.pop(0))
        new.extend(left)
        new.extend(right)
        return new

    def binary_sort(lst):
        if len(lst) <2:
            return lst
        mid = len(lst)//2
        left = binary_sort(lst[:mid])
        right = binary_sort(lst[mid:])
        return merge_lists(left,right)

    new = binary_sort(lst)
    return new[:k]


def top_k(lst, k):
    sorted_lst = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sorted_lst.append(largest)
    return sorted_lst[:k]
    pass


def top_k(lst, k):
    newlist = []
    while len(newlist) < k:
        newlist += [max(lst)]
        for i in range(len(lst)):
            if lst[i] == max(lst):
                break
        del lst[i]
    return newlist


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        sort.append(largest)
    print(sort)
    return sort[:k]


def top_k(lst, k):
    lst=sort_list(lst)
    while len(lst)>k:
        lst.pop(-1)
    return lst

def sort_list(lst):
    sort=[]
    while lst:
        largest=lst[0]
        for i in lst:
            if i>largest:
                largest=i
        lst.remove(largest)   #1.indentation: under while loop
        sort+=[largest]
    return sort

#1. find the largest first, then remove it from the lst,
#then append the sort


def top_k(lst, k):
    # Fill in your code here
    sort_lst=sort(lst)
    return sort_lst[:k]

def sort(lst):
    sort=[]
    while lst:
        largest=lst[0]
        for elem in lst:
            if elem > largest:
                largest = elem
        lst.remove(largest)
        sort.append(largest)
    return sort


def top_k(lst, k):
    sorted_list = []
    while lst:
        smallest = lst[0]
        for element in lst:
            if element < smallest:
                smallest = element
        lst.remove(smallest)
        sorted_list.append(smallest)
    final = sorted_list[::-1]
    return final[:k]


def top_k(lst, k):
    if k == 0 or lst == []:
        return []
    else:
        sort, output = [], []
        while lst:
            largest = lst[0]
            for i in lst:
                if i > largest:
                    largest = i
            lst.remove(largest)
            sort.append(largest)
        for j in sort:
            output.append(j)
            if len(output) == k:
                break
        return output


def top_k(lst, k):
    n= []
    while lst:
        big = lst[0]
        for ele in lst:
            if ele>big:
                big = ele
        if len(n)==k:
            break
        else:
            n.append(big)
            lst.remove(big)
    return n

    pass


def top_k(lst, k):
    sorted = []
    while lst:
        biggest = lst[0]
        for elem in lst:
            if elem > biggest:
                biggest = elem
        lst.remove(biggest)
        sorted.append(biggest)
    return sorted[:k]


def top_k(lst, k):
    result = []
    for i in range(len(lst)):
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        result.append(largest)
    return result[:k]


def top_k(lst, k):
    new_lst = []
    while lst:
        largest = lst[0]
        for numbers in lst:
            if numbers > largest:
                largest = numbers
        new_lst.append(largest)
        lst.remove(largest)
    return new_lst[:k]


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i>largest:
                largest = i
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    new_lst = []
    if k == 0 or lst == []:
        return []
    else:
        while k > 0:
            new_lst.append(max(lst))
            lst.remove(new_lst[-1])
            k -= 1
        return new_lst


def top_k(lst, k):
    result = []
    for x in range(k):
        result.append(max(lst))
        lst.remove(max(lst))
    return result


def top_k(lst, k):
    new = []
    i = 1
    while i <= k:
        element = find_largest(lst)
        new.append(element)
        lst.remove(element)
        i = i+1
    return new

def find_largest(lst):
    largest = lst[0]
    for element in lst:
        if element > largest:
            largest = element
    return largest


def top_k(lst, k):
    final = []
    while lst:
        biggest = lst[0]
        for i in lst:
            if i>biggest:
                biggest = i
        lst.remove(biggest)
        final.append(biggest)
    return final[:k]


def top_k(lst, k):
    result = []
    while lst:
        biggest = lst[0]
        for elem in lst:
            if elem > biggest:
                biggest = elem
        lst.remove(biggest)
        result.append(biggest)

    return result[:k]

    # Fill in your code here
    pass


def top_k(lst, k):
    lst2 = []
    lst3 = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        lst2.append(largest)
    if len(lst2) <= k:
        k = len(lst2)
    for i2 in range(k):
        lst3.append(lst2[i2])
    return lst3


def top_k(lst, k):
    newlst = []
    while len(newlst)<k:
        maximum = lst[0]
        for i in lst:
            if i > maximum:
                maximum = i
        newlst.append(maximum)
        lst.remove(maximum)
    return newlst


def sort_no(lst):
    new = []
    while lst:
        Max = max(lst,key = lambda x : x)
        new.append(Max)
        lst.remove(Max)
    return new

def top_k(lst, k):
    Sorted = sort_no(lst)
    return Sorted[0:k]


def top_k(lst, k):
    def sort(lst2):
        holder=[]
        if lst==[]:
            return []
        for x in lst:
            if holder==[]:
                holder=x
            elif x>holder:
                holder=x
        lst.remove(holder)
        return [holder]+sort(lst)
    counter=k
    final=[]
    new_lst=sort(list)
    while counter>0:
	    a=new_lst.pop(0)
	    final.append(a)
	    counter-=1
    return final


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[0:k]


def top_k(lst, k):
    # Fill in your code here
    out = [lst[0],]
    for ele in lst[1:]:
        for indx in range(len(out)):
            if out[indx] <= ele:
                out.insert(indx, ele)
                break
            elif indx == len(out) - 1:
                out.append(ele)
    return out[0:k]


import heapq
def top_k(lst, k):
    k_sorted = heapq.nlargest(k, lst)
    return k_sorted


def top_k(lst, k):
    def rank(lyst):
        if len(lyst)==1:
            return list(lyst)
        else:
            a = max(lyst)
            lyst.remove(a)
            return [a]+ rank(lyst)
    new_list = rank(lst)
    return new_list[:k]


def top_k(lst, k):
    a = lst
    sort=[]
    while a:
        smallest = a[0]
        for element in a:
            if element<smallest:
                smallest = element
        a.remove(smallest)
        sort.append(smallest)
    sort.reverse()
    return sort[0:k]


def top_k(lst, k):
    max_lst =[]
    i = 0
    while i < k:
        max_lst.append(max(lst))
        lst.remove(max(lst))
        i += 1
    return max_lst


def sort_descending(lst):
    new_lst = []
    while lst:
        largest = lst[0]
        for i in range(len(lst)):
            if lst[i] > largest:
                largest = lst[i]
        lst.remove(largest)
        new_lst.append(largest)
    return new_lst

def top_k(lst, k):
    lst = sort_descending(lst)
    return lst[:k]


def top_k(lst, k):
    newlst = []
    while lst:
        largest = lst[0]
        for number in lst:
            if number > largest:
                largest = number
        lst.remove(largest)
        newlst.append(largest)
    result = []
    for counter in range(k):
        result = result + [newlst[counter],]
    return result


def sort(lst):
	sort = []
	while lst:
		largest = lst[0]
		for element in lst:
			if element > largest:
				largest = element
		lst.remove(largest)
		sort.append(largest)
	return sort

def top_k(lst, k):
	sorted_list = sort(lst)
	result = sorted_list[:k]
	return result


def top_k(lst, k):
    unsorted = lst
    result = []
    while True:
        if len(result) == k:
            break
        result += [max(unsorted),]
        unsorted.remove(max(unsorted))
    return result


def top_k(lst, k):
    if k>len(lst):
        return None
    def newsort(lsts):
        l=[]
        while lsts:
            biggest=lsts[0]
            for element in lsts:
                if element>biggest:
                    biggest=element
            l.append(biggest)
            lsts.remove(biggest)
        return l
    newlist=newsort(lst)
    return newlist[0:k]


def top_k(lst, k):

    newlist = []
    counter = k

    while counter > 0:
        biggest = lst[0]
        for i in lst[1:]:
            if biggest > i:
                continue
            else:
                biggest = i
                continue

        newlist += [biggest]
        counter -= 1
        lst.remove(biggest)

    return newlist


def top_k(lst, k):
    a = []
    while k > 0:
        a.append(max(lst))
        lst.remove(max(lst))
        k -= 1
    return a


def sort_no(lst):
    lst_len = len(lst) - 1
    while lst_len > 0:
        for i in range(lst_len):
            if lst[i] > lst[i+1]:
                lst[i], lst[i+1] = lst[i+1], lst[i]
        lst_len -= 1
    return lst[::-1]
def top_k(lst, k):
    return sort_no(lst)[:k]


def get_largest(lst):
    for i in range (len(lst)-1):
        if lst[i]> lst[i+1]:
            lst[i],lst[i+1] = lst[i+1],lst[i]

    return lst[-1]

def top_k(lst, k):
    result = []
    for i in range (len(lst)):
        if len(result) < k:
            largest = get_largest(lst)
            lst.remove(largest)
            result.append(largest)
    return result


def bubble_sort(seq):
    changed = True
    while changed:
        changed = False
        for i in range(len(seq) - 1):
            if seq[i] < seq[i+1]:
                seq[i], seq[i+1] = seq[i+1], seq[i]
                changed = True
    return None
def top_k(lst, k):
    bubble_sort(lst)
    return lst[:k]


def top_k(lst, k):

    answer = []
    a = lst[0]

    while lst:

        for i in range(len(lst)):
            answer.append(max(lst))
            lst.remove(max(lst))

    return answer[:k]


def top_k(lst, k):
    new_list = []
    while lst:
        maximum = lst[0]
        for i in lst:
            if i > maximum:
                maximum = i
        new_list.append(maximum)
        lst.remove(maximum)
    new_list_2 = []
    counter = 0
    for i in new_list:
        if counter < k:
            new_list_2.append(new_list[counter])
            counter = counter + 1
    return new_list_2


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    if lst == [] or k == 0:
        return []
    else:
        final = []
        while lst:
            element = max(lst)
            final += [element,]
            lst.remove(element)
            if len(final) == k:
                break
        return final


def top_k(lst, k):
    for item in range(len(lst)):
        for test in range(len(lst)):
            if lst[test] < lst[item]:
                lst[test], lst[item] = lst[item], lst[test]
    return lst[:k]


def top_k(lst, k):
    l=lst.copy()
    res=[]
    while len(res)<k and l :
        biggest=l[0]

        for element in l:
            if element>biggest:
                biggest=element

        l.remove(biggest)
        res.append(biggest)
    return res


def top_k(lst, k):
    sorted_list = []
    biggest = []
    while lst:
        largest = 0
        for i in lst:
            if i >= largest:
                largest = i
        lst.remove(largest)
        sorted_list.append(largest) # from biggest to smallest
    for j in range (k):
        biggest.append(sorted_list[j])
    return biggest
    pass


def top_k(lst,k):
    swap = True
    while swap:
        swap = False
        for i in range(len(lst)-1):
            if lst[i] < lst[i+1]:
                lst[i],lst[i+1] = lst[i+1], lst[i]
                swap = True
    while len(lst) > k:
        lst.pop(-1)
    return lst


def top_k(lst, k):
    new = []
    counter = k
    while counter > 0:
        curr = 0
        for i in range(len(lst)):
            if lst[i] > lst[curr]:
                curr = i
        new.append(lst.pop(curr))
        counter -= 1
    return new


def top_k(lst, k):
    def sort(lst):
        sort_list = []
        while lst: # a is not []
            biggest = lst[0]
            for element in lst:
                if element > biggest:
                    biggest = element
            lst.remove(biggest)
            sort_list.append(biggest)
        return sort_list
    return sort(lst)[:k]


def top_k(lst, k):
    sort = []
    counter = 0
    while counter != k:
        sort.append(max(lst))
        lst.remove(max(lst))
        counter += 1
    return sort


def top_k(lst, k):
    af_sort = []
    while lst:
        biggest = lst[0]
        for element in lst:
            if element > biggest:
                biggest = element
        lst.remove(biggest)
        af_sort.append(biggest)
    return af_sort[0:k]


def top_k(lst, k):
    i=0
    while i+1<len(lst):
        if lst[i]<lst[i+1]:
            lst.extend([lst[i]])
            lst.pop(i)
            i=0
        else:
            i+=1
    return lst[:k]


def top_k(lst, k):
    a = lst
    sort=[]
    while a:
        smallest = a[0]
        for element in a:
            if element<smallest:
                smallest = element
        a.remove(smallest)
        sort.append(smallest)
    sort.reverse()
    return sort[0:k]


def top_k(lst, k):
    a = []
    while k > 0:
        a.append(max(lst))
        lst.remove(max(lst))
        k -= 1
    return a


def top_k(lst, k):
    newlist = []
    while len(newlist)<k:
        newlist.append(max(lst))
        lst.remove(max(lst))
    return newlist


def top_k(lst, k):
    result=[]
    for i in range(0,k):
        max=lst[0]
        for j in lst:
            if j>max:
                max=j
        result.append(max)
        lst.remove(max)
    return result# Fill in your code here
    pass


def top_k(lst, k):
	new_lst = []
	while lst:
		largest = lst[0]
		for i in lst:
			if i > largest:
				largest = i
		lst.remove(largest)
		new_lst.append(largest)
	return new_lst[:k]


def top_k(lst, k):
    sorted = []
    while lst:
        biggest = lst[0]
        for elem in lst:
            if elem > biggest:
                biggest = elem
        lst.remove(biggest)
        sorted.append(biggest)
    return sorted[:k]


def top_k(lst, k):
    lst2 = []
    largest = lst[0]
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        lst2.append(largest)
    return lst2[0:k]


def top_k(lst,k):
    result=[]
    while lst:
        minimum=lst[0]
        for i in lst:
            if i<minimum:
                minimum=i
        result.append(minimum)
        lst.remove(minimum)
    result=result[::-1]
    return result[:k]


def top_k(lst, k):
    descending_list=[]
    while lst:
        largest=lst[0]
        for i in lst:
            if i>largest:
                largest=i
        descending_list.append(largest)
        lst.remove(largest)
    return descending_list[:k]


def top_k(lst, k):
    a=[]
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        a.append(largest)
    return a[:k]


def top_k(lst, k):
    new_lst = []

    while lst:
        big = lst[0]
        for i in lst:
            if i > big:
                big = i
        lst.remove(big)
        new_lst.append(big)

    return new_lst[:k]


def top_k(lst, k):
	def merge_sort(lst):
		if len(lst) < 2:
		    	return lst
		mid = len(lst) // 2
		left = merge_sort(lst[:mid])
		right = merge_sort(lst[mid:])
		return merge(left, right)
	def merge(left, right):
		results = []
		while left and right:
			if left[0] > right[0]:
				results.append(left.pop(0))
			else:
				results.append(right.pop(0))
		results.extend(left)
		results.extend(right)
		return results
	return merge_sort(lst)[:k]


def top_k(lst, k):
    def sort(lst):
        sort = True
        while sort:
            sort = False
            for i in range(len(lst)-1):
                if lst[i] < lst[i+1]:
                    sort = True
                    lst[i], lst[i+1] = lst[i+1], lst[i]
        return lst
    lsts = sort(lst)
    return lsts[:k]


def top_k(lst, k):
    new_list = []
    while len(new_list) != k:
        largest = lst[0]
        for i in lst:
            if largest < i:
                largest = i
            else:
                continue
        new_list += [largest]
        lst.remove(largest)
    return new_list


def top_k(lst, k):
    lst = sort_descending(lst)
    return lst[:k]

def sort_descending(lst):
    for i in range(len(lst)-1):
        for j in range(i, len(lst)):
            if lst[j] > lst[i]:
                x = lst[i]
                lst[i] = lst[j]
                lst[j] = x
    return lst


def top_k(lst, k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if element > largest:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[:k]


def top_k(lst, k):
    new_lst = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        new_lst.append(largest)
    return new_lst[0:k]


def top_k(lst,k):
    def selection_sort(lst):
        srt = []
        while lst:
            smallest = lst[0]
            for elem in lst:
                if elem < smallest:
                    smallest = elem
            lst.remove(smallest)
            srt.append(smallest)
        return srt
    return selection_sort(lst)[-1:-k-1:-1]


def top_k(lst, k):
    lst2 = []
    lst3 = []
    while lst:
        largest = lst[0]
        for i in lst:
            if i > largest:
                largest = i
        lst.remove(largest)
        lst2.append(largest)
    if len(lst2) <= k:
        k = len(lst2)
    for i2 in range(k):
        lst3.append(lst2[i2])
    return lst3


def top_k(lst, k):
	for start in range(len(lst)-1):
		maximum = start
		for i in range(start, len(lst)):
			if lst[i] > lst[maximum]:
				maximum = i
		lst[maximum], lst[start] = lst[start], lst[maximum]
	return lst[:k]


def top_k(lst, k):
    count = 0
    op = []
    while count < k:
        op += [max(lst)]
        lst.remove(max(lst))
        count += 1
    return op


def top_k(lst, k):
    results = []
    for i in range(k):
        results.append(max(lst))
        lst.remove(max(lst))
    return results


def top_k(lst, k):
    a=[]
    while lst:
        for i in lst:
            a.append(max(lst))
            lst.remove(max(lst))
    return a[0:k]


def top_k(lst, k):
    new_lst = []
    while len(new_lst) < k:
        new_lst.append(max(lst))
        lst.remove(max(lst))
    return new_lst


def top_k(lst, k):
    lst = sorting(lst)
    new = []
    for i in range(k):
        new.append(lst[0])
        del lst[0]
    return new

def sorting(lst):
    sorted_lst = []
    while lst:
        smallest = lst[0]
        for i in lst:
            if i < smallest:
                smallest = i
        lst.remove(smallest)
        sorted_lst.append(smallest)
    sorted_lst.reverse()
    return sorted_lst


def top_k(lst, k):
    for i, e in enumerate(lst):
        mx = max(range(i,len(lst)), key= lambda x: lst[x])
        lst[i], lst[mx] = lst[mx], e
    return lst[:k]


def top_k(lst, k):
    l=lst.copy()
    res=[]
    while len(res)<k and l :
        biggest=l[0]

        for element in l:
            if element>biggest:
                biggest=element

        l.remove(biggest)
        res.append(biggest)
    return res


def sort_descending(lst):
    new_lst = []
    while lst:
        largest = lst[0]
        for i in range(len(lst)):
            if lst[i] > largest:
                largest = lst[i]
        lst.remove(largest)
        new_lst.append(largest)
    return new_lst

def top_k(lst, k):
    lst = sort_descending(lst)
    return lst[:k]


def top_k(lst, k):
	if lst == [] or k == 0:
		return []
	sort = []
	while lst:
		largest = lst[0]
		for element in lst:
			if element > largest:
				largest = element
		lst.remove(largest)
		sort.append(largest)
		if len(sort) > k-1:
			break
	return sort


def top_k(lst,k):
    sort = []
    while lst:
        largest = lst[0]
        for element in lst:
            if largest < element:
                largest = element
        lst.remove(largest)
        sort.append(largest)
    return sort[0:k]


def top_k(lst, k):
    sortedlst = []
    while len(sortedlst) != k:
        largest = lst[0]
        for el in lst:
            if el > largest:
                largest = el
        sortedlst.append(largest)
        lst.remove(largest)
    return sortedlst