# [Challenge][Med-Hard] Hash Traversal

Page 1 of 1

## 13 Replies - 2862 Views - Last Post: 19 January 2014 - 11:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=338488&amp;s=947d5a83477c9ca04283861b93198fea&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Lemur

• Pragmatism over Dogma

Reputation: 1439
• Posts: 3,609
• Joined: 28-November 09

# [Challenge][Med-Hard] Hash Traversal

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

• blow up my boots

Reputation: 6528
• Posts: 26,411
• 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

### #3 xclite

• I wrote you an code

Reputation: 1264
• Posts: 4,062
• 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

### #4 Lemur

• Pragmatism over Dogma

Reputation: 1439
• Posts: 3,609
• 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

### #5 andrewsw

• blow up my boots

Reputation: 6528
• Posts: 26,411
• 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

### #6 andrewsw

• blow up my boots

Reputation: 6528
• Posts: 26,411
• 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..

### #7 Lemur

• Pragmatism over Dogma

Reputation: 1439
• Posts: 3,609
• 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

### #8 xclite

• I wrote you an code

Reputation: 1264
• Posts: 4,062
• 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?

### #9 andrewsw

• blow up my boots

Reputation: 6528
• Posts: 26,411
• 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.

### #10 andrewsw

• blow up my boots

Reputation: 6528
• Posts: 26,411
• Joined: 12-December 12

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

Posted 18 January 2014 - 09:10 PM

Lemur, 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

### #11 xclite

• I wrote you an code

Reputation: 1264
• Posts: 4,062
• Joined: 12-May 09

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

Posted 18 January 2014 - 09:14 PM

andrewsw, 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

### #12 andrewsw

• blow up my boots

Reputation: 6528
• Posts: 26,411
• Joined: 12-December 12

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

Posted 18 January 2014 - 09:50 PM

xclite, 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.

### #13 Lemur

• Pragmatism over Dogma

Reputation: 1439
• Posts: 3,609
• Joined: 28-November 09

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

Posted 19 January 2014 - 12:56 AM

xclite, 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.

### #14 andrewsw

• blow up my boots

Reputation: 6528
• Posts: 26,411
• 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