From 06897eb7a65eefac58b17ffb9ecf53daa5109758 Mon Sep 17 00:00:00 2001 From: Chanjian Zhu Date: Sat, 16 Dec 2023 17:31:34 +0800 Subject: [PATCH 1/4] Add challenge for variadic generics --- .../advanced-variadic-generics/question.py | 32 +++++++++++++++++++ .../advanced-variadic-generics/solution.py | 31 ++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 challenges/advanced-variadic-generics/question.py create mode 100644 challenges/advanced-variadic-generics/solution.py diff --git a/challenges/advanced-variadic-generics/question.py b/challenges/advanced-variadic-generics/question.py new file mode 100644 index 0000000..05f12fb --- /dev/null +++ b/challenges/advanced-variadic-generics/question.py @@ -0,0 +1,32 @@ +""" +TODO: + +Define a `Array` type that supports addition of arrays with the same shape. +""" + +from typing import Generic, Tuple, TypeAlias, TypeVar, TypeVarTuple + +T = TypeVar("T") +Ts = TypeVarTuple("Ts") + +S1: TypeAlias = Tuple[float, int] +S2: TypeAlias = Tuple[*S1, str] + + +class Array(Generic[*Ts]): + def __add__(self, other): + ... + + def add_dimension(self, x): + ... + + +a: Array[*S1] = Array() +b: Array[*S1] = Array() +print(a + b) + +c: Array[*S2] = Array() +print(a + c) # expect-type-error + +d = a.add_dimension("foo") +print(c + d) diff --git a/challenges/advanced-variadic-generics/solution.py b/challenges/advanced-variadic-generics/solution.py new file mode 100644 index 0000000..4703d40 --- /dev/null +++ b/challenges/advanced-variadic-generics/solution.py @@ -0,0 +1,31 @@ +""" +TODO: + +Define a `Array` type that supports addition of arrays with the same shape. +""" + +from typing import Generic, Tuple, TypeAlias, TypeVar, TypeVarTuple + +T = TypeVar("T") +Ts = TypeVarTuple("Ts") + +S1: TypeAlias = Tuple[float, int] +S2: TypeAlias = Tuple[*S1, str] + + +class Array(Generic[*Ts]): + def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]": + ... + + def add_dimension(self, x: "T") -> "Array[*Ts, T]": + ... + + +a: Array[*S1] = Array() +b: Array[*S1] = Array() +print(a + b) + +c: Array[*S2] = Array() +print(a + c) # expect-type-error +d = a.add_dimension("foo") +print(c + d) From 0ba35aba2303a389be189df6f73e00453b7b74a5 Mon Sep 17 00:00:00 2001 From: Chanjian Zhu Date: Sun, 17 Dec 2023 14:48:21 +0800 Subject: [PATCH 2/4] Remove variadic generics code from question code --- .../advanced-variadic-generics/question.py | 17 +++++------------ .../advanced-variadic-generics/solution.py | 13 ++++++------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/challenges/advanced-variadic-generics/question.py b/challenges/advanced-variadic-generics/question.py index 05f12fb..027073c 100644 --- a/challenges/advanced-variadic-generics/question.py +++ b/challenges/advanced-variadic-generics/question.py @@ -4,16 +4,8 @@ Define a `Array` type that supports addition of arrays with the same shape. """ -from typing import Generic, Tuple, TypeAlias, TypeVar, TypeVarTuple -T = TypeVar("T") -Ts = TypeVarTuple("Ts") - -S1: TypeAlias = Tuple[float, int] -S2: TypeAlias = Tuple[*S1, str] - - -class Array(Generic[*Ts]): +class Array: def __add__(self, other): ... @@ -21,11 +13,12 @@ def add_dimension(self, x): ... -a: Array[*S1] = Array() -b: Array[*S1] = Array() +## End of your code ## +a: Array[float, int] = Array() +b: Array[float, int] = Array() print(a + b) -c: Array[*S2] = Array() +c: Array[float, int, str] = Array() print(a + c) # expect-type-error d = a.add_dimension("foo") diff --git a/challenges/advanced-variadic-generics/solution.py b/challenges/advanced-variadic-generics/solution.py index 4703d40..2e64429 100644 --- a/challenges/advanced-variadic-generics/solution.py +++ b/challenges/advanced-variadic-generics/solution.py @@ -4,14 +4,11 @@ Define a `Array` type that supports addition of arrays with the same shape. """ -from typing import Generic, Tuple, TypeAlias, TypeVar, TypeVarTuple +from typing import Generic, TypeVar, TypeVarTuple T = TypeVar("T") Ts = TypeVarTuple("Ts") -S1: TypeAlias = Tuple[float, int] -S2: TypeAlias = Tuple[*S1, str] - class Array(Generic[*Ts]): def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]": @@ -21,11 +18,13 @@ def add_dimension(self, x: "T") -> "Array[*Ts, T]": ... -a: Array[*S1] = Array() -b: Array[*S1] = Array() +## End of your code ## +a: Array[float, int] = Array() +b: Array[float, int] = Array() print(a + b) -c: Array[*S2] = Array() +c: Array[float, int, str] = Array() print(a + c) # expect-type-error + d = a.add_dimension("foo") print(c + d) From 7201fd899047963e997f63b22cfbee4a98bc5b0f Mon Sep 17 00:00:00 2001 From: Chanjian Zhu Date: Sat, 23 Dec 2023 07:48:22 +0800 Subject: [PATCH 3/4] Remove add_dimension method --- .../advanced-variadic-generics/question.py | 14 +++++--------- .../advanced-variadic-generics/solution.py | 16 ++++++---------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/challenges/advanced-variadic-generics/question.py b/challenges/advanced-variadic-generics/question.py index 027073c..4d60921 100644 --- a/challenges/advanced-variadic-generics/question.py +++ b/challenges/advanced-variadic-generics/question.py @@ -1,7 +1,7 @@ """ TODO: -Define a `Array` type that supports addition of arrays with the same shape. +Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types. """ @@ -9,17 +9,13 @@ class Array: def __add__(self, other): ... - def add_dimension(self, x): - ... - ## End of your code ## +from typing import assert_type + a: Array[float, int] = Array() b: Array[float, int] = Array() -print(a + b) +assert_type(a + b, Array[float, int]) c: Array[float, int, str] = Array() -print(a + c) # expect-type-error - -d = a.add_dimension("foo") -print(c + d) +assert_type(a + c, Array[float, int, str]) # expect-type-error diff --git a/challenges/advanced-variadic-generics/solution.py b/challenges/advanced-variadic-generics/solution.py index 2e64429..b2fbca5 100644 --- a/challenges/advanced-variadic-generics/solution.py +++ b/challenges/advanced-variadic-generics/solution.py @@ -1,10 +1,10 @@ """ TODO: -Define a `Array` type that supports addition of arrays with the same shape. +Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types. """ -from typing import Generic, TypeVar, TypeVarTuple +from typing import Generic, TypeVar, TypeVarTuple, assert_type T = TypeVar("T") Ts = TypeVarTuple("Ts") @@ -14,17 +14,13 @@ class Array(Generic[*Ts]): def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]": ... - def add_dimension(self, x: "T") -> "Array[*Ts, T]": - ... - ## End of your code ## +from typing import assert_type + a: Array[float, int] = Array() b: Array[float, int] = Array() -print(a + b) +assert_type(a + b, Array[float, int]) c: Array[float, int, str] = Array() -print(a + c) # expect-type-error - -d = a.add_dimension("foo") -print(c + d) +assert_type(a + c, Array[float, int, str]) # expect-type-error From 0ef2761f00ad6938f6e1f56b734e3ce129db111e Mon Sep 17 00:00:00 2001 From: Chanjian Zhu Date: Sat, 23 Dec 2023 08:08:23 +0800 Subject: [PATCH 4/4] Add new generic syntax solution for variadic generics --- .../advanced-variadic-generics/solution2.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenges/advanced-variadic-generics/solution2.py diff --git a/challenges/advanced-variadic-generics/solution2.py b/challenges/advanced-variadic-generics/solution2.py new file mode 100644 index 0000000..2d08ea4 --- /dev/null +++ b/challenges/advanced-variadic-generics/solution2.py @@ -0,0 +1,23 @@ +""" +TODO: + +Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types. +""" + +from typing import assert_type + + +class Array[*Ts]: + def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]": + ... + + +## End of your code ## +from typing import assert_type + +a: Array[float, int] = Array() +b: Array[float, int] = Array() +assert_type(a + b, Array[float, int]) + +c: Array[float, int, str] = Array() +assert_type(a + c, Array[float, int, str]) # expect-type-error