Hacker Rank: Introduced 30 days of code to brush up your skills for interview.
Following blog contains the solution for problems which can be submitted through R programming.
Better solution for below codes are welcome.
#Day -2 Problem - https://www.hackerrank.com/challenges/30-operators
#Solution -
input <- file('stdin','r')
mealcost <- as.double(readLines(input,n=1))
tipPercent <- as.double(readLines(input,n=1))
taxPercent <- as.double(readLines(input,n=1))
tip <- as.double((mealcost*tipPercent)/100)
tax <- as.double((mealcost*taxPercent)/100)
totalcost <- round(mealcost + tip + tax)
cat("The total meal cost is",totalcost ,"dollars.")
#Day -3 Problem - https://www.hackerrank.com/challenges/30-conditional-statements
#Solution -
input<-file('stdin', 'r')
x <- readLines(input, n=1)
y <- as.integer(x)
if((y%%2) == 1){
cat("Weird")
}else if((y%%2) == 0){
if(y>=2 && y<=5){
cat("Not Weird")
}else if(y>=6 && y <=20){
cat("Weird")
}else{
cat("Not Weird")
}
}
#Day - 6 Problem - https://www.hackerrank.com/challenges/30-review-loop
#Solution -
input <- file('stdin','r')
x <- readLines(input)
z <- as.integer(x[1]) + 1
for(i in 2:z){
s <- strsplit(x[i], "")[[1]]
k <- s[c(TRUE, FALSE)]
l <- s[c(FALSE,TRUE)]
cat(k, sep="", collapse="","\t")
cat(l, sep="", collapse="","\n")
}
#Day - 7 Problem - https://www.hackerrank.com/challenges/30-arrays
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- as.integer(x[1])
s <- as.integer(strsplit(x[2]," ")[[1]])
z <- array(s, c(1,y))
v <- rev(z)
cat(v)
#Day - 9 Problem - https://www.hackerrank.com/challenges/30-recursion
#Solution -
input <- file('stdin','r')
x <- as.integer(readLines(input, n=1))
factorial <- 1
recur_factorial <- function(n) {
if(n == 0) {
print(1)
} else {
for(i in 1:n) {
factorial = factorial*i
}
print(factorial)
}
}
recur_factorial(x)
#Day - 10 Problem - https://www.hackerrank.com/challenges/30-binary-numbers
#Solution -
input <- file('stdin','r')
x <- as.integer(readLines(input, n=1))
l <- c()
i <- 1
maxcount <- 0
tempcount <- 0
while(x > 0){
l[[i]] <- (x%%2)
x <- (x%/%2)
i <- i + 1
}
for(j in 1:length(l)){
if(l[[j]] == 0) {
tempcount <- 0
}
else{
tempcount <- tempcount + 1
}
if(tempcount > maxcount){
maxcount <- tempcount
}
}
maxcount
#Day - 11 Problem - https://www.hackerrank.com/challenges/30-2d-arrays
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- length(x)
minValueInArray <- -9;
elementsInHourGlass <- 7;
tempsum <- minValueInArray * elementsInHourGlass;
maxsum <- 0
out <- array(dim = c( 6, 6))
for(i in 1:y){
out[i,] <- c(as.integer(strsplit(x[i]," ")[[1]]))
}
for(i in 1:(y-2)){
for(j in 1:(y-2)){
maxsum <- out[i,j] + out[i,j+1] + out[i,j+2] + out[i+1,j+1] + out[i+2,j] + out[i+2,j+1] + out[i+2,j+2]
if(tempsum < maxsum){
tempsum = maxsum
}
}
}
cat(tempsum)
#Day - 20 Problem - https://www.hackerrank.com/challenges/30-sorting
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- as.integer(x[1])
a <- c(as.integer(strsplit(x[2]," ")[[1]]))
numberOfSwaps <- 0
if(y >=2 && y <=600 ){
for (i in 1:y){
#Track number of elements swapped during a single array traversal
for (j in 1:(y-1)) {
#Swap adjacent elements if they are in decreasing order
if (a[j] > (a[j + 1])) {
temp <- a[j]
a[j] <- a[j+1]
a[j+1] <- temp
numberOfSwaps <- numberOfSwaps + 1
}
}
# // If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break
}
}
}
cat("Array is sorted in",numberOfSwaps,"swaps.\n")
cat("First Element:", a[1],"\n")
cat("Last Element:" ,a[y],"\n")
#Day 26 Problem - https://www.hackerrank.com/challenges/30-nested-logic
Solution -
input <- file('stdin','r')
x <- readLines(input)
a <- c(as.integer(strsplit(x[1]," ")[[1]]))
b <- c(as.integer(strsplit(x[2]," ")[[1]]))
fine <- 0
if(a[1] >= 1 && a[1] <= 31 && b[1] >= 1 && b[1] <= 31 && a[2] >= 1 && a[2] <= 12 && b[2] >= 1 && b[2] <=12 && a[3] >= 1 && a[3] <= 3000 && b[3] >= 1 && b[3] <= 3000){
if(a[3] == b[3] || a[3] < b[3]){
if(a[2] == b[2] || a[2] < b[2] || a[3] < b[3]) {
if(a[1] == b[1] || a[1] < b[1] || a[2] < b[2] || a[3] < b[3]){
fine <- 0
}else
fine <- (a[1] - b[1]) * 15
}else
fine <- (a[2] - b[2]) * 500
}else
fine <- 10000
}
cat(fine)
#Day -27 Problem - https://www.hackerrank.com/challenges/30-testing
#Solution -
cat(5,"\n");
cat("4 3 \n")
cat("0 -3 4 2 \n")
cat("5 2 \n")
cat("0 -3 4 2 2 \n")
cat("6 3 \n")
cat("0 -3 4 2 1 1 \n")
cat("7 2 \n")
cat("0 -3 1 1 1 1 1 \n")
cat("3 3 \n")
cat("0 -3 4 \n")
#Day - 28 Problem - https://www.hackerrank.com/challenges/30-regex-patterns
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- as.integer(x[1])
out <- array(dim = c( y, 2))
for(i in 1:y){
out[i,] <- c(strsplit(x[i+1]," ")[[1]])
}
z <- sort(out[grep("@gmail",out)-y])
for(i in 1:length(z)){
cat(z[i],"\n")
}
#Day - 29 Problem - https://www.hackerrank.com/challenges/30-bitwise-and
#Solution -
input <- file('stdin','r')
a <- readLines(input)
b <- as.integer(a[1])
c <- array(dim = c(b,2))
for(i in 1:b){
g <- 0
c[i,] <- c(as.integer(strsplit(a[i+1]," ")[[1]]))
for(d in 1:(c[i,1]-1)){
h <- d + 1
while(h <= c[i,1])
{
if((bitwAnd(d,h) < c[i,2]) && (bitwAnd(d,h) > g)){
g <- bitwAnd(d,h)
}
h <- h + 1
}
}
cat(g,"\n")
}
Following blog contains the solution for problems which can be submitted through R programming.
Better solution for below codes are welcome.
#Day -2 Problem - https://www.hackerrank.com/challenges/30-operators
#Solution -
input <- file('stdin','r')
mealcost <- as.double(readLines(input,n=1))
tipPercent <- as.double(readLines(input,n=1))
taxPercent <- as.double(readLines(input,n=1))
tip <- as.double((mealcost*tipPercent)/100)
tax <- as.double((mealcost*taxPercent)/100)
totalcost <- round(mealcost + tip + tax)
cat("The total meal cost is",totalcost ,"dollars.")
#Day -3 Problem - https://www.hackerrank.com/challenges/30-conditional-statements
#Solution -
input<-file('stdin', 'r')
x <- readLines(input, n=1)
y <- as.integer(x)
if((y%%2) == 1){
cat("Weird")
}else if((y%%2) == 0){
if(y>=2 && y<=5){
cat("Not Weird")
}else if(y>=6 && y <=20){
cat("Weird")
}else{
cat("Not Weird")
}
}
#Day - 6 Problem - https://www.hackerrank.com/challenges/30-review-loop
#Solution -
input <- file('stdin','r')
x <- readLines(input)
z <- as.integer(x[1]) + 1
for(i in 2:z){
s <- strsplit(x[i], "")[[1]]
k <- s[c(TRUE, FALSE)]
l <- s[c(FALSE,TRUE)]
cat(k, sep="", collapse="","\t")
cat(l, sep="", collapse="","\n")
}
#Day - 7 Problem - https://www.hackerrank.com/challenges/30-arrays
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- as.integer(x[1])
s <- as.integer(strsplit(x[2]," ")[[1]])
z <- array(s, c(1,y))
v <- rev(z)
cat(v)
#Day - 9 Problem - https://www.hackerrank.com/challenges/30-recursion
#Solution -
input <- file('stdin','r')
x <- as.integer(readLines(input, n=1))
factorial <- 1
recur_factorial <- function(n) {
if(n == 0) {
print(1)
} else {
for(i in 1:n) {
factorial = factorial*i
}
print(factorial)
}
}
recur_factorial(x)
#Day - 10 Problem - https://www.hackerrank.com/challenges/30-binary-numbers
#Solution -
input <- file('stdin','r')
x <- as.integer(readLines(input, n=1))
l <- c()
i <- 1
maxcount <- 0
tempcount <- 0
while(x > 0){
l[[i]] <- (x%%2)
x <- (x%/%2)
i <- i + 1
}
for(j in 1:length(l)){
if(l[[j]] == 0) {
tempcount <- 0
}
else{
tempcount <- tempcount + 1
}
if(tempcount > maxcount){
maxcount <- tempcount
}
}
maxcount
#Day - 11 Problem - https://www.hackerrank.com/challenges/30-2d-arrays
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- length(x)
minValueInArray <- -9;
elementsInHourGlass <- 7;
tempsum <- minValueInArray * elementsInHourGlass;
maxsum <- 0
out <- array(dim = c( 6, 6))
for(i in 1:y){
out[i,] <- c(as.integer(strsplit(x[i]," ")[[1]]))
}
for(i in 1:(y-2)){
for(j in 1:(y-2)){
maxsum <- out[i,j] + out[i,j+1] + out[i,j+2] + out[i+1,j+1] + out[i+2,j] + out[i+2,j+1] + out[i+2,j+2]
if(tempsum < maxsum){
tempsum = maxsum
}
}
}
cat(tempsum)
#Day - 20 Problem - https://www.hackerrank.com/challenges/30-sorting
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- as.integer(x[1])
a <- c(as.integer(strsplit(x[2]," ")[[1]]))
numberOfSwaps <- 0
if(y >=2 && y <=600 ){
for (i in 1:y){
#Track number of elements swapped during a single array traversal
for (j in 1:(y-1)) {
#Swap adjacent elements if they are in decreasing order
if (a[j] > (a[j + 1])) {
temp <- a[j]
a[j] <- a[j+1]
a[j+1] <- temp
numberOfSwaps <- numberOfSwaps + 1
}
}
# // If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break
}
}
}
cat("Array is sorted in",numberOfSwaps,"swaps.\n")
cat("First Element:", a[1],"\n")
cat("Last Element:" ,a[y],"\n")
#Day 26 Problem - https://www.hackerrank.com/challenges/30-nested-logic
Solution -
input <- file('stdin','r')
x <- readLines(input)
a <- c(as.integer(strsplit(x[1]," ")[[1]]))
b <- c(as.integer(strsplit(x[2]," ")[[1]]))
fine <- 0
if(a[1] >= 1 && a[1] <= 31 && b[1] >= 1 && b[1] <= 31 && a[2] >= 1 && a[2] <= 12 && b[2] >= 1 && b[2] <=12 && a[3] >= 1 && a[3] <= 3000 && b[3] >= 1 && b[3] <= 3000){
if(a[3] == b[3] || a[3] < b[3]){
if(a[2] == b[2] || a[2] < b[2] || a[3] < b[3]) {
if(a[1] == b[1] || a[1] < b[1] || a[2] < b[2] || a[3] < b[3]){
fine <- 0
}else
fine <- (a[1] - b[1]) * 15
}else
fine <- (a[2] - b[2]) * 500
}else
fine <- 10000
}
cat(fine)
#Day -27 Problem - https://www.hackerrank.com/challenges/30-testing
#Solution -
cat(5,"\n");
cat("4 3 \n")
cat("0 -3 4 2 \n")
cat("5 2 \n")
cat("0 -3 4 2 2 \n")
cat("6 3 \n")
cat("0 -3 4 2 1 1 \n")
cat("7 2 \n")
cat("0 -3 1 1 1 1 1 \n")
cat("3 3 \n")
cat("0 -3 4 \n")
#Day - 28 Problem - https://www.hackerrank.com/challenges/30-regex-patterns
#Solution -
input <- file('stdin','r')
x <- readLines(input)
y <- as.integer(x[1])
out <- array(dim = c( y, 2))
for(i in 1:y){
out[i,] <- c(strsplit(x[i+1]," ")[[1]])
}
z <- sort(out[grep("@gmail",out)-y])
for(i in 1:length(z)){
cat(z[i],"\n")
}
#Day - 29 Problem - https://www.hackerrank.com/challenges/30-bitwise-and
#Solution -
input <- file('stdin','r')
a <- readLines(input)
b <- as.integer(a[1])
c <- array(dim = c(b,2))
for(i in 1:b){
g <- 0
c[i,] <- c(as.integer(strsplit(a[i+1]," ")[[1]]))
for(d in 1:(c[i,1]-1)){
h <- d + 1
while(h <= c[i,1])
{
if((bitwAnd(d,h) < c[i,2]) && (bitwAnd(d,h) > g)){
g <- bitwAnd(d,h)
}
h <- h + 1
}
}
cat(g,"\n")
}