- Write a program to calculate the sum of digits of a given number.
- Create a function to compute the factorial of a number using both iterative and recursive methods.
- Implement a program to convert a number from decimal to binary, octal, and hexadecimal.
- Write a function to count the number of vowels and consonants in a given string.
- Create a program that reverses a given string without using built-in functions.
- Write a program to merge two sorted lists into a single sorted list.
- Create a function to find the largest and smallest numbers in a list.
- Develop a program to count the frequency of each character in a string.
- Write a program to convert a binary number (as a string) to its decimal equivalent.
- Create a function to convert temperatures between Celsius, Fahrenheit, and Kelvin.
- Implement a program to find the sum of even and odd numbers from a list.
- Develop a program to sort a list of tuples based on the second element in each tuple.
- Create a program to generate a random password of specified length with letters and digits.
- Write a function to remove duplicate elements from a list while preserving the order.
- Implement a program to find common elements between two lists.
- Create a function to find all prime factors of a given number.
- Develop a program to find the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm.
- Implement a program to find the n_th largest number in a list.
- Write a function to remove all whitespace characters from a given string.
- Implement a program to transpose a given matrix.
R Programs for 20 Problems - Professor Gunupudi
1. Program to calculate the sum of digits of a given
number
sum_of_digits <- function(n) {
digits <-
as.numeric(unlist(strsplit(as.character(n), "")))
return(sum(digits))
}
# Example
sum_of_digits(1234) # Output: 10
2. Function to compute the factorial using iterative and
recursive methods
# Iterative method
factorial_iter <- function(n) {
result <- 1
for (i in 1:n) {
result <- result * i
}
return(result)
}
# Recursive method
factorial_rec <- function(n) {
if (n <= 1) return(1)
return(n * factorial_rec(n - 1))
}
# Example
factorial_iter(5) # Output: 120
factorial_rec(5) # Output: 120
3. Convert a number from decimal to binary, octal, and
hexadecimal
convert_number <- function(n) {
binary <-
paste(rev(as.integer(intToBits(n))), collapse = "")
binary <- sub("^0+",
"", binary)
octal <- as.character(as.octmode(n))
hex <-
toupper(as.character(as.hexmode(n)))
return(list(Binary = binary, Octal =
octal, Hexadecimal = hex))
}
# Example
convert_number(255)
4. Count number of vowels and consonants in a string
count_vowels_consonants <- function(text) {
text <-
tolower(gsub("[^a-z]", "", text))
chars <- unlist(strsplit(text,
""))
vowels <- sum(chars %in% c('a', 'e',
'i', 'o', 'u'))
consonants <- length(chars) - vowels
return(list(Vowels = vowels, Consonants
= consonants))
}
# Example
count_vowels_consonants("Hello World")
5. Reverse a string without using built-in reverse
function
reverse_string <- function(s) {
chars <- strsplit(s,
"")[[1]]
reversed <- chars[length(chars):1]
return(paste(reversed, collapse =
""))
}
# Example
reverse_string("OpenAI")
6. Merge two sorted lists into a single sorted list
merge_sorted_lists <- function(list1, list2) {
merged <- sort(c(list1, list2))
return(merged)
}
# Example
merge_sorted_lists(c(1, 3, 5), c(2, 4, 6))
7. Find the largest and smallest numbers in a list
find_min_max <- function(vec) {
return(list(Smallest = min(vec),
Largest = max(vec)))
}
# Example
find_min_max(c(12, 5, 8, 19, 3))
8. Count the frequency of each character in a string
char_frequency <- function(text) {
chars <-
unlist(strsplit(tolower(gsub("[^a-z]", "", text)),
""))
return(table(chars))
}
# Example
char_frequency("Hello World")
9. Convert a binary number (string) to its decimal
equivalent
binary_to_decimal <- function(bin_str) {
digits <-
as.numeric(strsplit(bin_str, "")[[1]])
decimal <- sum(digits *
2^((length(digits)-1):0))
return(decimal)
}
# Example
binary_to_decimal("1101")
10. Convert temperatures between Celsius, Fahrenheit, and
Kelvin
convert_temperature <- function(value, from, to) {
if (from == "C") {
if (to == "F")
return((value * 9/5) + 32)
if (to == "K") return(value
+ 273.15)
} else if (from == "F") {
if (to == "C")
return((value - 32) * 5/9)
if (to == "K")
return((value - 32) * 5/9 + 273.15)
} else if (from == "K") {
if (to == "C") return(value
- 273.15)
if (to == "F")
return((value - 273.15) * 9/5 + 32)
}
return(NA)
}
# Example
convert_temperature(100, "C", "F")
11. Find the sum of even and odd numbers from a list
sum_even_odd <- function(numbers) {
even_sum <- sum(numbers[numbers %% 2
== 0])
odd_sum <- sum(numbers[numbers %% 2
!= 0])
return(list(Even_Sum = even_sum,
Odd_Sum = odd_sum))
}
# Example
sum_even_odd(c(1, 2, 3, 4, 5, 6))
12. Sort a list of tuples based on the second element
sort_tuples_by_second <- function(tuples) {
sorted <-
tuples[order(sapply(tuples, function(x) x[2]))]
return(sorted)
}
# Example
list_of_tuples <- list(c("a", 3), c("b", 1),
c("c", 2))
sort_tuples_by_second(list_of_tuples)
13. Generate a random password of specified length with
letters and digits
generate_password <- function(length) {
chars <- c(letters, LETTERS, 0:9)
paste0(sample(chars, length, replace =
TRUE), collapse = "")
}
# Example
generate_password(10)
14. Remove duplicate elements from a list while preserving
the order
remove_duplicates <- function(vec) {
unique_vec <- vec[!duplicated(vec)]
return(unique_vec)
}
# Example
remove_duplicates(c(1, 2, 2, 3, 1, 4))
15. Find common elements between two lists
find_common_elements <- function(list1, list2) {
return(intersect(list1, list2))
}
# Example
find_common_elements(c(1, 2, 3, 4), c(3, 4, 5, 6))
16. Find all prime factors of a given number
prime_factors <- function(n) {
factors <- c()
i <- 2
while (i <= n) {
if (n %% i == 0) {
factors <- c(factors, i)
n <- n / i
} else {
i <- i + 1
}
}
return(factors)
}
# Example
prime_factors(84)
17. Find the Greatest Common Divisor (GCD) using the
Euclidean algorithm
gcd <- function(a, b) {
while (b != 0) {
temp <- b
b <- a %% b
a <- temp
}
return(a)
}
# Example
gcd(48, 18)
18. Find the n-th largest number in a list
nth_largest <- function(vec, n) {
sorted <- sort(unique(vec),
decreasing = TRUE)
if (n <= length(sorted)) {
return(sorted[n])
} else {
return(NA)
}
}
# Example
nth_largest(c(4, 1, 7, 7, 2, 9), 3)
19. Remove all whitespace characters from a given string
remove_whitespace <- function(text) {
return(gsub("\\s+",
"", text))
}
# Example
remove_whitespace(" Hello World \n\t")
20. Transpose a given matrix
transpose_matrix <- function(mat) {
return(t(mat))
}
# Example
m <- matrix(1:6, nrow = 2, ncol = 3)
transpose_matrix(m)
Data Visualiztion
ReplyDelete