0
Can you help me to solve this in R language ??
You need to create a function that takes two parameters and returns the sum of all numbers between the two parameters inclusive. The given code takes two numbers from input and passes them to a function called rangeSum(). Task Define the rangeSum() function, so that the code works as expected. Sample Input 4 9 Sample Output [1] 39 Explanation For the inputs 4 and 9, the returned value should be 39.
15 Answers
+ 18
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x, y) {
s=0
for (n in x:y) s <- s + n
return (s)
}
print(rangeSum(x, y))
+ 4
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
z <- as.integer(input[3])
print (max(x,y,z))
// Try not to follow me
+ 2
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x, y) {
s=0
for (n in x:y) s <- s + n
return (s)
}
print(rangeSum(x, y))
+ 1
I don't really like how SoloProg handed out the solution to you and you even marked it as best .
Maybe he should have printed the steps you should follow or let you know the concepts that you need to learn and post your attempt first to help you.
Sololearn is a self learning platform not a coding agency . But then people do what they want as i am no one to say what one should do .
+ 1
My code:
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x, y) {
s=0
for (n in x:y) s <- s + n
return (s)
}
print(rangeSum(x, y))
+ 1
#define the function
sum<-function(x,y){
g=0
for (n in x:y) {
g<-g+n
}
return (g)
}
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
print(sum(x,y))
0
Niloufar Kashanian
If you post your attempt, then people who are familiar with R will be able to help you resolve the problem.
PS: I have only just started to learn R, so would like to see the concept
0
You need to create a function that takes two parameters and returns the sum of all numbers between the two parameters inclusive.
The given code takes two numbers from input and passes them to a function called rangeSum().
Task
Define the rangeSum() function, so that the code works as expected.
Sample Input
4
9
Sample Output
[1] 39
Explanation
For the inputs 4 and 9, the returned value should be 39.
0
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x, y) {
s=0
for (n in x:y) s <- s + n
return (s)
}
print(rangeSum(x, y))
0
rangeSum<-function(x,y){
sum<-0
for(i in x:y){
sum<-sum+i
}
return(sum)
}
result <- rangeSum(x, y)
print(result)
0
input <- readLines('stdin')
a <- as.integer(input[1])
b <- as.integer(input[2])
rangeSum <- function(a, b) {
p=0
for (n in a:b) p <- p + n
return (p)
}
result <- rangeSum(a, b)
print(result)
0
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x,y){
sum <-0
for(i in x:y)
sum <-sum + i
print (sum)
}
rangeSum(x,y)
0
x <- readLines('stdin')
print(x[read ])
- 1
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x, y) {
s=0
for (n in x:y) s <- s + n
return (s)
}
print(rangeSum(x, y))
- 1
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <- function(x, y) {
s=0
for (n in x:y) s <- s + n
return (s)
}
print(rangeSum(x, y))