Skip to content

Commit 695dbfa

Browse files
committed
Add a challenge: extreme-concatenate
1 parent fd02922 commit 695dbfa

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
TODO:
3+
4+
Suppose there's a function `foo`, whose first parameter can be anything.
5+
6+
You want to use `foo`, but want to restrict the first argument to be a `Person`.
7+
You cannot modify `foo`, so you decide to write a function `transform`,
8+
to transform `foo` into the function you want.
9+
"""
10+
11+
12+
class Person:
13+
pass
14+
15+
16+
def transform(f):
17+
def wrapper(value: Person, *args, **kwargs):
18+
return f(value, *args, **kwargs)
19+
20+
return wrapper
21+
22+
23+
## End of your code ##
24+
def foo(value, mode: str) -> None:
25+
pass
26+
27+
28+
foo_with_person = transform(foo)
29+
foo_with_person(Person(), "1")
30+
foo_with_person(Person(), mode="1")
31+
foo_with_person(1, "1") # expect-type-error
32+
foo_with_person(Person(), 1) # expect-type-error
33+
foo_with_person(Person(), "1", 2) # expect-type-error
34+
35+
36+
def foo2(value, mode: str, *, maybe: bool) -> None:
37+
pass
38+
39+
40+
foo_with_person = transform(foo2)
41+
foo_with_person(Person(), "1", maybe=True)
42+
foo_with_person(value=Person(), mode="1", maybe=True)
43+
foo_with_person(Person(), mode="1", maybe=True)
44+
foo_with_person(Person(), mode="1") # expect-type-error
45+
foo_with_person(Person(), 1, maybe=True) # expect-type-error
46+
foo_with_person(Person(), "1", maybe=1) # expect-type-error
47+
foo_with_person(Person(), "1", True) # expect-type-error
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
TODO:
3+
4+
Suppose there's a function `foo`, whose first parameter can be anything.
5+
6+
You want to use `foo`, but want to restrict the first argument to be a `Person`.
7+
You cannot modify `foo`, so you decide to write a function `transform`,
8+
to transform `foo` into the function you want.
9+
"""
10+
11+
from typing import Concatenate, Callable, Any
12+
13+
14+
class Person:
15+
pass
16+
17+
18+
def transform[T, **P](f: Callable[Concatenate[Any, P], T]):
19+
def wrapper(value: Person, *args: P.args, **kwargs: P.kwargs) -> T:
20+
return f(value, *args, **kwargs)
21+
22+
return wrapper
23+
24+
25+
## End of your code ##
26+
def foo(value, mode: str) -> None:
27+
pass
28+
29+
30+
foo_with_person = transform(foo)
31+
foo_with_person(Person(), "1")
32+
foo_with_person(Person(), mode="1")
33+
foo_with_person(1, "1") # expect-type-error
34+
foo_with_person(Person(), 1) # expect-type-error
35+
foo_with_person(Person(), "1", 2) # expect-type-error
36+
37+
38+
def foo2(value, mode: str, *, maybe: bool) -> int:
39+
return 1
40+
41+
42+
foo_with_person = transform(foo2)
43+
foo_with_person(Person(), "1", maybe=True)
44+
foo_with_person(value=Person(), mode="1", maybe=True)
45+
foo_with_person(Person(), mode="1", maybe=True)
46+
foo_with_person(Person(), mode="1") # expect-type-error
47+
foo_with_person(Person(), 1, maybe=True) # expect-type-error
48+
foo_with_person(Person(), "1", maybe=1) # expect-type-error
49+
foo_with_person(Person(), "1", True) # expect-type-error

templates/challenge.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
<p class="challenge-header__exerpt">
240240
Complete code following the instructions, so that lines followed by
241241
<code># expect-type-error</code> (if any)
242-
should fail type check, others should pass.<br>
242+
fail type check, while others can pass.<br>
243243
Hit the "▶️ Run" button to see result.
244244
</p>
245245
</div>

tests/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from views.challenge import ChallengeKey, Level, challenge_manager
66

7+
78
def test_get_challenge(test_client: FlaskClient, question_file: Path):
89
level, challenge_name = question_file.parent.name.split("-", maxsplit=1)
910
response = test_client.get(f"/{level}/{challenge_name}")

0 commit comments

Comments
 (0)