Problems with Modulo operator Ruby: Why am I getting "undefined method `%' for 1..100:Range"? - ruby

For some reason I'm getting the error undefined method '%' for 1..100:Range when I run the following code:
[1..100].each do |x|
if x % 3 == 0 && x % 5 == 0
puts "CracklePop"
elsif x % 3 == 0
puts "Crackle"
elsif x % 5 == 0
puts "Pop"
else
puts x
end
end
Any idea what's going on? Any help is much appreciated.

That's the wrong syntax for ranges.
You've made an array with 1 element, and that element is itself the range 1..100. What you've written is equivalent to [(1.100)]. You're iterating over the outer array one time, and setting x to (1..100)
You want (1..100).each, which invokes each on the range, not on an array containing the range.

By doing [1..100] you are not looping from 1 to 100 but on 1..100, which is a Range object, what you really want to do is:-
(1..100).step do |x|
if x % 3 == 0 && x % 5 == 0
puts "CracklePop"
elsif x % 3 == 0
puts "Crackle"
elsif x % 5 == 0
puts "Pop"
else
puts x
end
end
Basically, Range represents an interval, you can iterate over Range as explained here, create an array from Range as explained here and more details on range can be found here.

Just as it says. 1..100 does not have a method %. The expression (1..100) % 3 is undefined.

Related

Ruby while and if loop issue

x = 16
while x != 1 do
if x % 2 == 0
x = x / 2
print "#{x} "
end
break if x < 0
end
Hi, the result I get from above is 8 4 2 1 . Is there any way to remove the space at the end?
One of Rubys main features is its beauty - you can shorten that loop to a nice one liner when using an array:
x = 16
arr = []
arr.push(x /= 2) while x.even?
puts arr.join(' ')
# => "8 4 2 1"
* As sagarpandya82 suggested x.even? is the same as using x % 2 == 0, leading to even more readable code
Don't print the values into the loop. Put them into a list (array) then, after the loop, join the array items using space as glue.
x = 16
a = []
while x != 1 do
if x % 2 == 0
x = x / 2
a << x
end
break if x < 0
end
puts '<' + a.join(' ') + '>'
The output is:
<8 4 2 1>
As #Bathsheba notes in a comment, this solution uses extra memory (the array) to store the values and also the call to Array#join generates a string that doubles the memory requirements. This is not an issue for small lists as the one in the question but needs to be considered the list becomes very large.
loop.reduce([[], 16]) do |(acc, val), _|
break acc if val <= 1
acc << val / 2 if val.even?
[acc, val / 2]
end.join ' '
if x != 0
print " "
end
is one way, having dropped the suffixed space from the other print. I/O will always be the performance bottleneck; an extra if will have a negligible effect on performance, and the extra print will merely contribute to the output stream which is normally buffered.

Substitute a string for multiple of 3

I'm printing the integers 1-100. However I want to substitute the integers that are multiples of 3 with the string "fizz".
My current code is
if num % 3 == 0
num.sub(/num/, "fizz");
end
It raises "undefined method 'sub'". It is the same when I try gsub. Am I missing something?
sub is for strings, not integers.
Try this :
if num % 3 == 0
"#{num}".sub(/#{num}/, "fizz");
end
You should be able to just puts "fizz". So:
if num % 3 == 0
puts "fizz"
end
Or a more complete example:
(1..100).each do |num|
if num % 3 == 0
puts "Fizz"
end
end
sub & gsub are for strings only and don't work with integers, hence your error.

Why Can't This Code Find Powers? (Ruby)

App Academy's practice test says their chosen way of finding if an input is a power of 2 is to keep dividing it by 2 on a loop and check whether the end result is 1 or 0 (after having tested for the numbers 1 and 0 as inputs), which makes sense, but why won't this way work?
def try
gets(num)
counter = 0
go = 2 ** counter
if num % go == 0
return true
else
counter = counter + 1
end
return false
end
I can't figure out why this won't work, unless the counter isn't working.
There are a number of problems with your code.
First of all, there is no loop and your counter will reset to zero each time if you intend to use the method in a loop, because of counter = 0.
counter = 0; go = 2 ** counter basically means go = 2 ** 0 which is 1. Therefore num % 1 will always be 0
You actually need to divide the number and change it in the process. 12 % 4 will return 0 but you don't know by that if 12 is a power of 2.
IO#gets returns a string and takes a separator as an argument, so you need to use num = gets.to_i to actually get a number in the variable num. You are giving num to gets as an argument, this does not do what you want.
Try:
# Check if num is a power of 2
#
# #param num [Integer] number to check
# #return [Boolean] true if power of 2, false otherwise
def power_of_2(num)
while num > 1 # runs as long as num is larger than 1
return false if (num % 2) == 1 # if number is odd it's not a power of 2
num /= 2 # divides num by 2 on each run
end
true # if num reached 1 without returning false, it's a power of 2
end
I add some checks for your code. Note, that gets(num) returns a String. Your code is fine, but not for Ruby. Ruby hates type-cross-transform like Perl does.
def try(num = 0)
# here we assure that num is number
unless (num.is_a?(Integer))
puts "oh!"
return false
end
counter = 0
go = 2 ** counter
if num % go == 0
return true
else
counter = counter + 1
end
return false
end
The general problem is "how string could use '%' operator with number?"
Try some code in the interpretator (irb):
"5" % 2
or
"5" % 0

How do I iterate in Ruby?

What's wrong with this Ruby code? I'm trying to solve the first Project Euler question.
I think the problem is in the syntax of sum += num, but I can't figure out what the proper syntax for this would be.
sum = 0
num = 0
num2 = 0
loop do
num += 1
if num % 3 == 0
sum += num
break if num > 1000
end
end
loop do
num2 += 1
if num2 % 5 == 0
sum += num2
break if num2 > 1000
end
end
puts sum
Here's an alternative:
(1...1000).select { |x| x % 3 == 0 || x % 5 == 0 }.reduce(:+)
You are making this way more complicated than it needs to be. Also, if the number is a multiple of 3 and 5, it gets added twice. Try something like this:
sum = 0 # initialize the sum
(1...1000).each { |x| # loop from 1 to 1000
sum += x if x % 3 == 0 || x % 5 == 0 # add the number to the sum if it is
# divisible by 3 or 5
}
puts sum # output the sum
This runs, your syntax is okay, but does not give the right answer because, as mentioned, you add multiples of both 3 and 5 twice, once in the first loop, with num, and the second loop, with num2.
So you have two loops, but you actually only need one.
You only need to consider each number once, you can check it to see if it is a multiple of either 3 or 5. This will solve your double-counting issue and also make your code more concise.
Also, like Doorknob shows, the each syntax would save you some lines on those loops. You could also use the for syntax:
for num in (1..1000)
<stuff here>
end
Check out the kinds of loops in "Loops: How to do thousands of operations with a few lines of code.".

Ruby: 'case true' when does it block loop?

There is piece of code
A = "am"
F = "fm"
def fmam(n)
return if n == 0
loopy(n - 1)
case true
when n % 15 == 0
puts B + L
when n % 5 == 0
puts L
when n % 3 == 0
puts B
else
puts n
end
end
fmam(20)
in this code what does case true do it this code?
case has two forms. The form you're using compares the "target" after the case keyword ( true in this case) with each comparison (the part after each when keyword) using the === operator. You end up with a series of boolean expressions and execute the code for the first one that evaluates to true. As such, it's redundant and a bit confusing. It would be better to remove the true and use the second form of case:
case
when n % 15 == 0
puts B + L
when n % 5 == 0
puts L
when n % 3 == 0
puts B
else
puts n
end
This does the same thing but is clearer.
tutorialspoint :- says
case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end
Compares the expression specified by case and that specified by when using the === operator and executes the code of the when clause that matches.
saying that look below:
A = "am"
F = "fm"
L = "dd"
B = 'aa'
def fmam(n)
return if n == 0
case true
when n % 15 == 0
puts B + L
when n % 5 == 0 # this evaluates to true first, which then matched with true value mentioned in the case statement.
puts L
when n % 3 == 0
puts B
else
puts n
end
end
fmam(20) #=> dd
Now look at the below code:
A = "am"
F = "fm"
L = "dd"
B = 'aa'
def fmam(n)
return if n == 0
case false
when n % 25 == 0 # this evaluates to false first, which then matched with false value mentioned in the case statement.
puts B + L
when n % 5 == 0
puts L
when n % 3 == 0
puts B
else
puts n
end
end
fmam(30) #=> aadd
You could refactor the case statement to
msg = case 0
when n % 15
B + L
when n % 5
L
when n % 3
B
else
n
end
puts msg

Resources