Exemple #1
0
def findMax(gL: IntList) -> int:
    """
    finds the maximum value of an IntList
    """
    if gL is None or gL.nil:
        return None
    current_max = gL.first(
    )  #assign the current max to the first one to avoid one comparison
    gL = gL.rest()  # reassign gL to be the rest of the list
    while gL.nil is False:
        if gL.first() > current_max:
            current_max = gL.first()
        gL = gL.rest()
    return current_max
Exemple #2
0
def merge(aL: IntList, bL: IntList) -> IntList:
    """
    merges two intlists together by converting them to a regular list.
    merging that.
    then converting back into an intlist.
    """
    return IntList(
        sorted(convert_to_regular_list(aL) + convert_to_regular_list(bL)))
Exemple #3
0
def list1():
    return IntList([1, 3, 5])
Exemple #4
0
def list2():
    return IntList([2, 3, 4, 5, 7])
Exemple #5
0
from intlist import IntList


def convert_to_regular_list(intlist):
    """
    Converts an IntList to a regular List.
    This makes many list based operations significantly easier
    """
    intlist = str(intlist)
    intlist = intlist[1:len(intlist) - 1]
    intlist = intlist.split(", ")
    newlist = []
    for each in intlist:
        newlist.append(int(each))
    return newlist


def merge(aL: IntList, bL: IntList) -> IntList:
    """
    merges two intlists together by converting them to a regular list.
    merging that.
    then converting back into an intlist.
    """
    return IntList(
        sorted(convert_to_regular_list(aL) + convert_to_regular_list(bL)))


print(
    merge(IntList([1, 2, 3, 4, 5, 6, 7, 8, 9]),
          IntList([1, 3, 4, 7, 6, 44, 88])))
Exemple #6
0
def insert(i:int, k:int, aL:IntList) -> IntList :
    if aL.first() == k:
        return aL.cons(i)
    return insert(i, k, aL.rest()).cons(aL.first())
Exemple #7
0
from intlist import IntList

def insert(i:int, k:int, aL:IntList) -> IntList :
    if aL.first() == k:
        return aL.cons(i)
    return insert(i, k, aL.rest()).cons(aL.first())

print(insert(1, 5, IntList([2,3,4,5,6])));
Exemple #8
0
"""
    worst-case complexity = n - 1
    average-case complexity = n - 1

    this algorithm is optimal since it only does n - 1 comparisons which is the
    minimum amount required to find the largest element in an unordered list
"""

from intlist import IntList


def findMax(gL: IntList) -> int:
    """
    finds the maximum value of an IntList
    """
    if gL is None or gL.nil:
        return None
    current_max = gL.first(
    )  #assign the current max to the first one to avoid one comparison
    gL = gL.rest()  # reassign gL to be the rest of the list
    while gL.nil is False:
        if gL.first() > current_max:
            current_max = gL.first()
        gL = gL.rest()
    return current_max


print(findMax(IntList([9, 1, 2, 3, 55, 4, 5])))