Skip to content

Commit

Permalink
Add test for walrus operator
Browse files Browse the repository at this point in the history
  • Loading branch information
brocla committed Mar 7, 2024
1 parent 11a3a38 commit ea5bc8a
Show file tree
Hide file tree
Showing 6 changed files with 1,175 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/example-walrus-normalization/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"authors": [
"brocla"
],
"files": {
"solution": [
"example_walrus_normalization.py"
]
}
}
29 changes: 29 additions & 0 deletions test/example-walrus-normalization/example_walrus_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Examples of walrus usage in user solutions

def slices(series, length):
"""
Given a string of digits, output all the contiguous substrings of length `n`,
in that string, in the order that they appear.
"""
return [
sub_str for i, _ in enumerate(series)
if len(sub_str := series[i:i+length]) == length
]


def check_height(grid):
"""check that row count is a multiple of 4"""
if (height := len(grid)) % 3:
raise ValueError("grid rows not a multiple of 4")
return height


def nswe_points(self, point):
"""return a set of four adjacent points"""
nswe_offsets = set([(1, 0), (-1, 0), (0, -1), (0, 1)])
return {
neighbor
for offset in nswe_offsets
if self.on_the_board(neighbor := point + offset)
}

16 changes: 16 additions & 0 deletions test/example-walrus-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"placeholder_0": "slices",
"placeholder_1": "series",
"placeholder_2": "length",
"placeholder_3": "i",
"placeholder_4": "_",
"placeholder_5": "sub_str",
"placeholder_6": "check_height",
"placeholder_7": "grid",
"placeholder_8": "height",
"placeholder_9": "nswe_points",
"placeholder_10": "point",
"placeholder_11": "nswe_offsets",
"placeholder_12": "offset",
"placeholder_13": "neighbor"
}
3 changes: 3 additions & 0 deletions test/example-walrus-normalization/representation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": 2
}
Loading

0 comments on commit ea5bc8a

Please sign in to comment.