In [1]:
# using sorting
def max_min(nums: list[int]) -> tuple[int, int]:
    """Return a tuple containing the min and max values from nums"""
    return (sorted(nums)[0], sorted(nums)[-1])

print(max_min([4,7,8,4,2,9]))

# without sorting
def min_max(nums: list[int]) -> tuple[int, int]:
    """Return a tuple containing the min and max values from nums"""
    min = nums[0]
    max = nums[0]
    for val in nums:
        if val < min:
            min = val
        if val > max:
            max = val
    return(min, max)

print(min_max([4,7,8,4,2,9]))
(2, 9)
(2, 9)
In [ ]:
def sort_dictionary(d: dict[str, int]) -> dict[str, int]:
    """Return a new dictionary containing the same key-value pairs as
    d sorted by the length of its keys from greatest to least and in alphabetical order"""
    new_d = {}
    keys = sorted(d)
    keys = sorted(keys, key = len, reverse = True)
    for key in keys:
        new_d[key] = d[key]
    return(new_d)
    
print(sort_dictionary({"hello": 5, "apple": 7, "b": 9, "a": 8}))
print(sort_dictionary({"wow": 3,   "any": 76,  "hello": 2, "c":8}))
In [2]:
from typing import Any

def changeGrade(lst: list[dict[str, Any]]) -> None:
    """Mutate the list lst containing dictionaries describing students such that
    the student with the lowest grade will revieve a 95.0"""
    lowest_student_index = 0
    for i in range(1, len(lst)):
        if lst[i]["average"] < lst[lowest_student_index]["average"]:
            lowest_student_index = i
    lst[lowest_student_index]["average"] = 95.0
    
Student_A = {"name": "Elena",
            "number": 123456,
            "average": 100.0}
Student_B = {"name": "Elsa",
            "number": 234567,
            "average": 100.0}
Student_C = {"name": "Gregor",
            "number": 345678,
            "average": 0.0}

x= [Student_C, Student_B, Student_A]
changeGrade(x)
print(x)
[{'name': 'Gregor', 'number': 345678, 'average': 95.0}, {'name': 'Elsa', 'number': 234567, 'average': 100.0}, {'name': 'Elena', 'number': 123456, 'average': 100.0}]
In [ ]:
 
In [ ]: