13 Replies - 2233 Views - Last Post: 19 January 2014 - 11:45 AM Rate Topic: -----

#1 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1372
  • View blog
  • Posts: 3,469
  • Joined: 28-November 09

[Challenge][Med-Hard] Hash Traversal

Post icon  Posted 18 January 2014 - 05:58 PM

Let's have some fun with hashes! This one has an medium path, and a hard one. Make sure to tag your solutions with which one you're solving. This was an interesting problem I had to solve about a week ago, and I'll post my solution in a few days.

On the note of challenges, I'll have a new one up every week. Make sure to upvote solutions you like!

Medium:

Given a hash:
{a: 1, b: 'foo', c: 1.0, e: [1,2,3]} 



Return a new hash that tells you the types of each value contained in keys, such that it returns:
{:a => 'Fixnum', :b => 'String', :c => 'Float', :e  => 'Array[Fixnum]'}



Hard:

Given a hash:
{a: {b: 3}, c: [1,2,3], d.: {e: {f: 'g'}}}



Return the following, diving into hashes to find their value types:

{:a => {:b => 'Fixnum'}, :c => 'Array[Fixnum]', :.d => {:e => {:f => 'String'}}}


Best of luck!

This post has been edited by Lemur: 18 January 2014 - 08:56 PM


Is This A Good Question/Topic? 2
  • +

Replies To: [Challenge][Med-Hard] Hash Traversal

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 06:20 PM

Medium

Spoiler

Output:
Spoiler

I think I'm missing something though, as this is a little too simple?

This post has been edited by andrewsw: 18 January 2014 - 06:22 PM

Was This Post Helpful? 1
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 06:32 PM

What should the output be in this case:
{:a => 1, :b => "hi", :c => {}}

Reasonable options seem to be:
{:a => 'Fixnum', :b => 'String', :c => {}}
which would match what we do if :c actually has members.

Another option:
{:a => 'Fixnum', :b => 'String', :c => Hash}
An empty hash just returns its type rather than being left empty in the result.

Solution for leaving empty hashes empty in the result:
Spoiler

Solution for returning the class Hash in the case of an empty hash:
Spoiler

Was This Post Helpful? 1
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1372
  • View blog
  • Posts: 3,469
  • Joined: 28-November 09

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 07:08 PM

Indeed, the empty hash case is something I didn't account for. I would say Hash would be perfectly valid.

I've added the array part to the Medium challenge to make it slightly more difficult.

Hard

Spoiler


For whatever reason my original post is odd.

This post has been edited by Lemur: 18 January 2014 - 07:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 08:22 PM

Medium

Spoiler

Took a bit more effort this time ;)

Output:
Spoiler

This post has been edited by andrewsw: 18 January 2014 - 08:25 PM

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 08:51 PM

Medium

This is slightly more compact:

Spoiler

This is my final answer! I don't want to phone a friend..
Was This Post Helpful? 2
  • +
  • -

#7 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1372
  • View blog
  • Posts: 3,469
  • Joined: 28-November 09

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 08:54 PM

Look into reduce: http://ruby-doc.org/...method-i-reduce

Though do note that if you do something like this:
hash.each.reduce({}) { |subhash, k, v| ... }



that it will fail. Piping it to reduce combines the k/v pair into an array, so you need to do this:
hash.each.reduce({}) { |subhash, (k, v)| ... }


This post has been edited by Lemur: 18 January 2014 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 08:56 PM

What if the array is of a mixed type?

What should
{:a => 1, :b => 2, :c => [1, 2, 3, "hello"]}
Return?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 09:00 PM

I'm referring to the Medium example:
Ruby is dynamic so, unless we want to iterate each element and produce a type-name, I made the assumption that the array will contain the same types.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 09:10 PM

View PostLemur, on 19 January 2014 - 03:54 AM, said:


I'm not sure if this was addressed to me? If so, it's a little beyond my ken at the moment. The only thing I've ever done with Ruby is respond to these two challenges ;)



Symbols are confusing me at the moment (as are a number of other oddities) but I haven't spent much time researching. Actually, I don't think I mean symbols; not :foo, but #bar..? I'll look into it some time ;)
Was This Post Helpful? 0
  • +
  • -

#11 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 09:14 PM

View Postandrewsw, on 18 January 2014 - 11:00 PM, said:

I'm referring to the Medium example:
Ruby is dynamic so, unless we want to iterate each element and produce a type-name, I made the assumption that the array will contain the same types.

Sorry, I was asking about the challenge requirements, rather than trying to poke holes in your solution =p
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 18 January 2014 - 09:50 PM

View Postxclite, on 19 January 2014 - 04:14 AM, said:

Sorry, I was asking about the challenge requirements, rather than trying to poke holes in your solution =p

That is what I understood you to mean ;)

I was just offering my interpretation of the requirement but you'll probably want to wait for the quizmaster to confirm.
Was This Post Helpful? 0
  • +
  • -

#13 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1372
  • View blog
  • Posts: 3,469
  • Joined: 28-November 09

Re: [Challenge][Med-Hard] Hash Traversal

Posted 19 January 2014 - 12:56 AM

View Postxclite, on 18 January 2014 - 09:56 PM, said:

What if the array is of a mixed type?

What should
{:a => 1, :b => 2, :c => [1, 2, 3, "hello"]}
Return?


A swift and well placed backhand. That seems like a really bad code smell do do something like that. Interpreting it would be as trivial as making an array reducer, but that still seems like horrid code practice. They're not tuples.

On terms of reduce, definitely something to look into. Actually, the entire enumerable library.

On the subject of Symbols, think of them only as markers. They're strings stripped of almost all of their methods, and are meant only for keys, markers, or other such things because of their light weight. Symbols are also immutable, and as such are much faster in comparison than strings, making them a lot faster for hash table lookups among other things.

Basically, if you're using a string as anything that could be construed as a label, use a Symbol.
Was This Post Helpful? 1
  • +
  • -

#14 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: [Challenge][Med-Hard] Hash Traversal

Posted 19 January 2014 - 11:45 AM

Thank you @Lemur. I'll look into these things some time ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1