Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Hash Reverse ? (http://www.velocityreviews.com/forums/t860156-hash-reverse.html)

salai 10-26-2009 12:59 PM

Hash Reverse ?
 
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.


Aldric Giacomoni 10-26-2009 02:09 PM

Re: Hash Reverse ?
 
Salai Koko wrote:
> 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'.
--
Posted via http://www.ruby-forum.com/.


Robert Klemme 10-26-2009 02:15 PM

Re: Hash Reverse ?
 
2009/10/26 Aldric Giacomoni <aldric@trevoke.net>:
> Salai Koko wrote:
>> 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/


Gregory Brown 10-26-2009 04:16 PM

Re: Hash Reverse ?
 
On Mon, Oct 26, 2009 at 8:59 AM, salai <sayakyi@gmail.com> wrote:

> 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


Rajinder Yadav 10-26-2009 05:52 PM

Re: Hash Reverse ?
 
On Mon, Oct 26, 2009 at 8:59 AM, salai <sayakyi@gmail.com> wrote:
> 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


codepad.org idea is wicked, new bookmark for me =)


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.


Aldric Giacomoni 10-26-2009 06:34 PM

Re: Hash Reverse ?
 
Rajinder Yadav wrote:
> On Mon, Oct 26, 2009 at 8:59 AM, salai <sayakyi@gmail.com> wrote:
>> 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

>
> codepad.org idea is wicked, new bookmark for me =)
>

I recommend pastie.org , actually.
--
Posted via http://www.ruby-forum.com/.


John W Higgins 10-26-2009 07:15 PM

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

Good Morning,

On Mon, Oct 26, 2009 at 5:59 AM, salai <sayakyi@gmail.com> wrote:

> 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


Gregory Brown 10-26-2009 07:57 PM

Re: Hash Reverse ?
 
On Mon, Oct 26, 2009 at 2:34 PM, Aldric Giacomoni <aldric@trevoke.net> wrote:
> Rajinder Yadav wrote:
>> On Mon, Oct 26, 2009 at 8:59 AM, salai <sayakyi@gmail.com> wrote:
>>> 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

>>
>> codepad.org idea is wicked, new bookmark for me =)
>>

> 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


salai 10-27-2009 05:23 PM

Re: Hash Reverse ?
 
Dear All,

Thank you very much to you all, I passed the exam.

regards,
Salai

On Mon, Oct 26, 2009 at 8:57 PM, Gregory Brown
<gregory.t.brown@gmail.com> wrote:
> On Mon, Oct 26, 2009 at 2:34 PM, Aldric Giacomoni <aldric@trevoke.net> wr=

ote:
>> Rajinder Yadav wrote:
>>> On Mon, Oct 26, 2009 at 8:59 AM, salai <sayakyi@gmail.com> wrote:
>>>> 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
>>>
>>> codepad.org idea is wicked, new bookmark for me =3D)
>>>

>> I recommend pastie.org , actually.

>
> But the two are not the same thing. =A0Pastie 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. =A0That is what is
> neat about it.
>
> -greg
>
>




All times are GMT. The time now is 06:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.