Skip to content

Fix errors in Advanced Functions notebook #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions 12_functions_advanced.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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}\")"
]
},
Expand All @@ -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\")"
]
},
{
Expand Down Expand Up @@ -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}\")"
]
},
Expand All @@ -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\")"
]
},
{
Expand Down
28 changes: 27 additions & 1 deletion tutorial/tests/test_12_functions_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()."
)


#
Expand Down