def q1(inputString, minLetter):
  
    first = None
    firstIndex = None
    second = None
    secondIndex = None
    third = None
    thirdIndex = None

    # Just go through string once, updating first, second, third when appropriate
    currentIndex = 0
    while currentIndex < len(inputString):
        char = inputString[currentIndex].lower()
        if char >= minLetter.lower():
            if (first == None) or (char < first):
                third = second
                thirdIndex = secondIndex
                second = first
                secondIndex = firstIndex
                first = char
                firstIndex = currentIndex
            elif (char != first) and ((second == None) or (char < second)):
                third = second
                thirdIndex = secondIndex
                second = char
                secondIndex = currentIndex
            elif (char != first) and (char != second) and ((third == None) or (char < third)):
                third = char
                thirdIndex = currentIndex
        currentIndex = currentIndex + 1
            
    # Traverse the string once more to find the character that occurs most often.
    # (This work could be done in the loop above, but some people find it
    # easier to understand this way.)
    # 
    mostCommonLetter = None
    maxNumOccurrences = 0
    for charToCount in inputString.lower():
        charCount = 0
        for char in inputString.lower():
            if charToCount == char:
                charCount = charCount + 1
        if charCount > maxNumOccurrences:
            maxNumOccurrences = charCount
            mostCommonLetter = charToCount
        elif ((charCount == maxNumOccurrences) and (charToCount > mostCommonLetter)):
            maxNumOccurrences = charCount
            mostCommonLetter = charToCount

    #print("In '{}', the largest letter is '{}', the third largest letter is '{}',\n and the most common letter is '{}', occurring {} times".format(inputString, first, third, mostCommonLetter, maxNumOccurrences))
    return(first, firstIndex, third, thirdIndex, mostCommonLetter, maxNumOccurrences)


def q2(L, goalX, goalY):
    currClosest = L[0]
    if abs(L[0][0]-goalX) < abs(L[0][1]-goalY):
        minDist = abs(L[0][0]-goalX) 
        which = 'XMIN'
    else:
        minDist = abs(L[0][1] - goalY)
        which = 'YMIN'
    for pt in L[1:]:
        if abs(pt[0]-goalX) < minDist:
            minDist = abs(pt[0]-goalX)
            currClosest = pt
            which = 'XMIN'
        if abs(pt[1]-goalY) < minDist:
            minDist = abs(pt[1]-goalY)
            currClosest = pt
            which = 'YMIN'
    return (currClosest, which)

def q3(L):
    sums = []
    numPos = 0
    maxNum = None
    for sublist in L:
        sum = 0
        for num in sublist:
            if (maxNum == None) or (num > maxNum):
                maxNum = num
            sum += num
        sums.append(sum)
        posExcess = 0
        for num in sublist:
            if num > 0:
                posExcess += 1
            if num < 0:
                posExcess -= 1
        if posExcess > 0:
            numPos += 1
    return [sums, numPos, maxNum]


