Skip to main content

Computing the longest common subsequence of a pair of sequences and their diff

Project description

lcs2

lcs2 is a Python package which helps find the longest common subsequence of a pair of sequences and compute their diff.

Installation

pip install lcs2

Reference

The package provides the following functions:

Function Signature Result
lcs Iterable[T], Iterable[T] -> list[T] Longest common subsequence (LCS)
lcs_indices Iterable[T], Iterable[T] -> list[tuple[int, int]] Indices of the LCS
lcs_length Iterable[T], Iterable[T] -> int Length of the LCS
diff Iterable[T], Iterable[T] -> list[tuple[list[T], list[T]]] Differing segments of the sequences based on the LCS
diff_ranges Iterable[T], Iterable[T] -> list[tuple[range, range]] Ranges of indices of the differing segments

Calling lcs_length(a, b) is somewhat more efficient than len(lcs(a, b)).

Sample Usage

from lcs2 import diff, diff_ranges, lcs, lcs_indices, lcs_length

a = 'Hello, world!'
b = 'Foobar'

print(lcs(a, b))  # ['o', 'o', 'r']
print(lcs_indices(a, b))  # [(4, 1), (8, 2), (9, 5)]
print(lcs_length(a, b))  # 3

print(diff(a, b))  # [(['H', 'e', 'l', 'l'], ['F']),
                   # ([',', ' ', 'w'], []),
                   # ([], ['b', 'a']),
                   # (['l', 'd', '!'], [])]
print(diff_ranges(a, b))  # [(range(0, 4), range(0, 1)),
                          # (range(5, 8), range(2, 2)),
                          # (range(9, 9), range(3, 5)),
                          # (range(10, 13), range(6, 6))]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lcs2-1.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

lcs2-1.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file lcs2-1.1.0.tar.gz.

File metadata

  • Download URL: lcs2-1.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for lcs2-1.1.0.tar.gz
Algorithm Hash digest
SHA256 178ee8b33e0b8ab35d94d21f811c7fdbb36020f8dd5a9a59be6b47588581e51b
MD5 1a50fa99d4706b4eab9f45f4390bf8fa
BLAKE2b-256 165d3bc1b14c1071f1df91c1b9b04d5caeb578e245e94237802f6685a2f2b5b8

See more details on using hashes here.

File details

Details for the file lcs2-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: lcs2-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for lcs2-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 025d76600b9180383bbcb2da2fd8c97abf6c1b6d739e8c03ef924594a126966c
MD5 e01760724620af4803e25e67e1afb807
BLAKE2b-256 de557dcd8c5e4f77d391663028115f09e226fd9a2c9b5d9c20749937941faec6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page