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

Add test for walrus operator #51

Merged
merged 3 commits into from
Mar 8, 2024
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
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"
]
}
}
49 changes: 49 additions & 0 deletions test/example-walrus-normalization/example_walrus_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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)
}


def first_item_greater_than_N(iterable, N):
if any((item := x) > N for x in iterable):
return item
return None


def generate_codes(seat_numbers, flight_id):
"""Generate codes for a ticket.

:param seat_numbers: list[str] - list of seat numbers.
:param flight_id: str - string containing the flight identifier.
:return: generator - generator that yields 12 character long ticket codes.

"""
return (
base.ljust(12, "0") for seat in seat_numbers if (base := f"{seat}{flight_id}")
)
27 changes: 27 additions & 0 deletions test/example-walrus-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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",
"placeholder_14": "first_item_greater_than_N",
"placeholder_15": "iterable",
"placeholder_16": "N",
"placeholder_17": "item",
"placeholder_18": "x",
"placeholder_19": "placeholder_17",
"placeholder_20": "generate_codes",
"placeholder_21": "seat_numbers",
"placeholder_22": "flight_id",
"placeholder_23": "seat",
"placeholder_24": "base"
}
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
Loading