-
-
Notifications
You must be signed in to change notification settings - Fork 342
feat: Add R program to count unique letters in a string #160
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,47 @@ | ||
| cocktailSort <- function(arr) { | ||
| # Cocktail Sort in R | ||
| cocktail.sort <- function(arr) { | ||
| n <- length(arr) | ||
| swapped <- TRUE | ||
| beg <- 1 | ||
| start <- 1 | ||
| end <- n - 1 | ||
|
|
||
| while (swapped) { | ||
| swapped <- FALSE | ||
|
|
||
| # Forward pass (left to right) | ||
| for (i in seq(beg, end)) { | ||
| # Forward pass | ||
| for (i in seq(start, end)) { | ||
| if (arr[i] > arr[i + 1]) { | ||
| # Swap arr[i] and arr[i + 1] | ||
| temp <- arr[i] | ||
| arr[i] <- arr[i + 1] | ||
| arr[i + 1] <- temp | ||
| swapped <- TRUE | ||
| } | ||
| } | ||
|
|
||
| # If no swaps occurred in the forward pass, the array is sorted | ||
| if (!swapped) { | ||
| break | ||
| } | ||
| if (!swapped) break | ||
|
|
||
| swapped <- FALSE | ||
| end <- end - 1 | ||
|
|
||
| # Backward pass (right to left) | ||
| for (i in seq(end, beg, by = -1)) { | ||
| # Backward pass | ||
| for (i in seq(end, start, by = -1)) { | ||
| if (arr[i] > arr[i + 1]) { | ||
| # Swap arr[i] and arr[i + 1] | ||
| temp <- arr[i] | ||
| arr[i] <- arr[i + 1] | ||
| arr[i + 1] <- temp | ||
| swapped <- TRUE | ||
| } | ||
| } | ||
|
|
||
| beg <- beg + 1 | ||
| start <- start + 1 | ||
| } | ||
|
|
||
| return(arr) | ||
| } | ||
|
|
||
| # Example Usage | ||
| unsorted_array <- c(38, 27, 43, 3, 9, 82, 10) | ||
| cat("Unsorted Array: ", unsorted_array, "\n") | ||
|
|
||
| # Call the Cocktail Sort function to sort the array | ||
| sorted_array <- cocktailSort(unsorted_array) | ||
|
|
||
| cat("Sorted Array: ", sorted_array, "\n") | ||
|
|
||
| # Example: The 'unsorted_array' is sorted using Cocktail Sort | ||
| # Example usage | ||
| unsorted.array <- c(38, 27, 43, 3, 9, 82, 10) | ||
| cat("Unsorted Array:", unsorted.array, "\n") | ||
|
|
||
| sorted.array <- cocktail.sort(unsorted.array) | ||
| cat("Sorted Array: ", sorted.array, "\n") |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||
| # Ask for user input | ||||||
| input.string <- readline(prompt = "Enter a string: ") | ||||||
|
|
||||||
| # Convert to lowercase and remove non-letter characters | ||||||
| clean.string <- tolower(gsub("[^a-z]", "", input.string)) | ||||||
|
|
||||||
| # Split string into individual letters | ||||||
| letters.vec <- strsplit(clean.string, "")[[1]] | ||||||
|
|
||||||
| # Get unique letters | ||||||
| unique.letters <- unique(letters.vec) | ||||||
|
|
||||||
| # Count occurrences of each unique letter | ||||||
| letter.counts <- table(letters.vec) | ||||||
|
||||||
| letter.counts <- table(letters.vec) | |
| letter.counts <- table(letters.vec)[unique.letters] |
Copilot
AI
Oct 7, 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 table() function is called on letters.vec but only values from unique.letters are accessed. This creates unnecessary computation for letters that won't be displayed. Consider using letter.counts <- table(letters.vec)[unique.letters] or iterating directly over the table results.
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 regex pattern
[^a-z]will remove uppercase letters beforetolower()is applied. This should be[^a-zA-Z]to preserve all letters before converting to lowercase, or the order should be reversed to applytolower()first.