Skip to content

Add compute_z_array Function with Unit Tests #930

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Simranstha045
Copy link

@Simranstha045 Simranstha045 commented Oct 18, 2024

PR Description:

This PR introduces the implementation of the compute_z_array function, which computes the Z-array for a given string in linear time, as well as comprehensive unit tests to verify its correctness.

Description of compute_z_array Function:

The compute_z_array function calculates the Z-array for a string, where each element at index i represents the length of the longest substring starting from index i that is also a prefix of the string.

Key Features:

  • Utilizes the Z-box technique to reduce unnecessary comparisons.
  • Processes the string in linear time.

Use case:
The Z Algorithm is like a super-smart search tool that looks at parts of a string and tells you how much they match the beginning of the string. It's useful when you want to find patterns quickly, like searching for words in a long text, detecting DNA sequences, or even finding virus patterns in files. It does all this in a smart way, skipping unnecessary checks, so it's super fast!

Unit Tests for compute_z_array:

The following tests have been added to ensure robustness:

  • Basic Test Cases: Verifies correct Z-array generation for strings with repeating patterns (e.g., "aaaaa" and "ababa").
  • Edge Case Tests:
    • Strings with no prefix matches (e.g., "abcde").
    • Full match strings (e.g., "aaaa").
    • Mixed case strings with partial matches.
    • Single character strings.
    • Empty string input.

These unit tests ensure the function works as expected across a range of inputs, providing confidence in its correctness and edge-case handling.

All tests have been added under the unittest framework to maintain consistency with the existing test suite.

@Simranstha045 Simranstha045 marked this pull request as ready for review October 18, 2024 12:23
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant