# Notebook 2

This notebook will add an entry to the Hash Table. Click **here** to access it.

## Task 1

Inside the first cell, create a `Node`

class.

```
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
```

Run the cell.

## Task 2

Whenever an entry is inserted:

- Process the key into a
`hash()`

function. - Performing a bitwise
`&`

operation between the length and the unique hash. - Insert your node at the computed index.

```
def insert(self, key, value):
if not self.buckets:
self.buckets = [None] * 16
hash_key = hash(key) ## <---- step 1
index = (len(self.buckets) - 1) & hash_key ## <---- step 2
node = Node(key, value) ## <----- step 3
self.buckets[index] = node ## <----- step 3
```

## Task 3

Run all cells.

Your inventory should now contain `Apples`

.

```
BUCKET 0
None
BUCKET 1
None
BUCKET 2
key = Apple, value = 2.99
BUCKET 3
None
...
```

## Solution

You can access the solution code **here**.

##### This workbook was created by Jad and Rayan Slim. Feel free to explore some of their courses:

##### The Complete Java Development Bootcamp

##### The Complete Spring Boot Development Bootcamp – Become a Java Web Developer

Feedback Summary

0.0

0 students

5

0%

4

0%

3

0%

2

0%

1

0%

Written Reviews

There are no written reviews yet.