Linear Search
linear_search <- function(data_list,item) {
for (i in 1:length(data_list)) {
# Check if the current element matches the target value
if (data_list[[i]] == item) {
# Return the index (position) if a match is found
return(i)
# If the loop finishes without finding a match, return -1
return(-1)
my_list <- list("apple", "banana", "cherry", "date")
#search_item <- "cherry"
search_item <- "grape"
# Search for "cherry"
result<- linear_search(my_list,search_item)
if (result!= -1) {
print(paste(search_item, "found at index:", result1))
} else {
print(paste(search_item, "not found"))
}
Binary Search
binarySearch = function(arr,item) {
beg <- 1;
end <- length(arr)
while (beg <= end){
mid <- [Link](round((beg + end) / 2))
if (abs(arr[mid] - item) ==0) {
return(mid)
} else if (arr[mid] < item) {
beg <- mid + 1
} else {
end <- mid - 1
}
}
return(0)
arr <- c(20,10,40,30,50)
sorted_arr <- sort(arr)
item <- 30
loc <- binarySearch(sorted_arr, item)
if (loc!=0){
cat("Element is present at index ",loc)
}else{
print("element not found")
}
Sum of n numbers
# Ask the user for a number
number <- readline(prompt="Enter a positive integer: ")
# Convert the input string to an integer
num <- [Link](number)
# Use a while loop to calculate the sum
sum=0
i=1
while (i <= num) {
sum <- sum + i
i <- i + 1
}
# Print the result
print(paste("The sum of numbers from 1 to num is:", sum))
Matrix Operations
# Creating 1st Matrix
mat1 = matrix(c(10, 20, 30, 40, 50, 60), nrow = 2, ncol = 3)
# Creating 2nd Matrix
mat2= matrix(c(90, 80, 70, 110, 120, 100), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(mat1)
num_of_cols = ncol(mat1)
# Creating sum matrix to store results
sum = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(mat1)
print(mat2)
# Calculating sum of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
sum[row, col] = mat1[row, col] + mat2[row, col]
# Printing resultant sum matrix
print(sum)
# Creating difference matrix to store results
diff = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Calculating difference of matrices
for(row in 1:num_of_rows)
for(col in 1:num_of_cols)
diff[row, col] = mat1[row, col] - mat2[row, col]
# Printing resultant difference matrix
print(diff)
prod = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Calculating product of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
prod[row, col] = mat1[row, col] * mat2[row, col]
# Printing resultant product matrix
print(prod)
Maximum element of list using function
# User defined function to find maximum value in a list
find_max <- function(lst) {
# Convert list to numeric vector
values <- unlist(lst)
# Assume first element is maximum
max_val <- values[1]
# Loop to compare values
for (i in values) {
if (i > max_val) {
max_val <- i
return(max_val)
}
# Creating a list
my_list <- list(10, 25, 7, 45, 18)
# Calling the function
result <- find_max(my_list)
# Display result
print(paste("Maximum value in the list is:", result))
String Palindrome
check_palindrome <- function(str) {
n <- nchar(str)
for(i in 1:(n/2)) {
if(substr(str,i,i) != substr(str,n-i+1,n-i+1)) {
print("Not Palindrome")
return()
print("Palindrome")
}
check_palindrome("madam")
Multiplication Table from 1-12
for(i in 1:12) {
cat("Multiplication table of", i, "\n")
for(j in 1:10) {
cat(i, "x", j, "=", i*j, "\n")
cat("\n")