"""
Example: enumerate()
"""

my_list = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
for index, value in enumerate(my_list):
    if index < 5:
	    print(index + 1,value, end="; ")

# Move to the next line
print()

# Output:
# 1 Mon; 2 Tue; 3 Wed; 4 Thu; 5 Fri;

"""
Example: Multiplication Table
"""

# Loop through the numbers 1 to 10 (this will be the first factor in multiplication)
for x in range(10):
    # Loop through the numbers 1 to 10 (this will be the second factor in multiplication)
    for y in range(10):
        v = (x + 1) * (y + 1)
        # Print the result 'v', formatted to take up to 4 spaces for alignment, and stay on the same line
        print(f"{v:4}", end="")
    # Move to the next line after completing a row of the table
    print()

"""
Example: Write code that reads the elements of an input list, which is sorted in increasing order, and copies them to the output list. Copying stops as soon as a value larger than the threshold is encountered in the input list or when the entire input list is copied. The program prints the output list and its number of elements.
"""

# The program assumes in_list has been already filled in (using input() or a file) and sorted
in_list = [9, 10, 14, 17, 25, 28, 29, 44, 46,
           54, 56, 57, 59, 61, 64, 71, 74, 90, 94, 95]
threshold = int(
    input(f"Enter a threshold between {min(in_list)} and {max(in_list)} = "))

# Create an empty list to store the output
out_list = []

# Iterate through each element in the sorted input list
for elem in in_list:
    # Check if the current element exceeds the threshold
    if elem > threshold:
        # If it does, exit the loop
        break
    out_list.append(elem)

# Printing
print(out_list)
print(f"Output list has {len(out_list)} elements.")


# Print the number of elements in the output list using len() to get the length
print(f"Output list has {len(out_list)} elements.")

"""
Write code that copies only odd numbers from a list of numbers to an output list, skipping over any even numbers.
"""

# The program assumes in_list has been already filled in (using input() or a file) and sorted
in_list = [9, 10, 14, 17, 25, 28, 29, 44, 46,
           54, 56, 57, 59, 61, 64, 71, 74, 90, 94, 95]

# Create an empty list to store the output
out_list = []
for num in in_list:  # Iterate through the list of numbers
    # If the number is even, skip the remaining instructions
    # and move to the next iteration
    if not (num % 2):
        continue
    # If the number is odd, append it to the output list
    out_list.append(num)
print(out_list)

"""
Example: Creating a 5x5 matrix
"""

# Solution 1
# Using nested loops

matrix = []
for row in range(5):
    matrix.append([])  # Append an empty sublist inside the list
    for col in range(5):
        matrix[row].append(col)
print(matrix)

# Output:
# [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

# Solution 2
# Nested list comprehension
matrix = [[col for col in range(5)] for row in range(5)]
print(matrix)

"""
Example: Write a block of code that traverses an input matrix
and generates a list of odd numbers found in the matrix.
"""

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Solution 1
# Using nested loops

odd_numbers = []
for row in matrix:
    for element in row:
        if element % 2:
            odd_numbers.append(element)
print(odd_numbers)

# Solution 2
# Using nested list comprehension

odd_numbers = [element for row in matrix
                for element in row
                  if element % 2 != 0]

print(odd_numbers)