diff --git a/itertools-in-python3/README.md b/itertools-in-python3/README.md index 9e4beb954a..2ecc8c8b07 100644 --- a/itertools-in-python3/README.md +++ b/itertools-in-python3/README.md @@ -28,7 +28,7 @@ Alternatively, you can download the entire repository as a `.zip` file from the ## Running the Code Examples -To run the code examples, first make sure you have Python 3 installed on your machine. **The code examples WILL NOT work with Python 2.** If you need help installing Python 3, check out Real Python's [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/). +To run the code examples, first make sure you have Python 3.12 or later installed on your machine, since `swimmers.py` uses `itertools.batched()`. If you need help installing Python 3, check out Real Python's [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/). To run the `sp500.py` file, type `python3 sp500.py` into your terminal. Here's how that looks, with output: @@ -39,7 +39,7 @@ Max loss: -20.47% on 1987-10-19 Longest growth streak: 14 days (1971-03-26 to 1971-04-15) ``` -> **Note:** Depending on your installation, you may need to type `python3.6` or `python36` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem. +> **Note:** Depending on your installation, you may need to type `python3.15` or `python315` to run the examples. If you follow the [Python 3 Installation & Setup Guide](https://realpython.com/installing-python/), you should have no problem. Similarly, for `swimmers.py`: diff --git a/itertools-in-python3/sp500.py b/itertools-in-python3/sp500.py index 0a026ebd30..ef68080e06 100644 --- a/itertools-in-python3/sp500.py +++ b/itertools-in-python3/sp500.py @@ -44,12 +44,12 @@ def read_prices(csvfile, _strptime=datetime.strptime): prices = tuple(read_prices("SP500.csv")) gains = tuple( DataPoint(day.date, 100 * (day.value / prev_day.value - 1.0)) - for day, prev_day in zip(prices[1:], prices, strict=False) + for prev_day, day in it.pairwise(prices) ) # Find maximum daily gain/loss. zdp = DataPoint(None, 0) # zero DataPoint -max_gain = ft.reduce(max, it.filterfalse(lambda p: p <= zdp, gains)) +max_gain = ft.reduce(max, it.filterfalse(lambda p: p <= zdp, gains), zdp) max_loss = ft.reduce(min, it.filterfalse(lambda p: p > zdp, gains), zdp) diff --git a/itertools-in-python3/swimmers.py b/itertools-in-python3/swimmers.py index 6f7f8e0990..814ac85d98 100644 --- a/itertools-in-python3/swimmers.py +++ b/itertools-in-python3/swimmers.py @@ -16,15 +16,10 @@ def sort_and_group(iterable, key=None): return it.groupby(sorted(iterable, key=key), key=key) -def grouper(iterable, n, fillvalue=None): - iters = [iter(iterable)] * n - return it.zip_longest(*iters, fillvalue=fillvalue) - - def read_events(csvfile, _strptime=datetime.datetime.strptime): def _median(times): return statistics.median( - (_strptime(time, "%M:%S:%f").time() for time in row["Times"]) + (_strptime(time, "%M:%S:%f").time() for time in times) ) fieldnames = ["Event", "Name", "Stroke"] @@ -41,9 +36,7 @@ def _median(times): events_by_name = sort_and_group(evts, key=lambda evt: evt.name) best_times = (min(evt) for _, evt in events_by_name) sorted_by_time = sorted(best_times, key=lambda evt: evt.time) - teams = zip( - ("A", "B"), it.islice(grouper(sorted_by_time, 4), 2), strict=False - ) + teams = zip(("A", "B"), it.batched(sorted_by_time, 4), strict=False) for team, swimmers in teams: print( "{stroke} {team}: {names}".format(