# Skeleton Program for Advaspire Mock Examination 2
# International GCSE Computer Science (9210) Paper 1 Programming
# Developed using Python 3
# To be released to candidates before the examination

MAX_RUNNERS = 20
MAX_NAME_LENGTH = 12
EXAMPLE_NAMES = ["Ava", "Leo", "Mia", "Noah", "Zara", "Omar", "Ella", "Kai"]
EXAMPLE_TIMES = ["3:42", "4:05", "3:18", "4:05", "2:57", "3:30", "4:51", "3:18"]

def ValidateTime(timeText):
    if len(timeText) < 4 or len(timeText) > 5:
        return False
    if timeText[-3] != ":":
        return False
    minutesPart = timeText[:-3]
    secondsPart = timeText[-2:]
    if not minutesPart.isdigit() or not secondsPart.isdigit():
        return False
    if int(secondsPart) > 59:
        return False
    return True

def ConvertTimeToSeconds(timeText):
    minutes = int(timeText[:-3])
    seconds = int(timeText[-2:])
    return minutes * 60 + seconds

def FormatTime(totalSeconds):
    minutes = totalSeconds // 60
    seconds = totalSeconds % 60
    if seconds < 10:
        secondsText = "0" + str(seconds)
    else:
        secondsText = str(seconds)
    return str(minutes) + ":" + secondsText

def ValidateName(name):
    if len(name) < 1 or len(name) > MAX_NAME_LENGTH:
        return False
    for character in name:
        if not character.isalpha():
            return False
    return True

def GetName():
    name = input("Enter the runner's name: ").strip()
    while not ValidateName(name):
        print("A name must be 1 to " + str(MAX_NAME_LENGTH) + " letters")
        name = input("Enter the runner's name: ").strip()
    return name

def GetTime():
    timeText = input("Enter the time (minutes:seconds, for example 3:42): ").strip()
    while not ValidateTime(timeText):
        print("That is not a valid time")
        timeText = input("Enter the time (minutes:seconds, for example 3:42): ").strip()
    return timeText

def AddResult(names, times):
    if len(names) >= MAX_RUNNERS:
        print("The results list is full")
    else:
        name = GetName()
        timeText = GetTime()
        names.append(name)
        times.append(ConvertTimeToSeconds(timeText))
        print(name + " added with a time of " + FormatTime(times[len(times) - 1]))

def DisplayResults(names, times):
    print()
    print("Name          Time")
    for index in range(len(names)):
        print(names[index].ljust(14) + FormatTime(times[index]))
    print()

def FindFastest(names, times):
    if len(times) == 0:
        print("There are no results yet")
    else:
        fastestIndex = 0
        for index in range(1, len(times)):
            if times[index] < times[fastestIndex]:
                fastestIndex = index
        print("Fastest runner: " + names[fastestIndex] + " in " + FormatTime(times[fastestIndex]))

def SortResults(names, times):
    swapped = True
    while swapped:
        swapped = False
        for index in range(len(times) - 1):
            if times[index] > times[index + 1]:
                temp = times[index]
                times[index] = times[index + 1]
                times[index + 1] = temp
                temp = names[index]
                names[index] = names[index + 1]
                names[index + 1] = temp
                swapped = True
    print("Results sorted from fastest to slowest")

def SearchForRunner(names, times):
    target = input("Enter the name to search for: ").strip()
    found = False
    index = 0
    while index < len(names) and not found:
        if names[index].upper() == target.upper():
            found = True
        else:
            index = index + 1
    if found:
        print(names[index] + "'s time is " + FormatTime(times[index]))
    else:
        print(target + " was not found")

def LoadExampleResults():
    names = []
    times = []
    for index in range(len(EXAMPLE_NAMES)):
        names.append(EXAMPLE_NAMES[index])
        times.append(ConvertTimeToSeconds(EXAMPLE_TIMES[index]))
    print("Example results loaded")
    return names, times

def DisplayMenu():
    print("RACE RESULTS")
    print("1. Display results")
    print("2. Add a result")
    print("3. Show fastest runner")
    print("4. Sort results")
    print("5. Search for a runner")
    print("6. Load example results")
    print("Q. Quit")

def Main():
    names = []
    times = []
    choice = ""
    while choice != "Q":
        DisplayMenu()
        choice = input("Enter your choice: ").upper()
        if choice == "1":
            DisplayResults(names, times)
        elif choice == "2":
            AddResult(names, times)
        elif choice == "3":
            FindFastest(names, times)
        elif choice == "4":
            SortResults(names, times)
        elif choice == "5":
            SearchForRunner(names, times)
        elif choice == "6":
            names, times = LoadExampleResults()
        elif choice == "Q":
            print("Goodbye")
        print()

if __name__ == "__main__":
    Main()
