Skip to content

Conversation

shimmer12
Copy link
Contributor

@shimmer12 shimmer12 commented Oct 11, 2025

Description of Change

  • Implemented Manacher’s Algorithm in R to find the longest palindromic substring.
  • Achieves O(n) time complexity (vs. naive O(n^3)), with linear-time expansion via center radii.
  • Handles both odd and even length palindromes using a transformed string representation.
  • Returns the actual substring, not just its length.
  • Case-insensitive option supported for flexible matching.
  • Clear variable names and inline comments for readability.
  • Added example usage and simple self-tests.
  • Stored under string_manipulation/manacher_longest_palindrome.r following project conventions.

Checklist

  • Verified that this algorithm is not already implemented elsewhere in the repository (including under a different name)
  • Confirmed this is a recognized CS algorithm (not a problem-specific variant)
  • The file uses a lowercase .r extension and follows directory structure (string_manipulation/)
  • DIRECTORY.md updated with the new entry
  • Code includes appropriate documentation (roxygen-style header) and examples
  • Variable naming follows repository conventions (e.g., snake_case or dot.case)
  • Code runs without errors/warnings on test cases; included a simple self-test (e.g., stopifnot(...))

Notes: Adds an educational, linear-time longest-palindrome implementation in R with examples and inline commentary; improves the string_manipulation coverage with a core string algorithm.

@Copilot Copilot AI review requested due to automatic review settings October 11, 2025 19:51
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds two new algorithms to the repository: modular exponentiation using fast binary exponentiation and the Levenshtein distance algorithm for string comparison. However, there appears to be a mismatch between the PR title/description (which mentions Manacher's Algorithm) and the actual code changes.

  • Added modular exponentiation algorithm with O(log n) time complexity
  • Added Levenshtein distance algorithm with O(m*n) time complexity for string edit distance
  • Both implementations include comprehensive documentation and examples

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
mathematics/modular_exponentiation.R Implements fast binary exponentiation with optional modular arithmetic
dynamic_programming/Levenshtein_Distance.R Implements dynamic programming solution for computing edit distance between strings

@@ -0,0 +1,69 @@
#' Computes modular exponentiation using fast binary exponentiation.
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

File extension should be lowercase .r not .R according to repository conventions.

Copilot generated this review using guidance from repository custom instructions.

@@ -0,0 +1,84 @@
# Levenshtein Distance Algorithm
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

File extension should be lowercase .r not .R according to repository conventions.

Copilot generated this review using guidance from repository custom instructions.

#' - Negative exponents are **not supported** (would require modular inverse).
#'
#' @examples
#' modular_exponentiation(2, 10, 1000) # 24
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The example comment shows incorrect result. 2^10 mod 1000 = 1024 mod 1000 = 24, but this should be verified as the comment suggests the result is 24 when it should be 24.

Copilot uses AI. Check for mistakes.

Comment on lines 61 to 84
cat("=== Levenshtein Distance Algorithm ===\n\n")

# Test 1: Classic example
cat("1. Classic Example\n")
str1 <- "saturday"
str2 <- "sunday"
cat("String 1:", str1, "\n")
cat("String 2:", str2, "\n")
cat("Distance:", levenshtein_distance(str1, str2), "\n\n")

# Test 2: Another common example
cat("2. Common Example\n")
str1 <- "kitten"
str2 <- "sitting"
cat("String 1:", str1, "\n")
cat("String 2:", str2, "\n")
cat("Distance:", levenshtein_distance(str1, str2), "\n\n")

# Test 3: Edge cases
cat("3. Edge Cases\n")
cat("Distance between 'apple' and '':", levenshtein_distance("apple", ""), "\n")
cat("Distance between '' and 'banana':", levenshtein_distance("", "banana"), "\n")
cat("Distance between 'book' and 'book':", levenshtein_distance("book", "book"), "\n")
cat("Distance between 'car' and 'bus':", levenshtein_distance("car", "bus"), "\n\n")
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The file contains example usage with cat() statements that will print output when the file is sourced. Consider moving these examples to a separate demo script or wrapping them in a function to avoid unwanted output during file loading.

Suggested change
cat("=== Levenshtein Distance Algorithm ===\n\n")
# Test 1: Classic example
cat("1. Classic Example\n")
str1 <- "saturday"
str2 <- "sunday"
cat("String 1:", str1, "\n")
cat("String 2:", str2, "\n")
cat("Distance:", levenshtein_distance(str1, str2), "\n\n")
# Test 2: Another common example
cat("2. Common Example\n")
str1 <- "kitten"
str2 <- "sitting"
cat("String 1:", str1, "\n")
cat("String 2:", str2, "\n")
cat("Distance:", levenshtein_distance(str1, str2), "\n\n")
# Test 3: Edge cases
cat("3. Edge Cases\n")
cat("Distance between 'apple' and '':", levenshtein_distance("apple", ""), "\n")
cat("Distance between '' and 'banana':", levenshtein_distance("", "banana"), "\n")
cat("Distance between 'book' and 'book':", levenshtein_distance("book", "book"), "\n")
cat("Distance between 'car' and 'bus':", levenshtein_distance("car", "bus"), "\n\n")
demo_levenshtein_distance <- function() {
cat("=== Levenshtein Distance Algorithm ===\n\n")
# Test 1: Classic example
cat("1. Classic Example\n")
str1 <- "saturday"
str2 <- "sunday"
cat("String 1:", str1, "\n")
cat("String 2:", str2, "\n")
cat("Distance:", levenshtein_distance(str1, str2), "\n\n")
# Test 2: Another common example
cat("2. Common Example\n")
str1 <- "kitten"
str2 <- "sitting"
cat("String 1:", str1, "\n")
cat("String 2:", str2, "\n")
cat("Distance:", levenshtein_distance(str1, str2), "\n\n")
# Test 3: Edge cases
cat("3. Edge Cases\n")
cat("Distance between 'apple' and '':", levenshtein_distance("apple", ""), "\n")
cat("Distance between '' and 'banana':", levenshtein_distance("", "banana"), "\n")
cat("Distance between 'book' and 'book':", levenshtein_distance("book", "book"), "\n")
cat("Distance between 'car' and 'bus':", levenshtein_distance("car", "bus"), "\n\n")
}

Copilot uses AI. Check for mistakes.

@Copilot Copilot AI review requested due to automatic review settings October 11, 2025 19:53
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

#' - Negative exponents are **not supported** (would require modular inverse).
#'
#' @examples
#' modular_exponentiation(2, 10, 1000) # 24
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The expected result is incorrect. 2^10 = 1024, so 1024 mod 1000 = 24, but 2^10 mod 1000 should be 24. However, the comment should show the correct calculation: 2^10 = 1024, 1024 mod 1000 = 24.

Suggested change
#' modular_exponentiation(2, 10, 1000) # 24
#' modular_exponentiation(2, 10, 1000) # 2^10 = 1024, 1024 mod 1000 = 24

Copilot uses AI. Check for mistakes.

#' - Negative exponents are **not supported** (would require modular inverse).
#'
#' @examples
#' modular_exponentiation(2, 10, 1000) # 24
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The expected result comment is misleading. 2^10 = 1024, and 1024 mod 1000 = 24, so the result is correct but the documentation should clarify the calculation for better understanding.

Suggested change
#' modular_exponentiation(2, 10, 1000) # 24
#' modular_exponentiation(2, 10, 1000) # 24 because 2^10 = 1024 and 1024 %% 1000 = 24

Copilot uses AI. Check for mistakes.

@shimmer12 shimmer12 closed this by deleting the head repository Oct 11, 2025
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