ruby Argument Error: wrong number of arguments [duplicate] - ruby

This question already has answers here:
Using stack to find the greatest common divisor
(2 answers)
Closed 9 years ago.
Hi All I am new to ruby and I am trying to implement the alogrithm to find the greatest
common divisor using a stack:
Here is my code:
def d8(a,b)
return a if (a==b)
s = Stack.new
s.push(b)
s.push(a)
c1 = s.pop(a)
c2 = s.pop(b)
while c1!=c2
if s.count>0
c1 = s.pop(c1)
c2 = s.pop(c2)
end
if c1== c2
return c1
elsif c1>c2
c1 = c1-c2
s.push(c2)
s.push(c1)
else
c2 = c2 -c1
s.push(c2)
s.push(c1)
end
end
return nil
end
However, I keep getting a Argument Error: wrong number of arguments (1 for 0)
from line 7

Stack#pop method probably takes no arguments, so it should be:
c1 = s.pop
c2 = s.pop

Related

A quicker way to use || in IF expressions? [duplicate]

This question already has answers here:
How do I count vowels?
(5 answers)
Test whether a variable equals either one of two values
(8 answers)
Closed 6 years ago.
I'm making a method in Ruby to count how many vowels are in a string. I figured it out, but at first I made the mistake of not repeating "variable==" after every '||'. The correct way seems inefficient. Is there a quicker way to do this and get the same result?
See example:
CORRECT:
def count_vowels(string)
vnum = 0
n = 0
while n < string.length
if string[n] == "a"||string[n] == "e"||string[n] =="i"||string[n] =="o"||string[n] =="u"||string[n] =="y"
vnum += 1
end
n += 1
end
return vnum
end
INCORRECT:
def count_vowels(string)
vnum = 0
n = 0
while n < string.length
if string[n] == "a"|| "e"||"i"||"o"||"u"||"y"
vnum += 1
end
n += 1
end
return vnum
end

syntax error, unexpected keyword_end end [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm getting into Ruby and I've searched and searched and looked at syntax, yet I can't figure out what's going wrong.
The program is supposed to solve a ProjectEuler problem.
I'm encountering four of this syntax error:
unexpected keyword_end end
This is my code:
grid = #20x20 "grid"/array of numbers
largest = 0
lateral(0,1,2,3)
vertical(0,20,40,60)
diagonal_right(0,21,42,63)
diagonal_left(3,22,41,60)
puts largest
#lateral
def lateral(a, b, c, d)
while (d < grid.size)
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
if ((d % 19) == 0)
a += 4
b += 4
c += 4
d += 4
else
a++
b++
c++
d++
end # <===== getting syntax error here
end
end
def vertical(a, b, c, d)
while (d < grid.size)
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
a++
b++
c++
d++
end # <===== getting syntax error here
end
def diagonal_right(a, b, c, d)
while (d < grid.size)
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
if ((d % 19) == 0)
a += 4
b += 4
c += 4
d += 4
else
a++
b++
c++
d++
end # <===== getting syntax error here
end
end
def diagonal_left(a, b, c, d)
while (d < (grid.size - 4))
temp = grid[a] * grid[b] * grid[c] * grid[d]
if (temp > largest)
largest = temp
end
if ((a % 19) == 0)
a += 4
b += 4
c += 4
d += 4
else
a++
b++
c++
d++
end # <===== getting syntax error here
end
end
I marked the four spots where I'm getting the syntax errors.
I've adjusted parentheses, played with and double-checked the ends positions and amount. I do not understand what's wrong with it. Could it be an interpreter problem? I'm using a MacBook Pro.
The ++ operator doesn't exist in Ruby. You should go for += 1.
You have very strange formatting. If you had formatted your code properly by following one of the many Ruby styleguides (or simply hitting Alt+Shift+F or whatever the combination is in your editor of choice), you would have immediately seen the problem. According to most style guides, there should be a space to both sides of a binary infix operator and no space after a unary prefix operator. I.e. you should write foo - bar and !baz. You have no space around your infix operators and a newline in between the unary prefix operator and its operand.
This is what it looks like correctly formatted:
a + +b + +c + +d + + # plus what?
Do you see the problem? You are missing the operand to the last unary prefix + operator.

Sorting a table by nested value in Lua [duplicate]

This question already has answers here:
Associatively sorting a table by value in Lua
(7 answers)
Closed 8 years ago.
I have a program which aggregates for every user the total number of downloads performed with an aggregate of the total downloaded data in kb.
local table = {}
table[userID] = {5, 23498502}
My aim is that the output of the printTable function will produce the entire list of users ordered in descending order by the amount of kb downloaded v[2]
local aUsers = {}
...
function topUsers(key, nDownloads, totalSize)
if aUsers[key] then
aUsers[key][1] = aUsers[key][1] + nDownloads
aUsers[key][2] = aUsers[key][2] + totalSize
else
aUsers[key] = {nDownloads, totalSize}
end
end
function printTable(t)
local str = ""
-- How to sort 't' so that it prints in v[2] descending order?
for k,v in pairs(t) do
str = str .. k .. ", " .. v[1] .. ", " .. v[2] .. "\n"
end
return str
end
...
Any ideas how could I do that?
You can get the keys into a separate table and then sort that table using the criteria you need:
local t = {
a = {1,2},
b = {2,3},
c = {4,1},
d = {9,9},
}
local keys = {}
for k in pairs(t) do table.insert(keys, k) end
table.sort(keys, function(a, b) return t[a][2] > t[b][2] end)
for _, k in ipairs(keys) do print(k, t[k][1], t[k][2]) end
will print:
d 9 9
b 2 3
a 1 2
c 4 1

Using stack to find the greatest common divisor

I am trying to implement an alogorithm to find the greatest common divisor using a stack: I am unable to formulate the correct answer based on my logic below. Please help. Here is my code:
def d8(a,b)
if (a==b)
return a
end
s = Stack.new
s.push(b)
s.push(a)
c1 = s.pop
c2 = s.pop
while c1!=c2
if s.count>0
c1 = s.pop
c2 = s.pop
end
if c1== c2
return c1
elsif c1>c2
c1 = c1-c2
s.push(c2)
s.push(c1)
else
c2 = c2 -c1
s.push(c2)
s.push(c1)
end
end
return nil
end
GCD cannot be nil. Two integers always have a GCD. So the logic in the function is already incorrect just because under some condition it has a return nil.
Looking at this return nil condition, it is happening when c1 == c2 (it will exit the while loop). At the same time, inside the while loop, you return a value if c1 == c2. These two cases are in logical contradiction. In other words, you are exiting the while loop on the c1 == c2 condition and treating that condition as invalid before your if c1 == c2 condition can trigger and treat the condition as valid and return the correct answer.
Simplifying the logic a little, you get:
def d8(a,b)
return a if a == b # Just a simpler way of doing a small if statement
s = Stack.new # "Stack" must be a gem, not std Ruby; "Array" will work here
s.push(b)
s.push(a)
#c1 = s.pop # These two statements aren't really needed because of the first
#c2 = s.pop # "if" condition in the while loop
while c1 != c2
if s.count > 0
c1 = s.pop
c2 = s.pop
end
# if c1 == c2 isn't needed because the `while` condition takes care of it
if c1 > c2
c1 = c1 - c2
else
c2 = c2 - c1
end
# These pushes are the same at the end of both if conditions, so they
# can be pulled out
s.push(c2)
s.push(c1)
end
return c1 # This return occurs when c1 == c2
end
This will work, but it becomes more obvious that the use of a stack is superfluous and serves no purpose at all in the algorithm. s.count > 0 will always be true, and you are popping variables off right after you push them (basically a no-op). So this is equivalent to:
def d8(a,b)
return a if a == b
c1 = a
c2 = b
while c1 != c2
if c1 > c2
c1 = c1 - c2
else
c2 = c2 - c1
end
end
return c1
end
Java code for it would be
public static int gcd (int p, int q) {
StackGeneric<Integer> stack = new StackGeneric<Integer>();
int temp;
stack.push(p);
stack.push(q);
while (true) {
q = stack.pop();
p = stack.pop();
if (q == 0) {
break;
}
temp = q;
q = p % q;
p = temp;
stack.push(p);
stack.push(q);
}
return p;
}
Replace the function call in recursive solution with the while loop and iterate it till the second argument becomes 0, as it happens with the recursive function call
public static int gcd (int p, int q) {
if (q == 0) {
return p;
}
return gcd(q, p % q);
}

Calculations on the iteration count in for loop

I was playing around with Ruby and Latex to create a color coding set for a registor. I have the following block of code. When attempting to run this, band1 = 1e+02.
I tried band1 = (BigDecimal(i) * 100).to_f, thinking maybe there was some odd floating point issue. An integer multiplied by an integer should create an integer. I tried a variety of other things as well, but to no avail.
(1..9).each do |i| #Band 1
(0..9).each do |j| #Band 2
(0..11).each do |k| #Band 3
#Band 3 Start
#these are the colors of the resistor bands
b1 = $c_band12[i]
b2 = $c_band12[j]
b3 = $c_band3[k]
b4 = "Gold"
oms = ((i*100) + (j*10)) * $mult[k]
band1 = i*100
band2 = j
band3 = $mult[k]
end
end
end
Not sure what I'm missing. Should I be using each_with_index through these iterations? I tried this:
(1..9).each_with_index {|i, indexi| #Band 1
(0..9).each_with_index {|j, indexj| #Band 2
(0..11).each_with_index {|k, indexk| #Band 3
#Band 3 Start
#these are the colors of the resistor bands
b1 = $c_band12[i]
b2 = $c_band12[j]
b3 = $c_band3[k]
b4 = "Gold"
oms = ((i*100) + (j*10)) * $mult[k]
band1 = indexk * 100
and I got the same answer. I can't see why 1*100 should equate to such a large number.
edit: Additional info: If I have this: band1=i*10
then the calculation is correct. In fact, the calculation is correct up to 99.
In your code, band1 has to be a Fixnum. Check with p band1.class. Not sure how you get "1e+02", maybe you print in some strange fashion, or you do band1 == 1e+02 which returns true in Ruby. You must use eql? to distinguish between 1 and 1.0:
1 == 1.0 # => true
1.eql?(1.0) # => false

Resources