syntax error, unexpected keyword_else [closed] - ruby

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 8 years ago.
Improve this question
Who can tell me how to solve the error as title?
How to use a nested if-else sentence?
Thank U.
b = Array.new(n,0)
c = Array.new(n) {Array.new(n,0)}
n.times do |i|
a[i], b[i] = gets.split(" ").map(&:to_i)
end
n.times do |i|
w.times do |j|
i=i+1
j=j+1
if a[i-1] > j || i > k then
c[i][j] = c[i-1][j]
else
if c[i-1][j] < c[i-1][j-a[i-1]]+b[i-1] then
c[i][j] = c[i-1][j-a[i-1]]+b[i-1]
k--
else
c[i][j] = c[i-1][j]
end
end
end
end

There is no such thing as the increment/decrement operator in Ruby.
Replace k-- with k -= 1.
You could also use compound operators for these:
i += 1 # previously i=i+1
j += 1 # previously j=j+1

Related

Division with if condition [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 3 years ago.
Improve this question
I just start learning ruby with an online course and in my very first exercise, I can't complete the challenge. I have to create functions to sum, subtract, multiply and divide that meet the specs conditions.
For the division, I need to check if it will divide per 0 or not, give the result or a warning. My code is
def divisao(primeiro_numero, segundo_numero)
if segundo_numero > 0
primeiro_numero / segundo_numero
else
puts 'Opa! Zero como divisor'
end
But when I run the specs, I get the following warning
0 examples, 0 failures, 1 error occurred outside of examples
I have changed all the functions, but this one doesn't seem to work.
I added the complete functions and specs file here:https://gist.github.com/isafloriano/86c170400b2f5fc63dc5e8edd8913525
Can anyone give me a clue why this doesn't work?
You are just missing the end of the if-else block:
def divisao(primeiro_numero, segundo_numero)
if segundo_numero > 0
primeiro_numero / segundo_numero
else
puts 'Opa! Zero como divisor'
end # <= this one was missing
end
Some suggestion:
Use English method and variable names. That makes it easier for others to understand your code and to help you.
Try to follow common Ruby conventions right from the start. Ruby code is indented with two spaces.
Here's a compact way to write your four methods.
def add(n1, n2)
f(n1, n2, :+)
end
def subtract(n1, n2)
f(n1, n2, :-)
end
def multiply(n1, n2)
f(n1, n2, :*)
end
def divide(n1, n2)
if n2.zero?
puts "Zero-divide error"
return nil
end
f(n1, n2, :/)
end
def f(n1, n2, op)
n1.public_send(op, n2)
end
add(3, 7) #=> 10
add(5, 7.2) #=> 12.2
subtract(4, 8) #=> -4
subtract(6.1, 3) #=> 3.0999999999999996
multiply(2, 6) #=> 12
multiply(1.0, 6) #=> 6.0
divide(4, 0) #=> nil
Zero-divide error
divide(4, 0.0) #=> nil
Zero-divide error
divide(4, 2) #=> 2
divide(5, 2) #=> 2
divide(5, 2.0) #=> 2.5
See Object#public_send.

Undefined method for 'n' in ruby while loop [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 am trying to write an algorithm to solve the math problem Σ n = 1 to 49 of n(n+1). I keep getting an error "undefined method 'n' for main object"
def solver(n)
sum = 0
while n < 49
temp = n(n+1)
n+=1
sum = sum + temp
end
return sum
end
puts solver(1)
Instead of:
temp = n(n+1)
put:
temp = n*(n+1)
The reason you're getting undefined method 'n' for main object is because your code thinks that n(n+1) is calling n() method on the main object. In ruby main refers to the top level object which is an instance of Object
In any case, the solution to your issue is actually multiplying n * (n + 1) rather than calling a method n with argument n + 1
This is because of missing *. Try this solution:
def solver(n)
sum = 0
while n < 49
temp = n*(n+1)
n+=1
sum = sum + temp
end
return sum
end
puts solver(1)

Ruby diagonal matrix from random numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a diagonal matrix (8x8) and fill it with random numbers in [-30..45] range
After I need to transform array into vector and sort it
I tried different ways.
At first I create matrix (8x8)
matrix = Matrix.build(8) {0}
puts matrix.each_slice(matrix.column_size) {|r| p r }
Also I made an array with random numbers in range
array = (-30..45).to_a.shuffle
puts array [0..8]
And made a diagonal matrix
matrixd = Matrix.diagonal(1,2,3,4,5,6,7,8)
puts matrixd.each_slice(matrix.column_size) {|r| p r}
How can i fill diagonal matrix with my array and put it in vector?
I have 2 algorithms for sorting vector
class Array
def bubblesort!
length.times do |j|
for i in 1...(length - j)
if self[i] < self[i - 1]
self[i], self[i - 1] = self[i - 1], self[i]
end
end
end
self
end
end
class Array
def selectionsort!
0.upto(length - 2) do |i|
(min_idx = i + 1).upto(length - 1) do |j|
if self[j] < self[min_idx]
min_idx = j
end
end
if self[i] > self[min_idx]
self[i], self[min_idx] = self[min_idx], self[i]
end
end
self
end
end
For the diagonal matrix:
elems = [*-30..45].sample(8)
Matrix.diagonal(*elems)
For the rest, I'm not sure what you're trying to accomplish

Why doesn't this algorithm yield the fibonacci sequence under 100? [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 9 years ago.
Improve this question
a = 1; b = 2
fibonacci = []
while fibonacci.length < 100
fibonacci.push(a)
fibonacci.push(b)
a = a + b; b = a + b
end
push fibonacci
The error message is "undefined method `push' for main:Obj"
You're trying to #push the array itself on the last line! :)
That's what it's complaining about. The push method is being invoked on the 'main' object, and push is not a Kernel method.
I'm guessing you mean puts. Otherwise it looks okay, if somewhat non-idiomatic. Naturally you can find lots of Ruby solutions for this problem on the site that might read a bit more clearly (see here for a recursive one.)
As others have said before the last line should be 'puts'
Also your numbers are wrong.
a = 1; b = 1
fibonacci = []
while fibonacci.length < 100
fibonacci << a
fibonacci << b
a += b
b += a
end
puts fibonacci
But also the fib starts at 1 and the the second element is also 1.
This make you sequence off, if you start at 1, 2
Fib = 1, 1, 2, 3, 5, 8, ...

Ruby puts not putting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am answering the following question:
Q3: Write a method, is_prime?, that takes a number num and returns
true if it is prime and false otherwise.
You may wish to use the modulo operation: 5 % 2 returns the remainder
when dividing 5 by 2: 1. If num is divisible by i, then num % i == 0.
(You would not be expected to already know about modulo for the
challenge)
but I get no answer back. My final "puts" doesn't seem to be working. Here is my code:
# Q3 method is_prime?
def is_prime? (num)
i = 2
while i <= num
is_divisble = ((num%i) == 0)
if is_divisble
return false
end
i+=1
end
# is not divisible
return true
end
puts 'select a number greater than one and see if it\'s prime'
num = gets.chomp.to_i
puts is_prime?(num)
The code prints fine. However, it always prints true. Remedy: change while i <= num
to while i < num .
i <= num
It is your mistake :).
Your code will increment i until i == num so num%i will always be true at the end.
Just replace it with
i < num

Resources