Hash Reverse ?

S

salai

I am preparing for my orals exam in Ruby (my last chance.. , If I fail
the test, no more possible to study Computer Science related :) )

http://codepad.org/foNb9Pd3

class Hash
def hash_revert
hash_new = Hash.new
self.each {|key,value|
if not hash_new.has_key?(key) then hash_new[value] = key end
}
return hash_new
end
end

h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}

or Should I get this ??
# => {"a"=>2, "b"=>1, "c"=>6}

Because HASH is working like "Set" ??

many thanks in advance,
salai.
 
A

Aldric Giacomoni

Salai said:
I am preparing for my orals exam in Ruby (my last chance.. , If I fail
the test, no more possible to study Computer Science related :) )

http://codepad.org/foNb9Pd3

class Hash
def hash_revert
hash_new = Hash.new
self.each {|key,value|
if not hash_new.has_key?(key) then hash_new[value] = key end
}
return hash_new
end
end

h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}

or Should I get this ??
# => {"a"=>2, "b"=>1, "c"=>6}

Because HASH is working like "Set" ??

many thanks in advance,
salai.

In Ruby 1.9, hashes keep the order of input. Before Ruby 1.9, the order
is "random" - so it's not guaranteed which will be the first key that
you will see with 'each'.
 
R

Robert Klemme

2009/10/26 Aldric Giacomoni said:
Salai said:
I am preparing for my orals exam in Ruby (my last chance.. , If I fail
the test, no more possible to study Computer Science related :) )

http://codepad.org/foNb9Pd3

class Hash
=A0 def hash_revert
=A0 =A0 hash_new =3D Hash.new
=A0 =A0 self.each {|key,value|
=A0 =A0 =A0 if not hash_new.has_key?(key) then hash_new[value] =3D key e= nd
=A0 =A0 }
=A0 =A0 return hash_new
=A0 end
end

h =3D {2=3D>"a", 1=3D> "b", 3 =3D>"a", 4=3D> "a", 5 =3D> "b", 6=3D>"c"}
p h.hash_revert #--> {"a"=3D>4, "b"=3D>1, "c"=3D>6}

or Should I get this ??
# =3D> {"a"=3D>2, "b"=3D>1, "c"=3D>6}

Because HASH is working like "Set" ??

many thanks in advance,
salai.

In Ruby 1.9, hashes keep the order of input. Before Ruby 1.9, the order
is "random" - so it's not guaranteed which will be the first key that
you will see with 'each'.

As far as I can see salai does not want to reverse the order of
elements but rather the key value relationship.

Salai, it really depends on the requirements. Here's another valid variant=
:

class Hash
def hash_revert
r =3D Hash.new {|h,k| h[k] =3D []}
each {|k,v| r[v] << k}
r
end
end

irb(main):008:0> {2=3D>"a", 1=3D> "b", 3 =3D>"a", 4=3D> "a", 5 =3D> "b",
6=3D>"c"}.hash_revert
=3D> {"a"=3D>[2, 3, 4], "b"=3D>[1, 5], "c"=3D>[6]}

http://codepad.org/XNvwvOxi

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
G

Gregory Brown

h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}

or Should I get this ??
# => {"a"=>2, "b"=>1, "c"=>6}

Because HASH is working like "Set" ??

See Hash#invert and perhaps its implementation.

-greg
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Good Morning,

I am preparing for my orals exam in Ruby (my last chance.. , If I fail
the test, no more possible to study Computer Science related :) )

http://codepad.org/foNb9Pd3

class Hash
def hash_revert
hash_new = Hash.new
self.each {|key,value|
if not hash_new.has_key?(key) then hash_new[value] =
key end
}
return hash_new
end
end

h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}

or Should I get this ??
# => {"a"=>2, "b"=>1, "c"=>6}
The answer can be different (and most likely will be) under 1.8 and 1.9 - in
1.8 order is not guaranteed - but in 1.9 it is (order of insertion into the
hash) and therefore under 1.9 you will get the second option and apparantly
under 1.8 you get your first.

John
 
G

Gregory Brown

I recommend pastie.org , actually.

But the two are not the same thing. Pastie is a nice pastebin
service, for sure (I typically use it myself when I don't need the
features gist has to offer)
But codepad.org actually executes the code pasted in. That is what is
neat about it.

-greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top