Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hash map tests for Ruby #28263

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions ruby/computer_science/project_hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ You already know the magic behind hash maps, now it's time to write your own imp
You might find yourself confusing keys with hash codes while accessing key-value pairs later. We would like to stress that the key is what your `hash` function will take as an input. In a way, we could say that the key is important for us only inside the `hash` function. But we never access a bucket directly with the key. Instead we do so with the hash code.

<div class="lesson-note lesson-note--tip" markdown="1">

Hash maps could accommodate various data types for keys like numbers, strings, and even other hashes. But for this project, only handle keys of type strings.

</div>

1. `set(key, value)` takes two arguments, the first is a key and the second is a value that is assigned to this key. If a key already exists, then the old value is overwritten or we can say that we *update* the key's value (e.g. `Carlos` is our key but it is called twice: once with value `I am the old value.`, and once with value `I am the new value.`. From the logic stated above, `Carlos` should contain only the latter value).
1. `#set(key, value)` takes two arguments, the first is a key and the second is a value that is assigned to this key. If a key already exists, then the old value is overwritten or we can say that we *update* the key's value (e.g. `Carlos` is our key but it is called twice: once with value `I am the old value.`, and once with value `I am the new value.`. From the logic stated above, `Carlos` should contain only the latter value).

In the meantime, a collision is when *TWO DIFFERENT* keys sit inside the same bucket, because they generate the same hash code (e.g. `Carlos` and `Carla` are both hashed to `3`, so `3` becomes a location for `Carlos` AND `Carla`. However, we know that it is the collision. It means we should find a way how to resolve it — how to *deal with collisions*, which was mentioned in the previous lesson).

- Remember to grow your buckets size when it needs to, by calculating if your bucket has reached the `load factor`. Some of the methods in this assignment that are mentioned later could be reused to help you handle that growth logic more easily. So you may want to hold onto implementing your growing functionality just for now. However, the reason why we mention it with `set()` is because it's important to grow buckets exactly when they are being expanded.
- Remember to grow your buckets size when it needs to, by calculating if your bucket has reached the `load factor`. Some of the methods in this assignment that are mentioned later could be reused to help you handle that growth logic more easily. So you may want to hold onto implementing your growing functionality just for now. However, the reason why we mention it with `#set` is because it's important to grow buckets exactly when they are being expanded.

1. `#get(key)` takes one argument as a key and returns the value that is assigned to this key. If key is not found, return `nil`.

Expand All @@ -63,6 +65,49 @@ You already know the magic behind hash maps, now it's time to write your own imp

Remember that our hash map does not preserve insertion order when you are retrieving your hash map's data. It is normal and expected for keys and values to appear out of the order you inserted them in.

#### Test Your Hash Map

1. Create a new Ruby file.

1. Create a new instance of your hash map and set the load factor to be `0.75`.

```ruby
test = HashMap.new
```

1. Populate your hash map using the `#set(key, value)` method by copying the following:

```ruby
test.set('apple', 'red')
test.set('banana', 'yellow')
test.set('carrot', 'orange')
test.set('dog', 'brown')
test.set('elephant', 'gray')
test.set('frog', 'green')
test.set('grape', 'purple')
test.set('hat', 'black')
test.set('ice cream', 'white')
test.set('jacket', 'blue')
test.set('kite', 'pink')
test.set('lion', 'golden')
```

1. After populating your hash map with the data above, your hash map's actual capacity should now be at `0.75` (full capacity).

1. Now with a full hash map, try overwriting a few nodes using `#set(key, value)`. By right, this should only over-write the existing `values` of your nodes and not add new ones.

1. After that, populate your hash map with the last node below (doing this will make your hash map exceed your current load factor, hence expanding your buckets and growing your hash map):

```ruby
test.set('moon', 'silver')
```

1. If you have implemented your hash map correctly, the capacity of your new hash map will drop well below your load factor and you will notice that the nodes in your hash map are spread much evenly among your buckets.

1. With your new hash map, try overwriting a few nodes using `#set(key, value)`. Again, this should only over-write existing `values` of your nodes.

1. Test the other methods of your hash maps such as `#get(key)`, `#has?(key)`, `#remove(key)`, `#length`, `#clear`, `#keys`, `#values`, and `#entries` to check if they are still working as expected after expanding your hash map.

#### Extra Credit

- Create a class `HashSet` that behaves the same as a `HashMap` but only contains `keys` with no `values`.
Expand Down
Loading