diff --git a/12_functions_advanced.ipynb b/12_functions_advanced.ipynb index b4659cf4..3950c49b 100644 --- a/12_functions_advanced.ipynb +++ b/12_functions_advanced.ipynb @@ -335,10 +335,10 @@ "metadata": {}, "outputs": [], "source": [ - "from datetime import datetime\n", + "from datetime import datetime, UTC\n", "\n", "\n", - "def log(msg, *, dt=datetime.utcnow()):\n", + "def log(msg, *, dt=datetime.now(UTC)):\n", " print(f\"{dt}: {msg}\")" ] }, @@ -362,9 +362,9 @@ "source": [ "from time import sleep\n", "\n", - "log(\"my first message\")\n", + "log(\"A first message\")\n", "sleep(5)\n", - "log(\"my first message\")" + "log(\"A second message\")" ] }, { @@ -395,7 +395,7 @@ "outputs": [], "source": [ "def log(msg, *, dt=None):\n", - " dt = dt or datetime.now(datetime.timezone.utc)\n", + " dt = dt or datetime.now(UTC)\n", " print(f\"{dt}: {msg}\")" ] }, @@ -408,9 +408,9 @@ }, "outputs": [], "source": [ - "log(\"my first message\")\n", + "log(\"A first message\")\n", "sleep(5)\n", - "log(\"my first message\")" + "log(\"A second message\")" ] }, { diff --git a/tutorial/tests/test_12_functions_advanced.py b/tutorial/tests/test_12_functions_advanced.py index 4e7e11b5..8881b98d 100644 --- a/tutorial/tests/test_12_functions_advanced.py +++ b/tutorial/tests/test_12_functions_advanced.py @@ -29,7 +29,33 @@ def test_randomize_list( function_to_test: t.Callable, my_list: list[int], ): - assert function_to_test(my_list) == reference_randomize_list(my_list) + # Skip randomization tests for trivial lists + if len(my_list) <= 1: + return + + # Run the function multiple times to ensure randomization + results = [] + for _ in range(3): # Run multiple times + # Use a copy to prevent the function from modifying the original + test_list = my_list.copy() + result = function_to_test(test_list) + + # 1. Check the same elements are present + assert set(result) == set(my_list) + # 2. Check the length is preserved + assert len(result) == len(my_list) + + results.append(result) + + # For lists with enough elements, very unlikely to get the same result twice + # 1. Convert each of the results to tuples + # 2. {tuple(r) for r in results} create a set that deduplicates identical orderings + # 3. len(...) > 1 checks that we've seen at least two different orderings + if len(my_list) > 2: + # Check there's at least some variation in results + assert len({tuple(r) for r in results}) > 1, ( + "Function doesn't appear to randomize. You should not use random.seed()." + ) #