0

I was playing around with some challenges at codeeval.com and I get stuck when it comes to submitting my code. There is this weird bunch of code and I just don't understand, where to put my code :) Here is what I see:

 =begin
 Sample code to read in test cases:
 File.open(ARGV[0]).each_line do |line|
 #Do something with line, ignore empty lines
 #...
 end
 =end

The task is: Given numbers x and n, where n is a power of 2, print out the smallest multiple of n which is greater than or equal to x. Do not use division or modulo operator.

INPUT SAMPLE:

The first argument will be a path to a filename containing a comma separated list of two integers, one list per line. E.g.

13,8

17,16

OUTPUT SAMPLE:

Print to stdout, the smallest multiple of n which is greater than or equal to x, one per line. E.g.

16
32

My code works, when I run it through the terminal. It looks like this:

def multiples(x, n)
while n < x
n *= 2
end
puts n
end 

Thank you for your attention! :)

2 Answers2

0
# You can place your method definitions and other things 
# needed to solve the problem right here.
# ...   

# Code to read in test cases : 
File.open(ARGV[0]).each_line do |line|
    # You should solve stuffs here and print to stdout.
    # Parse your input values from the line variable,
    # which will contain a single line from the input file.
    # And print the output to stdout (puts or print).
end

For instance, let the question be that it asks you to find and print (to stdout) the sum of all the pairs of numbers inside the given file. The pairs will be placed on individual lines inside the file.

Say the input file (input.txt) will be like this :

1,2
3,4
5,10

Then you can (I'm not telling you should, there are other ways) write the solution (solution.rb) to solve it as :

def add(a, b)
    a + b
end

File.open(ARGV[0]).each_line do |line|
    a, b = line.split(',').map { |x| x.to_i }
    puts add(a, b)
end

To test it locally invoke it as :

$ ruby solution.rb input.txt
limekin
  • 1,934
  • 1
  • 12
  • 15
0

So, thanks for the support. I also fixed the code. My first properly solved codeeval problem :D

def multiples(x, n)
    m = 0
    while m < x
    m += n  
    end
    puts m
end     



 File.open(ARGV[0]).each_line do |line|
    x, n = line.split(',').map { |x| x.to_i }
    puts multiples(x, n)
 end