"""
Recommended Python Style Guide:
https://peps.python.org/pep-0008/#introduction
"""

from random import randint

# Create a list filled with 20 random integers between 1 and 100
# List comprehension for brevity
numbers = [randint(1, 100) for i in range(20)]

"""
Function to find the index of the minimum value in a list
from a specified starting index.
"""


def find_min_index(l, index_start=0):
    # Validate index_start
    if not isinstance(index_start, int) or index_start < 0:
        print('Error in find_min_index: Invalid value for index_start')
        return None

    # Validate the list
    if not l:
        print('Error in find_min_index: The list is empty.')
        return None

    index_min = index_start  # Initialize minimum index
    # Iterate through the list to find the minimum
    for i in range(index_start + 1, len(l)):
        if l[i] < l[index_min]:
            index_min = i  # Update index_min if a new minimum is found
    return index_min


# Test cases for the find_min_index function
print('Example 1: find_min_index([3, 1, 4, 1, 18]) =',
      find_min_index([3, 1, 4, 1, 18]))  # Expected output: 1

print('Example 2: find_min_index([3, 1, 4, 1, 18], 0) =',
      find_min_index([3, 1, 4, 1, 18], 0))  # Expected output: 1

print('Example 3: find_min_index([3, 1, 4, 1, 18], index_start=2) =',
      find_min_index([3, 1, 4, 1, 18], index_start=2))  # Expected output: 3

print('Example 4: find_min_index([3, 1, 4, 1, 18], index_start=4) =',
      find_min_index([3, 1, 4, 1, 18], index_start=4))  # Expected output: 4


# -----------------------------------------------------
# Function to sort a list in increasing order using selection sort


def selection_sort(l):
    # Check if the list is empty
    if not l:
        print('Error in selection_sort: The list is empty.')
        return  # Exit the function if the list is empty

    # Iterate over each index in the list
    for index_start in range(len(l) - 1):
        # Find the index of the minimum element
        index_min = find_min_index(l, index_start)

        # Swap the found minimum element with the first element
        # Note that Python offers a more elegant way to swap two numbers:
        # l[index_start], l[index_min] = l[index_min], l[index_start]
        # We will talk more about it in the lesson on Tuples (n-uples)

        temp = l[index_start]
        l[index_start] = l[index_min]
        l[index_min] = temp



# Print the list before and after sorting
print('List before sorting: ', numbers)
selection_sort(numbers)
print('List after sorting: ', numbers)
