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.
Related
I'm currently trying to implement a mathematic method to approximate
f(x) = 0. I've already implemented it in some languages and I want to do it in ruby now just for training.
But I have this error that I really does'nt understand
Here is my code
def fonction (x)
return (x ** 3) + 4 * (x ** 2) - 10
end
def derive (x)
return 3 * (x ** 2) + 8 * x
end
def newton(f, fPrime, n, u0)
if n == 0 then
return u0
else
uN = newton (f, fPrime, (n - 1), u0)
return uN - f(uN) / fPrime(uN)
end
end
for i in 0..6
puts (newton (fonction, derive, i, 2))
end
i think there is space on newton method call
uN = newton (f, fPrime, (n - 1), u0) # there is space after newton
also in this one
for i in 0..6
puts (newton (fonction, derive, i, 2)) # there is space after newton
end
try remove it, and you will see another error i guess, i try it on repl
SOS i keep getting errors in the loop solving by finite difference method.
I either get the following error when i start with i = 2 : N :
diffusion: A(I,J): row index out of bounds; value 2 out of bound 1
error: called from
diffusion at line 37 column 10 % note line change due to edit!
or, I get the following error when i do i = 2 : N :
subscript indices must be either positive integers less than 2^31 or logicals
error: called from
diffusion at line 37 column 10 % note line change due to edit!
Please help
clear all; close all;
% mesh in space
dx = 0.1;
x = 0 : dx : 1;
% mesh in time
dt = 1 / 50;
t0 = 0;
tf = 10;
t = t0 : dt : tf;
% diffusivity
D = 0.5;
% number of nodes
N = 11;
% number of iterations
M = 10;
% initial conditions
if x <= .5 && x >= 0 % note, in octave, you don't need parentheses around the test expression
u0 = x;
elseif
u0 = 1-x;
endif
u = u0;
alpha = D * dt / (dx^2);
for j = 1 : M
for i = 1 : N
u(i, j+1) = u(i, j ) ...
+ alpha ...
* ( u(i-1, j) ...
+ u(i+1, j) ...
- 2 ...
* u(i, j) ...
) ;
end
u(N+1, j+1) = u(N+1, j) ...
+ alpha ...
* ( ...
u(N, j) ...
- 2 ...
* u(N+1, j) ...
+ u(N, j) ...
) ;
% boundary conditions
u(0, :) = u0;
u(1, :) = u1;
u1 = u0;
u0 = 0;
end
% exact solution with 14 terms
%k=14 % COMMENTED OUT
v = (4 / ((k * pi) .^ 2)) ...
* sin( (k * pi) / 2 ) ...
* sin( k * pi * x ) ...
* exp .^ (D * ((k * pi) ^ 2) * t) ;
exact = symsum( v, k, 1, 14 );
error = exact - u;
% plot stuff
plot( t, error );
xlabel( 'time' );
ylabel( 'error' );
legend( 't = 1 / 50' );
Have a look at the edited code I cleaned up for you above and study it.
Don't underestimate the importance of clean, readable code when hunting for bugs.
It will save you more time than it will cost. Especially a week from now when you will need to revisit this code and you will not remember at all what you were trying to do.
Now regarding your errors. (all line references are with respect to the cleaned up code above)
Scenario 1:
In line 29 you initialise u as a single value.
If you start your loop in line 35 starting with i = 2, then as soon as you try to do u(i, j+1), i.e. u(2,2) in the next line, octave will complain that you're trying to index the second row, in an array that so far only contains one row. (in fact, the same will apply for j at this point, since at this point you only have one column as well)
Scenario 2:
I assume the second scenario was a typo and you meant to say i = 1 : N.
If you start with i=1 in the loop, then have a look at line 38: you are trying to get element u(i-1, j), i.e. u(0,1). Therefore octave will complain that you're trying to get the zero element, but in octave arrays start from one and zero is not defined. Attempting to access any array with a zero will result in the error you see (try it in a terminal!).
UPDATE
Also, now that the code is clean, you can spot another bug, which octave helpfully warns you about if you try to run the code.
Look at line 26. There is NO condition in the elseif leg, so octave looks for the next statement as the test condition.
This means that the elseif condition will always succeed as long as the result of u0 = 1-x is non-zero.
This is clearly a bug. Either you forgot to put the condition for the elseif, or more likely, you probably just meant to say else, rather than elseif.
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
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hello i am trying to solve this equation for a programming problem that states that you need to do a complete search algorithm to find this results.
However an O(N^4) algorithm takes a lot of time, since the range for each value of A,B,C, and D, is (0,2000] . so we can say A<=B<=C<=D
i want to make my algorithm faster translating it to a O(n^3) solution. For doing that i am taking into account certain things with A,B and C to make the algorithm runs a little faster (prunning). But the main issue is to take out the search of D, i have read some solutions for a similar problem and the way they find D derivating it from A+B+C=A*B*C is really confusing, can somebody explain to me the O(N^3) solution to this problem? thanks a lot!
The equation
A * B * C * D == A + B + C + D
has just one solution
1 1 2 4
So time complexity is O(1).
Since A <= B <= C <= D,
A + B + C + D <= 4 * D
hence
A * B * C * D <= 4 * D
and
A * B * C <= 4
Therefore, it is enough to check just a few combinations:
for(int a = 1; a <= 4; a++)
for(int b = a; b <= 4; b++)
for(int c = b; c <= 4; c++)
{
// a*b*c*d == a+b+c+d
// => d == (a+b+c) / (a*b*c - 1)
if(a * b * c - 1 != 0 && (a + b + c) % (a * b * c - 1) == 0)
{
int d = (a + b + c) / (a * b * c - 1);
if (d >= c)
Console.WriteLine("{0} {1} {2} {3}",
a, b, c, (a + b + c) / (a * b * c - 1));
}
}
Equivalently d = (a+b+c)/(abc-1). Now just walk over all values of a, b, and c, seeing which ones return an integer value for d.
Furthermore a+b+c < abc-1 when a,b,c >= 2. Should shorten your search time quite a bit...
I'm getting a strange error: "syntax error, unexpected $end, expecting kEND" and it points to the final line of my code.
New to Ruby and not sure what I'm doing wrong here. Any help would be fantastic. Thanks!
def add(x,y)
if(x > y)
c = x + y
return c
else
puts "Y is too big"
return
end
a = 4
b = 6
add(a,b)
BTW, you can refactor your if..end statement out completely if you prefer
def add(x,y)
return (x + y) if(x > y)
puts "Y is too big"
end
Corrected code (you are missing one end for the if-else):
def add(x,y)
if(x > y)
c = x + y
return c
else
puts "Y is too big"
return
end
end
a = 4
b = 6
add(a,b)
Both if statements and function definitions require an end statement to terminate them.
Try adding another end after your existing end and your problem should go away.
wap to input principle, rate, time and choice. if choice is 1 than calculate simple interest, if choice is 2 calculate compund interest and if it is other than 1 or 2 print "Enter valid choice"