Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add Edit Distance Algorithm to Dynamic Programming #82

Closed
wants to merge 1 commit into from

Conversation

staging-devin-ai-integration[bot]
Copy link

Add Edit Distance Algorithm to Dynamic Programming

This pull request adds the implementation of the Edit Distance algorithm to the Dynamic Programming (DP) section of the algorithms repository.

Description

The Edit Distance algorithm calculates the minimum number of operations (insertion, deletion, substitution) required to transform one string into another. This is a classic dynamic programming problem with applications in spell checking, DNA sequence alignment, and natural language processing.

Implementation Details

  • File: dp/edit_distance.py
  • Time Complexity: O(m*n), where m and n are the lengths of the input strings
  • Space Complexity: O(m*n)

The algorithm uses a 2D DP table to store intermediate results, with each cell representing the minimum edit distance for substrings of the input strings.

Test Cases

A comprehensive set of unit tests has been added in dp/test_edit_distance.py, covering:

  1. Basic cases
  2. Edge cases (empty strings, identical strings)
  3. Single character operations (insertion, deletion, substitution)
  4. Long strings

All tests pass successfully, ensuring the correctness of the implementation.

Integration with Existing Codebase

This new algorithm complements the existing DP algorithms in the repository, providing a solution to a fundamental string manipulation problem. It follows the coding style and structure of other algorithms in the project.

Usage Example

from dp.edit_distance import edit_distance

str1 = "kitten"
str2 = "sitting"
distance = edit_distance(str1, str2)
print(f"Edit distance between '{str1}' and '{str2}': {distance}")
# Output: Edit distance between 'kitten' and 'sitting': 3

This PR is ready for review and integration into the main branch.

This Devin run was requested by Federico.


# Example usage
if __name__ == "__main__":
str1 = "kitten"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change this to dog?

Copy link
Author

Closing due to inactivity.

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.

1 participant