Forum: Ruby Subtraction, union and intersection of arrays in Ruby

8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2012-09-06 19:39
Hi All.

In the exercise I'm trying to solve I have 2 arrays of names and what I
have to do in a subtraction, a union and an intersection of arrays, the
issue is that I have to use basic functions as a for loop such as this
is as indicated by the exercise, and while I understand the logic of
what to do, I can not think how to implement it correctly.

Exercise such functions done with Ruby or unused cycles would look like:

#######################################################################

op_sys = ["Fedora", "SuSE", "Tuquito", "Windows", "MacOS"]
linux_sys = ["Tuquito", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]

sub = (op_sys) - (linux_sys)
int = (op_sys) & (linux_sys)
uni = (op_sys) | (linux_sys)

puts "Subtraction: # {rest}"
puts "intersection: # {int}"
puts "Union: # {uni}"

#########################################################################

Now if I try to do with basic functions, such as subtraction of arrays:

################################################################

op_sys = ["Fedora", "SuSE", "Tuquito", "Windows", "MacOS"]
linux_sys = ["Tuquito", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]

for i in 0 .. 4
     op_sys if [i]! = linux_sys [i]
            subtraction = [i] = op_sys [i]
     end
end

puts "Subtraction: # {subtraction}"

#################################################################


Here is a little tricky, and not calculated well, I've been trying many
combinations, but do not know how to calculate the difference of arrays
using basic functions properly.

I accept any suggestions. Thank you.
F5a540b04b1f6430efe51d9f3361ef17?d=identicon&s=25 Jan E. (jacques1)
on 2012-09-06 20:11
Hi,

So you're not even allowed to use basic methods like Array#include? In
this case you'll need a nested iterator to check for every element if
it's in the subtracted array. However, I refuse to use a "for" loop,
because it's merely the evil twin of the "each" method and is in no way
more "basic". Ruby isn't C.

So the code would look something like this:

result = []
op_sys.each do |op|
  add_element = true
  linux_sys.each do |linux|
    if op == linux
      add_element = false
      break
    end
  end
  result << op if add_element
end

Your own solution I don't understand. It seems you have some
difficulties with the programming logic and Ruby syntax.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2012-09-06 21:26
Joao Silva wrote in post #1074949:
> Here is a little tricky, and not calculated well

it doesn't even *parse*:

$ ruby ert.rb
ert.rb:5: syntax error, unexpected '!', expecting kEND
     op_sys if [i]! = linux_sys [i]
                   ^
ert.rb:6: syntax error, unexpected '=', expecting kEND
            subtraction = [i] = op_sys [i]
                               ^
ert.rb:8: syntax error, unexpected kEND, expecting $end

First make your program syntactically valid. Then you can see if it
works or not.

Note: the error message "unexpected kEND, expecting $end" is not a very
good message. What it should say is "unexpected 'end', was expecting
end-of-file"

That is, there is one too many 'ends' in your program.

Hint: a statement like

  foo if bar == baz

is a single complete statement. It does *not* want a matching 'end'
statement. It means "if bar == baz, do foo"

But if you write it the other way:

  if bar == baz
    foo
  end

this means the same, but *does* have a matching 'end' statement.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.