tags:

views:

168

answers:

4
n=0
k=[]
q=[]
j=0
if n!=-1
  p=gets
    n=p.to_i
    for i in (1..n)
      l=gets
      k[i]=l.to_i
      a=a+k[i]
    end
    if a%n !=0
      q[j] = -1
      continue
    end
    a=a/n
    for i in (1..n)
      if k[i] >a
      q[j]=q[j]+(a-k[i])
      end
    end
      j=j+1
end
      for i in (1..j)
        puts(q[j])
      end

it gives me errors like this line 23 .. unexpected end line 26 unexpected end .. can any one debug ?

i have put identations now .. its in ruby !!

my inputs are in integers!!

A: 

It looks like you have two end's in a row about six lines up from the bottom, but it's hard to tell since the program is not consistently indented throughout.

What is the program supposed to do?

Is this homework?

Barry Brown
+1 because I'm struggling with the indentation too. And because you love Perl.
Chris Lutz
A: 

You have end at lines 21 and 22, back to back, same indentation -- I think one of them is redundant.

Alex Martelli
I think they're all necessary, but it's hard to tell with that awful indentation style. I'm almost tempted to edit the post and indent it properly.
Chris Lutz
A: 

In addition to what everyone else has suggested, the line q[j] -1 should also be q[j] = q[j] - 1.

Chris Bunch
+3  A: 

I have re-indented the provided code:

n=0
k=[]
q=[]
j=0
if n!=-1
  p=gets
  n=p.to_i
  for i in (1..n)
    l=gets
    k[i]=l.to_i
    a=a+k[i]
  end
  if a%n !=0
    q[j] = -1
    continue
  end
  a=a/n
  for i in (1..n)
    if k[i] >a
      q[j]=q[j]+(a-k[i])
    end
  end
  j=j+1
end
for i in (1..j)
  puts(q[j])
end

I ran this code using IRB and I found two issues. The first is that there is no such thing as "continue". You should be able to use "next" but this statement is not within a loop. The second problem is that the variable "a" is undefined and therefore the "+" operator throws an error. My assumption is that this code is really inside another loop, probably a "while", and that the "a" variable is defined outside of it and that is the reason for the "j" variable. Maybe the "if n!= -1" is supposed to be "while n!= -1". Another problem that might show up (I didn't try it) relates to q[j] not being initialized in all situations before being used. Maybe this

if a%n != 0
  q[j] = -1
  next
end

should be

if a%n != 0
  q[j] = -1
  next
else
  q[j] = 0
end
martinatime
It should be said by someone that either the variables need more descriptive names or this needs comments.
method
I completely agree. I was just trying to help with what was given.
martinatime
I can't believe you spent the time to try :)
Ian Terrell
Ian, isn't that the spirit of SO.com? I'm relatively new to Ruby myself so I guess it was both for karma and education.
martinatime