Skip to content

Added pairwise iteration algorithms - #9645

Open
matheusfvesco wants to merge 6 commits into
TheAlgorithms:masterfrom
matheusfvesco:master
Open

Added pairwise iteration algorithms#9645
matheusfvesco wants to merge 6 commits into
TheAlgorithms:masterfrom
matheusfvesco:master

Conversation

@matheusfvesco

@matheusfvesco matheusfvesco commented Oct 3, 2023

Copy link
Copy Markdown

Could you please add the hacktoberfest label to this PR, if possible, please?

Describe your change:

Added a pairwise iteration algorithm. It will iterate over any iterable, yielding tuples that represent (iterable[i],iterable[i+1]). Second implementation allows specifying a step size. This algorithm can be useful for various domains, like:

  • Iterating over frames of a video to track objects;
  • Comparing the current word with the next to try to predict the next word;
  • Comparing codons of a genetic sequence.

Couldn't find any wikipedia articles for this algorithm.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 3, 2023
@matheusfvesco

Copy link
Copy Markdown
Author

Need reviews, please!

@matheusfvesco

Copy link
Copy Markdown
Author

@chriso345 need reviews, please!

@cclauss

cclauss commented Sep 11, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev, please review.

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Thanks for the PR, @matheusfvesco, and for your patience — I took a careful pass. 🙂

What works: the code is clean and the docstrings are genuinely nice. I checked out the branch and ran the doctests: 16 tests in 3 items, 16 passed / 0 failed on Python 3.12, and I spot-checked the dict/set/string cases by hand — all correct. The DIRECTORY.md diff (incl. the physics/hamiltonian.py line) is just the auto-generated sync, so no concern there.

A couple of things worth discussing before merge:

  1. pairwise_iteration_tee is exactly itertools.pairwise (stdlib since 3.10, which this repo targets) — same tee + offset recipe. We usually avoid re-implementing the stdlib. Two good options: (a) drop this function, or (b) keep it but reframe the docstring as "educational: here's how itertools.pairwise works under the hood" and cross-reference it, so a reader knows to prefer the builtin in real code.

  2. pairwise_iteration_comprehension's step is the part that adds real value — the stdlib has no strided variant, so this is the function worth keeping. One note: items = [item for i, item in enumerate(iterable) if i % step == 0] materializes the whole input into a list, so the "iterator" laziness is lost for large inputs. That's fine for the educational scope, but a one-line comment saying so (or naming it strided_pairwise) would set expectations.

  3. Nit: both docstrings say "Yields", but both return zip(...) rather than yield — worth changing to "Returns" for accuracy.

None of these are hard blockers; if you fold #1 and #2 in, I think this is in good shape. Nice work! 👍

@cclauss

cclauss commented Sep 11, 2026

Copy link
Copy Markdown
Member

Nice review! I love yielding (just-in-time -- stream-flow delivery of) data to the caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants