Skip to content

Commit 5199d9f

Browse files
authored
Improve challenges with alternative solutions and hints (#125)
* Add alternative solution in advanced-forward challenge "from __future__ import annotations" * Improve extreme-self-casting challenge * Improve advanced-variadic-generics challenge
1 parent 6f4a7ea commit 5199d9f

File tree

7 files changed

+41
-77
lines changed

7 files changed

+41
-77
lines changed

challenges/advanced-forward/solution.py

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ def copy(self) -> "MyClass":
88
return copied_object
99

1010

11+
# Alternative soltion:
12+
#
13+
# from __future__ import annotations
14+
# class MyClass:
15+
# def __init__(self, x: int) -> None:
16+
# self.x = x
17+
#
18+
# # TODO: Fix the type hints of `copy` to make it type check
19+
# def copy(self) -> MyClass:
20+
# copied_object = MyClass(x=self.x)
21+
# return copied_object
22+
23+
1124
## End of your code ##
1225

1326
from typing import assert_type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check out [TypeVarTuple](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple).

challenges/advanced-variadic-generics/solution.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types.
55
"""
66

7-
from typing import Generic, TypeVar, TypeVarTuple, assert_type
8-
9-
T = TypeVar("T")
10-
Ts = TypeVarTuple("Ts")
11-
12-
13-
class Array(Generic[*Ts]):
7+
# For Python < 3.12
8+
# Ts = TypeVarTuple("Ts")
9+
#
10+
# class Array(Generic[*Ts]):
11+
# def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]":
12+
# ...
13+
14+
# For Python >= 3.12
15+
class Array[*Ts]:
1416
def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]":
1517
...
1618

challenges/advanced-variadic-generics/solution2.py

-23
This file was deleted.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Checkout [ParamSpec](https://docs.python.org/3/library/typing.html#typing.ParamSpec) and [Concatenate](https://docs.python.org/3/library/typing.html#typing.Concatenate).

challenges/extreme-self-casting/solution.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@
1212

1313
from typing import Callable, Concatenate, Generic, ParamSpec, TypeVar
1414

15-
16-
R = TypeVar("R")
17-
P = ParamSpec("P")
18-
19-
20-
class Fn(Generic[R, P]):
21-
def __init__(self, f: Callable[P, R]):
15+
# # For Python < 3.12
16+
# R = TypeVar("R")
17+
# P = ParamSpec("P")
18+
#
19+
#
20+
# class Fn(Generic[R, P]):
21+
# def __init__(self, f: Callable[P, R]):
22+
# self.f = f
23+
#
24+
# def transform_callable(self) -> Callable[Concatenate[object, P], R]:
25+
# ...
26+
27+
28+
# For Python >= 3.12
29+
class Fn[R, **P]:
30+
def __init__(self, f: Callable[P, R]) -> None:
2231
self.f = f
2332

24-
def transform_callable(self) -> Callable[Concatenate[object, P], R]:
33+
def transform_callable(self) -> Callable[Concatenate[Any, P], R]:
2534
...
2635

2736

challenges/extreme-self-casting/solution2.py

-39
This file was deleted.

0 commit comments

Comments
 (0)