Skip to content
Merged
Show file tree
Hide file tree
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
60 changes: 60 additions & 0 deletions Data-Structure-Arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Data Structure Arrays

Internally, `array` is a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An `array` is used to store a collection of data, but it is often more useful to think of an `array` as a collection of variables of the same type.

`array` consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

## Arrays in Python

Python doesn't have a native `array` data structure. An `array` in Python should not be confused with `list`. The major difference between a `list`
and an `array` in Python is that a `list` can have different types of values whereas an `array` should have all the values of same type.

#### Declaration of `array`

```python
from array import array
intarray = array('i') # Declares an array of integer type
```

#### Adding elements to `array`:

```python
intarray.append(1) # Adds an integer value of 1 to the array
intarray.append(0) # Adds an integer value of 0 to the array
intarray.append(-1) # Adds an integer value of -1 to the array
intarray.append(1) # Again adds an integer value of 1 to the array

intarray.append('d') # Would give a TypeError as the array is of integer type.

#Resolve the above error and then move ahead.
```

#### Printing an `array`:

```python
print(intarray) # Returns array('i', [1, 4, -1])
print(intarray[0]) # Returns 1 which is the element at index 0 of the array
print(intarray[3]) # Would give IndexError as there is no element at index 3 of array.

#Resolve the above error and then move ahead.

# To print all the elements of the array
for i in intarray:
print(i)
```

#### Basic operations on `array`:

```python
len(intarray) # Returns the length of the array i.e. 3
intarray.itemsize # Returns length in bytes of one array item i.e. 4 as it is an integer
intarray.count(1) # Returns the number of occurrences of 1 in the array i.e. 2
intarray.insert(1, 3) # Insert a new item with value x in the array before position i
intarray.remove(1) # Remove the first occurrence of 1 from the array
intarray.reverse() # Reverse the order of the items in the array
intarray.pop(1) # Removes the item with the index 1 from the array and returns it
```

:rocket: [Run Code](https://repl.it/CWJB)

[Official Docs](https://docs.python.org/3.5/library/array.html)
26 changes: 15 additions & 11 deletions Hash-Tables-And-Hashing-Functions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hash Tables and Hashing Functions

## Introduction to hashing
### Introduction to hashing

Hashing is designed to solve the problem of needing to efficiently find or store an item in a collection.
For example, if we have a list of 10,000 words of English and we want to check if a given word is in the list, it would be inefficient to successively compare the word with all 10,000 items until we find a match. Even if the list of words are lexographically sorted, like in a dictionary, you will still need some time to find the word you are looking for.
Expand All @@ -17,6 +17,7 @@ Generally, these hash codes are used to generate an index, at which the value is
In hash tables, you store data in forms of key and value pairs. The key, which is used to identify the data, is given as an input to the hashing function. The hash code, which is an integer, is then mapped to the fixed size we have.

Hash tables have to support 3 functions.

- insert (key, value)
- get (key)
- delete (key)
Expand Down Expand Up @@ -54,20 +55,22 @@ Position | Keys array | Values array
11 | Switzerland | Berne


Now, in this specific example things work quite well.
Our array needs to be big enough to accommodate the longest string, but in this case that’s only 11 slots.
And we do waste a bit of space because, for example, there are no 1-letter keys in our data, nor keys between 8 and 10 letters. But in this case, the wasted space isn’t so bad either. Taking the length of a string is nice and fast, and so is the process of finding the value associated with a given key (certainly faster than doing up to five string comparisons).
Now, in this specific example things work quite well.
Our array needs to be big enough to accommodate the longest string, but in this case that’s only 11 slots.
We do waste a bit of space because, for example, there are no 1-letter keys in our data, nor keys between 8 and 10 letters. But in this case, the wasted space isn’t so bad either. Taking the length of a string is nice and fast, and so is the process of finding the value associated with a given key (certainly faster than doing up to five string comparisons).

But, what do we do if our dataset has a string which has more than 11 characters?
What if we have one another word with 5 characters, "India", and try assigning it to an index using our hash function. Since the index 5 is already occupied, we have to make a call on what to do with it. This is called a collision.
What if we have one another word with 5 characters, "India", and try assigning it to an index using our hash function. Since the index 5 is already occupied, we have to make a call on what to do with it. This is called a collision.

If our dataset had a string with thousand characters, and you make an array of thousand indices to store the data, it would result in a wastage of space. If our keys were random words from English, where there are so many words with same length, using length as a hashing function would be fairly useless.


## Collision Handling

Two basic methods are used to handle collisions.
1. Separate Chaining
2. Open Addressing

1. Separate Chaining
2. Open Addressing

#### Separate Chaining

Expand Down Expand Up @@ -96,9 +99,10 @@ The problem with separate chaining is that the data structure can grow with out
Open addressing does not introduce any new data structure. If a collision occurs then we look for availability in the next spot generated by an algorithm. Open Addressing is generally used where storage space is a restricted, i.e. embedded processors. Open addressing not necessarily faster then separate chaining.

Methods for Open Addressing
- [Linear Probing](https://en.wikipedia.org/wiki/Linear_probing)
- [Quadratic Probing](https://en.wikipedia.org/wiki/Quadratic_probing)
- [Double Hashing](https://en.wikipedia.org/wiki/Double_hashing)

- [Linear Probing](https://en.wikipedia.org/wiki/Linear_probing)
- [Quadratic Probing](https://en.wikipedia.org/wiki/Quadratic_probing)
- [Double Hashing](https://en.wikipedia.org/wiki/Double_hashing)


## How to use hashing in your code.
Expand Down Expand Up @@ -164,4 +168,4 @@ Methods for Open Addressing
- [Bloom Filters](https://www.youtube.com/watch?v=-SuTGoFYjZs)
- [Hashing Strategies](https://www.youtube.com/watch?v=D65JQ0qQwZk)
- [Password Hashing](https://crackstation.net/hashing-security.htm)
- [Difference between Hashing and encrypting](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it)
- [Difference between Hashing and encrypting](http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it)
9 changes: 9 additions & 0 deletions LocalGroups-List.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Please note that all local groups are unofficial, independently organized groups
- [Dhaka](https://www.facebook.com/groups/free.code.camp.dhaka/)
- [Rangpur](https://www.facebook.com/groups/free.code.camp.rangpur/)
- [Sylhet](https://www.facebook.com/groups/free.code.camp.sylhet/)
- Barbados
- [Bridgetown](https://www.facebook.com/groups/free.code.camp.barbados)
- Belarus
- [Brest](https://www.facebook.com/groups/free.code.camp.brest/)
- [Gomel](https://www.facebook.com/groups/free.code.camp.gomel/)
Expand Down Expand Up @@ -242,6 +244,7 @@ Please note that all local groups are unofficial, independently organized groups
- [Hefei](https://www.facebook.com/groups/free.code.camp.hefei1/)
- [Hohhot](https://www.facebook.com/groups/free.code.camp.hohhot/)
- [Jincheng](https://www.facebook.com/groups/free.code.camp.jincheng/)
- [Kangbashen](https://www.facebook.com/groups/free.code.camp.kangbashen/)
- [Linyi](https://www.facebook.com/groups/free.code.camp.Linyi/)
- [Nanchang](https://www.facebook.com/groups/free.code.camp.Nanchang/)
- [Nanjing](https://www.facebook.com/groups/free.code.camp.nanjing/)
Expand Down Expand Up @@ -315,6 +318,7 @@ Please note that all local groups are unofficial, independently organized groups
- [Asyut](https://www.facebook.com/groups/free.code.camp.assiut/)
- [Cairo](https://www.facebook.com/groups/free.code.camp.cairo/)
- [Damanhur](https://www.facebook.com/groups/free.code.camp.damanhur)
- [Portsaid](https://www.facebook.com/groups/free.code.camp.portsaid)
- [Sharqiyah](https://www.facebook.com/groups/free.code.camp.sharkia/)
- [Tanta](https://www.facebook.com/groups/free.code.camp.tanta/)
- El Salvador
Expand Down Expand Up @@ -743,6 +747,7 @@ Please note that all local groups are unofficial, independently organized groups
- Montenegro
- [Tivat](https://www.facebook.com/groups/free.code.camp.tivat.montenegro/)
- Morocco
- [Agadir](https://www.facebook.com/groups/free.code.camp.agadir/)
- [Casablanca](https://www.facebook.com/groups/free.code.camp.casablanca/)
- [Marrakesh](https://www.facebook.com/groups/free.code.camp.marrakesh/)
- [Rabat](https://www.facebook.com/groups/965419830196366/)
Expand Down Expand Up @@ -1071,6 +1076,7 @@ Please note that all local groups are unofficial, independently organized groups
- Turkey
- [Ankara](https://www.facebook.com/groups/free.code.camp.ankara/)
- [Antalya](https://www.facebook.com/groups/free.code.camp.antalya/)
- [Isparta](https://www.facebook.com/groups/free.code.camp.isparta/)
- [Istanbul](https://www.facebook.com/groups/free.code.camp.istanbul/)
- [Izmir](https://www.facebook.com/groups/free.code.camp.izmir)
- Uganda
Expand Down Expand Up @@ -1245,6 +1251,7 @@ Please note that all local groups are unofficial, independently organized groups
- [Augusta](https://www.facebook.com/groups/free.code.camp.augusta.ga/)
- [Cochran](https://www.facebook.com/groups/free.code.camp.cochran.ga/)
- [Columbus](https://www.facebook.com/groups/free.code.camp.columbus.georgia/)
- [Flowery Branch](https://www.facebook.com/groups/free.code.camp.flowerybranchga/)
- [Lawrenceville](https://www.facebook.com/groups/free.code.camp.lawrenceville/)
- [Macon](https://www.facebook.com/groups/703656036438040)
- [Savannah](https://www.facebook.com/groups/free.code.camp.savannah/)
Expand Down Expand Up @@ -1334,6 +1341,7 @@ Please note that all local groups are unofficial, independently organized groups
- [Boston](https://www.facebook.com/groups/free.code.camp.boston/)
- [Burlington](https://www.facebook.com/groups/free.code.camp.massachusetts.burlington/)
- [Cape Cod](https://www.facebook.com/groups/free.code.camp.cape.cod/)
- [Hubbardston](https://www.facebook.com/groups/free.code.camp.hubbardston/)
- [Lexington](https://www.facebook.com/groups/free.code.camp.lexington.ma/)
- [Northampton](https://www.facebook.com/groups/free.code.camp.northampton.mass/)
- [Pepperell](https://www.facebook.com/groups/free.code.camp.pepperell.ma/)
Expand All @@ -1347,6 +1355,7 @@ Please note that all local groups are unofficial, independently organized groups
- [Fenton](https://www.facebook.com/groups/free.code.camp.fenton.mi/)
- [Flint](https://www.facebook.com/groups/free.code.camp.flint/)
- [Grand Rapids](https://www.facebook.com/groups/free.code.camp.grand.rapids/)
- [Ironwood](https://www.facebook.com/groups/free.code.camp.ironwood/)
- [Jackson](https://www.facebook.com/groups/free.code.camp.jackson.mi)
- [Kalamazoo](https://www.facebook.com/groups/free.code.camp.kalamazoo/)
- [Lansing](https://www.facebook.com/groups/free.code.camp.lansing/)
Expand Down