# Help needed with ATM CodeChef problem

Hi everyone,

Link to CodeChef’s question.

I created an `if` statement that produces the proper output, but I am having trouble turning this solution into a function with standard inputs. In my solution, I am assigning input values, which I assume is not allowed and is the reason it is rejected every time.

The solution is written in the R language.

``````withdraw <- 30
balance <- 120.00

if(withdraw %% 5 == 0 & balance >= x + 0.50){
balance - withdraw - 0.50
}else{
withdraw
}
format(z, nsmall = 2)

[1] 89.5
[1] "89.50"``````

you can use readline() to take input

withdraw = readline();
withdraw = as.integer(withdraw);

balance = readline();
balance = as.integer(withdraw);

Read more about input and output

Thank you. Would this be within the function or outside it? I am using your suggestion and all it is doing is freezing up my RStudio.

``````withdraw = readline();
withdraw = as.integer(withdraw);
balance = readline();
balance = as.integer(withdraw);
atm <- function(withdraw, balance){
if(withdraw %% 5 == 0 & balance >= withdraw + 0.50){
balance - withdraw - 0.50
}else{
withdraw
}
}
atm(30, 120.00)``````

con=file(stdin, r)
withdr<-scan(file=con, what=integer(0), n=1)
bal<-scan(file=con, what=integer(0), n=1)
atm <- function(withdraw, balance){
if(withdraw %% 5 == 0 & balance >= withdraw + 0.50){
balance - withdraw - 0.50
}else{
withdraw
}
}
atm(withdr, bal)

Try this code

Hmmm. This code also freezes my RStudio. Also, when submitted to CodeChef returns wrong answer. Any ideas?

The problem with RS studio is that this requires input from stdin and you are probably not providing any input. hence the freezing. As for the wrong answer i have only provided the code to accept input and not altered your logic in any way.
Try your code here https://www.tutorialspoint.com/execute_r_online.php

and dont forget to enter input in the STDIN tab before executing!

When you say input in STDIN, are you referring to the:

con=file(stdin, r)

should be:

``````con=file("30, 120.00", "r")
``````

Ultimately, I want to see what the correct acceptable solution looks like so I can implement that in future questions. This is my first attempt using R as a functional programming language for CodeForces.

no you are not supposed to set the variables like that. Value of balance and withdrawal will keep changing so you need to read the input from “stdin” and set the values accordingly.

And if language is not an issue that i would suggest to use faster languages like C++ or JAVA.

I used the code you provided the main.r tab and the following in the STDIN tab on separate lines:

30
120

When I hit execute on both tabs, noting happens.

input in same line and spac seperated and no decimals.
so
30 120
will work

for input 31 120 your code gives output 31 instead of 120. Thats why wrong answer.

When executing in CodingGround, all I get for both tabs in the result side is:

\$Rscript main.r

With no output below. I would upload screenshots but I can’t as a new user.

Also, how on earth would I submit this as a solution to CodeForces? And can’t I use your original suggestion in RStudio? Somehow utilizing this:

``````withdraw = readline();
withdraw = as.integer(withdraw);

balance = readline();
balance = as.integer(withdraw);
``````

This is so frustrating. I can’t imagine how anyone successfully uses R with CodeForces. Which sucks because that is the language I am interested in. Coupled with the fact I am new to programming.

it is possible but since readLine() reads the whole line at once the second readline() will have nothing to read when submiting. Note that on codeforces the input both balance and withdrawal is given in single line. So you will have to use readline only once and then parse int to get the two number from it… all these will take time and might cause you answer to TLE.

R is not optimized for competitive programming at all so you will face a lot of problems ahead. Since you are new maybe start with python and then move to C++ or Java to really get the feel of how awesome algorithms and data structures are!

1 Like