Skip to content

Commit

Permalink
Add more walrus tests
Browse files Browse the repository at this point in the history
     for use in generators
  • Loading branch information
brocla authored and BethanyG committed Mar 8, 2024
1 parent 1b13fca commit 157962c
Show file tree
Hide file tree
Showing 4 changed files with 954 additions and 377 deletions.
26 changes: 23 additions & 3 deletions test/example-walrus-normalization/example_walrus_normalization.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# 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
]
sub_str
for i, _ in enumerate(series)
if len(sub_str := series[i : i + length]) == length
]


def check_height(grid):
Expand All @@ -27,3 +29,21 @@ def nswe_points(self, point):
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}")
)
13 changes: 12 additions & 1 deletion test/example-walrus-normalization/mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@
"placeholder_10": "point",
"placeholder_11": "nswe_offsets",
"placeholder_12": "offset",
"placeholder_13": "neighbor"
"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"
}
Loading

0 comments on commit 157962c

Please sign in to comment.