Skip to content

Add LZW compression module - #2767

Open
dakshp26 wants to merge 3 commits into
keon:mainfrom
dakshp26:add_lzw_compression_with_test
Open

Add LZW compression module#2767
dakshp26 wants to merge 3 commits into
keon:mainfrom
dakshp26:add_lzw_compression_with_test

Conversation

@dakshp26

Copy link
Copy Markdown

Summary

Adds a new LZW (Lempel-Ziv-Welch) compression implementation to the algorithms/compression package.

Changes

  • New file: algorithms/compression/lzw_compression.py
  • lzw_encode(data) — encodes a string using LZW and returns (codes, initial_dictionary) so the encoded output is self-contained for decoding.
  • lzw_decode(encoded, initial_dictionary) — decodes the integer codes back to the original string by rebuilding the dictionary on the fly.
  • Updated: algorithms/compression/init.py
  • Exports lzw_encode and lzw_decode.
  • Updated: tests/test_compression.py
  • Added TestLZWCompression with tests for encoding, decoding, round-trip, and empty input.

Conventions

Follows existing project conventions:

  • Module docstring with description, complexity, and reference link.
  • from future import annotations.
  • Type hints and function docstrings with examples.
  • doctest-compatible examples.

Testing

All 4 LZW tests pass:

  • test_lzw_encode
  • test_lzw_decode
  • test_lzw_roundtrip
  • test_lzw_empty

dakshp26 added 2 commits July 28, 2026 14:57
Implement Lempel-Ziv-Welch (LZW) encoding and decoding under
algorithms/compression, following the style of existing modules
(huffman_coding, rle_compression, elias).

- lzw_encode(data) returns a tuple of (codes, initial_dictionary),
  making the API self-contained since the decoder needs the same
  character-to-code mapping.
- lzw_decode(codes, initial_dictionary) reconstructs the dictionary
  on the fly and recovers the original string.
- Handles edge cases including empty input.
- Adds unit tests covering encoding, decoding, round-trip, and empty
  input.
- Exports lzw_encode and lzw_decode from compression/__init__.py.
@keon

keon commented Jul 29, 2026

Copy link
Copy Markdown
Owner

thanks for the contribution!

@dakshp26

Copy link
Copy Markdown
Author

I have fixed the lint error that ruff check found (dictionary line too long). Please let me know if I should make any other modifications. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants