title: R solving hackerrank challenge
body: <p>I would like to solve <a href="https://www.hackerrank.com/challenges/bday-gift" rel="noreferrer">the challenge</a>. The language of my preference is R. I am not sure how to receive input. On hackerrank coding window it says that </p>
  
  <pre><code>"# Enter your code here. Read input from STDIN. Print output to STDOUT"
  </code></pre>
  
  <p>So far I am used to receiving input by using</p>
  
  <pre><code>v1 &lt;- readline("Enter two integers: ")
  </code></pre>
  
  <p>How should i receive input on hackerrank? I tried to see solved examples but couldn't find any solved examples.</p>
  
  <hr>
  
  <p><strong>update 1</strong></p>
  
  <p>Below code works in R. Only problem is number of steps and ball values are not provided from keyboard input. We have to update them manually on line 1 and line2. How could I get update below solution so that it works on hackerrank?</p>
  
  <pre><code>steps=4
  ball_numbers=c(1,2,2,2)
  d=as.data.frame(c(0,1))
  
      for (i in (1:(length(ball_numbers)-1)))
      {
        assign(x = paste("A", i, sep = ""),value = c(0,1))
        e &lt;- as.data.frame(get(paste("A", i, sep = "")))
        colnames(e) &lt;- paste("A", i, sep="")
        d &lt;- merge(d,e)
  }
  
  
  d=as.matrix(t(d))
  answer=sum(ball_numbers %*% d)/ncol(d)
  </code></pre>
  
  <hr>
  
  <p><strong>update2</strong></p>
  
  <p>Below code produces correct answer</p>
  
  <pre><code># Enter your code here. Read input from STDIN. Print output to STDOUT
  nums &lt;- read.table("/dev/stdin", sep=" ");
  nums &lt;- as.matrix(as.data.frame(t(nums)))
  
  steps=nums[1]
  ball_numbers=nums[2:length(nums)]
  d=as.data.frame(c(0,1))
  
  for (i in (1:(length(ball_numbers)-1)))
  {
      assign(paste("A", i, sep = ""),value = c(0,1))
      e &lt;- as.data.frame(get(paste("A", i, sep = "")))
      colnames(e) &lt;- paste("A", i, sep="")
      d &lt;- merge(d,e)
  }
  
  
  d=as.matrix(t(d))
  #answer=as.numeric(format(round(sum(ball_numbers %*% d)/ncol(d),1),nsmall=1))
  answer = print(format(sum(ball_numbers %*% d)/ncol(d),nsmall=1, digits = 1), quote = F)
  write.table(as.numeric(answer), sep = "", append=T, row.names = F, col.names = F,quote = FALSE,)
  </code></pre>
  
  <p>I get below output</p>
  
  <pre><code>[1] 2.0
  2
  </code></pre>
  
  <p>which is different from expected output which is below. How can i modify my code to get the correct format of output</p>
  
  <pre><code>2.0
  </code></pre>