Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hackerrank : 10 Days of Statistics in R [https://www.hackerrank.com/domains/tutorials/10-days-of-statistics]
# Day 0: Mean, Median, and Mode
# Selecting standard input
con <- file('stdin', open='r')
# We don't need the first input
data_line <- readLines(con)[[2]]
# splitting the data into individual string
split_data <- strsplit(data_line, " ")
# String to integer conversion
data <- as.numeric(unlist(split_data))
# get mode function
getmode <- function(v) {
v <- sort(v)
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
mean_value <- mean(data)
median_value <- median(data)
mode_value <- getmode(data)
cat(mean_value, sep="\n")
cat(median_value, sep="\n")
cat(mode_value, sep="\n")
# Day 0: Weighted Mean
con <- file('stdin', open='r')
inputs <- readLines(con)
# We don't need the first input
data <- inputs[[2]]
weights <- inputs[[3]]
# Converts string to vector
stringToVector <- function(string){
return (as.numeric(unlist(strsplit(string, " "))))
}
result <- sum(stringToVector(data) * stringToVector(weights)) / sum(stringToVector(weights))
# Formatting result
cat(format(round(result, 1), nsmall=1))
# Day 1: Quartiles
# Enter your code here. Read input from STDIN. Print output to STDOUT
con <- file('stdin', open='r')
stringToVector <- function(string){
return (as.numeric(unlist(strsplit(string, " "))))
}
data_line <- readLines(con)[[2]]
v <- stringToVector(data_line)
quart <- function(x) {
x <- sort(x)
n <- length(x)
m <- (n+1)/2
if (floor(m) != m) {
l <- m-1/2; u <- m+1/2
} else {
l <- m-1; u <- m+1
}
o <- c(Q1=median(x[1:l]), median(x) ,Q3=median(x[u:n]))
return (o)
}
x <- quart(v)
cat(x, sep="\n")
# Day 1: Interquartile Range
con <- file('stdin', open='r')
stringToVector <- function(string){
return (as.numeric(unlist(strsplit(string, " "))))
}
inputs <- readLines(con)
data <- stringToVector(inputs[[2]])
repeatations <- stringToVector(inputs[[3]])
total_data <- c()
for (i in 1:length(data)){
total_data <- c(total_data, rep.int(data[i], repeatations[i]))
}
total_data <- sort(total_data)
quart <- function(x) {
x <- sort(x)
n <- length(x)
m <- (n+1)/2
if (floor(m) != m) {
l <- m-1/2; u <- m+1/2
} else {
l <- m-1; u <- m+1
}
return (median(x[u:n]) - median(x[1:l]))
}
cat(format(round(quart(total_data), 1), nsmall=1))
# Day 1: Standard Deviation
con <- file('stdin', open='r')
stringToVector <- function(string){
return (as.numeric(unlist(strsplit(string, " "))))
}
std <- function(v){
return (sd(v) * sqrt((length(v)-1)/length(v)))
}
inputs <- readLines(con)
data <- stringToVector(inputs[[2]])
cat(format(round(std(data), 1), nsmall=1))
# Enter your code here. Read input from STDIN. Print output to STDOUT
boy <- 1.09
girl <- 1
N <- 6
boy_atleast <- 3
p <- boy / (boy + girl)
combination <- function(n, x){
return (factorial(n)/(factorial(x) * factorial(n-x)))
}
binomialDist <- function(X, N, P){
return (combination(N, X) * P^X * (1-P)^(N-X))
}
leastBinomialDist <- function(R, N, P){
result <- 0
for (i in R:N){
result <- result + binomialDist(i, N, P)
}
return (result)
}
res <- leastBinomialDist(boy_atleast, N, p)
cat(format(round(res, 3), nsmall=1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment