1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
[case testWalrus1]
from typing import Optional
def foo(x: int) -> Optional[int]:
if x < 0:
return None
return x
def test(x: int) -> str:
if (n := foo(x)) is not None:
return str(x)
else:
return "<fail>"
[file driver.py]
from native import test
assert test(10) == "10"
assert test(-1) == "<fail>"
[case testWalrus2]
from typing import Optional, Tuple, List
class Node:
def __init__(self, val: int, next: Optional['Node']) -> None:
self.val = val
self.next = next
def pairs(nobe: Optional[Node]) -> List[Tuple[int, int]]:
if nobe is None:
return []
l = []
while next := nobe.next:
l.append((nobe.val, next.val))
nobe = next
return l
def make(l: List[int]) -> Optional[Node]:
cur: Optional[Node] = None
for x in reversed(l):
cur = Node(x, cur)
return cur
[file driver.py]
from native import Node, make, pairs
assert pairs(make([1,2,3])) == [(1,2), (2,3)]
assert pairs(make([1])) == []
assert pairs(make([])) == []
|