Ruby file reading - ruby

I have a problem reading a file in ruby.
I am trying to read each line of a file, split it based on characters, and store that into an array. That array, which corresponds to each line, has information. I want to check if that array includes the characters "u" "d" "l" or "r" as you can see below.
IF that line doesn't include ANY of those characters, I increase a count variable by one.
The count -= 1 just takes into account a base case.
My problem is that this gives me a wrong count. For example with a text file that reads:
4 0 0 3 3
0 0 d 0.391538986557049
0 1 ur 63.1258159853081 3.14882640637611
0 2 rd 0.0148854629087619 0.019301544005463
0 3 u 15.6415340291405
count is supposed to be 0.
def compute_closed(file)
count = 0
while line = file.gets do
array = line.split(//)
answer = array.include?("u" || "d" || "l" || "r")
if answer != true
count += 1
end
end
count -= 1
puts count
end

Maybe you have a reason for splitting the line up into bits, but if you're just checking for those characters somewhere in the line, why not use a regex?
def compute_closed(file)
count = 0
while line = file.gets do
count += 1 if line =~ /[udlr]/
end
count -= 1
puts count
end

If you absolutely need to split the lines up by character, then you may want to use sets instead of arrays:
def compute_closed(file)
count = 0
while line = file.gets do
cmp_set = Set.new ['u', 'd', 'l', 'r']
input_set = Set.new(line.split(//))
if input_set.intersection(cmp_set).size == 0
count += 1
end
end
count -= 1
puts count
end

Edit: after posting I see my answer is essentially the same as #Philip's, but I'll leave it up for the slightly different treatment.
There are many ways to do this. Here's another:
File.read('f1').each_line.reduce(0) {|t,s| t + (s =~ /[udlr]/ ? 0 : 1)} - 1
Let's try it:
text =<<_
Now is the time
for all good
Rubiests to
spend some
time in Hawaii.
_
File.write('f1', text)
File.read('f1').each_line.reduce(0) {|t,s| t+(s =~ /[udlr]/ ? 0 : 1)} - 1 #=> 1
It returns 2 - 1 => 1 because if finds a 'u,' 'd', 'l' or 'r' in all but the the first and last rows.
Initially, I had
File.read(fname).each_line.each_with_object(0) {|s,t|
t += 1 unless s =~ /[udlr]/ } - 1
but t would not increment. I was puzzled, so emailed my friend #ArupRakshit, who I can always count on to know the answer, or dig until he finds it. It turns out that in
...each_with_object(memo) {|s,memo|
memo must be a mutable object, an important difference between that method and reduce/inject (which is not made clear in the Ruby docs for Enumerable#each_with_object). Thanks, Arup.

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

Ruby while loop only executes once

This code is not working. I'm trying to code the Collatz conjecture but the code only seems to run once for the input 8. It prints out 4, 1 ,1 and so that shows it only runs for one step. The else block is never executed either. Can someone tell me what's wrong with this Ruby code? I have no idea why it's not working the way it's supposed to.
class Collatz
def collatz(number)
if number == 1
return 0
end
steps = 0
while number > 1 do
if number % 2 == 0
number = number / 2
puts number
steps = steps + 1
puts steps
else
number = (number * 3) + 1
puts number
steps = steps + 1
puts steps
end
return steps
end
end
steps = Collatz.new.collatz(8)
puts steps
end
You have a return statement that's terminating execution after the first iteration of the while loop.
Try
class Collatz
def collatz(number)
return 0 if number == 1
steps = 0
while number > 1 do
if number % 2 == 0
puts number /= 2
puts steps += 1
else
number = (number * 3) + 1
puts number
puts steps += 1
end
end
return steps
end
end
steps = Collatz.new.collatz(8)
puts steps
which returns 3 and prints
4
1
2
2
1
3
[Finished in 0.4s]
And if you want to make your code a little cleaner and more idiomatic, you could refactor it as follows:
class Collatz
def collatz(number)
return 0 if number == 1
steps = 0
while number > 1
number = number.even? ? number / 2 : (number * 3) + 1
puts number, steps += 1
end
steps
end
end
steps = Collatz.new.collatz(8)
#4
#1
#2
#2
#1
#3
#=> 3
Let's properly indent your code and see if we can find the problem:
class Collatz
def collatz(number)
if number == 1
return 0
end
steps = 0
while number > 1 do
if number % 2 == 0
number = number / 2
puts number
steps = steps + 1
puts steps
else
number = (number * 3) + 1
puts number
steps = steps + 1
puts steps
end
return steps
end
end
steps = Collatz.new.collatz(8)
puts steps
end
Look at that — the return is in your while-loop, so it returns at the end of the first iteration. It looks like you actually wanted to end your while loop on the line above that.
This one of the reasons why coding style is really important. It can trick you into thinking your code means something it doesn't.

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.".

Resources