Skip to content
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

Improve challenges with alternative solutions and hints #125

Merged
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
13 changes: 13 additions & 0 deletions challenges/advanced-forward/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def copy(self) -> "MyClass":
return copied_object


# Alternative soltion:
#
# from __future__ import annotations
# class MyClass:
# def __init__(self, x: int) -> None:
# self.x = x
#
# # TODO: Fix the type hints of `copy` to make it type check
# def copy(self) -> MyClass:
# copied_object = MyClass(x=self.x)
# return copied_object


## End of your code ##

from typing import assert_type
Expand Down
1 change: 1 addition & 0 deletions challenges/advanced-variadic-generics/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check out [TypeVarTuple](https://docs.python.org/3/library/typing.html#typing.TypeVarTuple).
16 changes: 9 additions & 7 deletions challenges/advanced-variadic-generics/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types.
"""

from typing import Generic, TypeVar, TypeVarTuple, assert_type

T = TypeVar("T")
Ts = TypeVarTuple("Ts")


class Array(Generic[*Ts]):
# For Python < 3.12
# Ts = TypeVarTuple("Ts")
#
# class Array(Generic[*Ts]):
# def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]":
# ...

# For Python >= 3.12
class Array[*Ts]:
def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]":
...

Expand Down
23 changes: 0 additions & 23 deletions challenges/advanced-variadic-generics/solution2.py

This file was deleted.

1 change: 1 addition & 0 deletions challenges/extreme-self-casting/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +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).
25 changes: 17 additions & 8 deletions challenges/extreme-self-casting/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@

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


R = TypeVar("R")
P = ParamSpec("P")


class Fn(Generic[R, P]):
def __init__(self, f: Callable[P, R]):
# # For Python < 3.12
# R = TypeVar("R")
# P = ParamSpec("P")
#
#
# class Fn(Generic[R, P]):
# def __init__(self, f: Callable[P, R]):
# self.f = f
#
# def transform_callable(self) -> Callable[Concatenate[object, P], R]:
# ...


# For Python >= 3.12
class Fn[R, **P]:
def __init__(self, f: Callable[P, R]) -> None:
self.f = f

def transform_callable(self) -> Callable[Concatenate[object, P], R]:
def transform_callable(self) -> Callable[Concatenate[Any, P], R]:
...


Expand Down
39 changes: 0 additions & 39 deletions challenges/extreme-self-casting/solution2.py

This file was deleted.

Loading