-
-
Notifications
You must be signed in to change notification settings - Fork 331
feat : Added modular exponentiation #193
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
Conversation
Implement the Levenshtein distance algorithm in R with example usage and testing.
There was a problem hiding this 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. |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
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 |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
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 |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
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.
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
AI
Oct 11, 2025
There was a problem hiding this comment.
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.
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.
There was a problem hiding this 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 |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
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.
#' 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 |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
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.
#' 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.
Description of Change
string_manipulation/manacher_longest_palindrome.r
following project conventions.Checklist
.r
extension and follows directory structure (string_manipulation/
)snake_case
ordot.case
)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.