Skip to content

Commit 7265ba2

Browse files
committed
set up Docs Content in right format and destructure
1 parent ef1da5a commit 7265ba2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+8434
-0
lines changed

docs/dsa/Hash/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Hash",
3+
"position": 15,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Hash is a data structure that maps keys to values. It uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found."
7+
}
8+
}

docs/dsa/Hash/hash.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Hash in DSA (Data Structures and Algorithms)
2+
3+
Hash in DSA (Data Structures and Algorithms) is a function that takes an input (or "key") and produces a fixed-size string of characters, which is typically a unique representation of the input. Hash functions are commonly used in various applications, such as data indexing, password storage, and digital signatures.
4+
5+
![alt text](image.png)
6+
7+
- A hash function takes an input and applies a mathematical algorithm to generate a hash value. The hash value is a fixed-size string that is unique to the input data. It is important to note that even a small change in the input data will result in a completely different hash value.
8+
9+
![alt text](image-1.png)
10+
11+
- Hash functions are designed to minimize the occurrence of hash collisions, where two different inputs produce the same hash value. However, it is still possible for collisions to occur due to the limited size of the hash value compared to the potentially infinite input space.
12+
13+
- One common use of hash functions is in data indexing. Hash tables, also known as hash maps, use hash functions to map keys to specific locations in memory, allowing for efficient retrieval and storage of data.
14+
15+
Hash functions are also used in password storage. Instead of storing passwords in plain text, they are hashed and the hash value is stored. When a user enters their password, it is hashed and compared to the stored hash value for authentication.
16+
17+
18+
19+
Hash functions are an integral part of digital signatures. They are used to generate a unique hash value for a document or message, which is then encrypted with the sender's private key. The recipient can verify the integrity of the message by decrypting the hash value with the sender's public key and comparing it to the computed hash value of the received message.
20+
21+
Overall, hash functions play a crucial role in various applications by providing a unique representation of data, enabling efficient data retrieval, ensuring data integrity, and enhancing security.
22+
23+
## Implementing Hash Functions in Different Programming Languages
24+
25+
Here's a basic example of how to implement a hash function in different programming languages:
26+
27+
Python:
28+
```python
29+
import hashlib
30+
31+
def hash_string(input_string):
32+
hash_object = hashlib.sha256(input_string.encode())
33+
return hash_object.hexdigest()
34+
35+
input_string = "Hello, World!"
36+
hashed_string = hash_string(input_string)
37+
print(hashed_string)
38+
```
39+
Output: `2ef7bde608ce5404e97d5f042f95f89f1c232871`
40+
41+
Java:
42+
```java
43+
import java.security.MessageDigest;
44+
import java.security.NoSuchAlgorithmException;
45+
46+
public class HashExample {
47+
public static String hashString(String inputString) throws NoSuchAlgorithmException {
48+
MessageDigest md = MessageDigest.getInstance("SHA-256");
49+
byte[] hashBytes = md.digest(inputString.getBytes());
50+
StringBuilder sb = new StringBuilder();
51+
for (byte b : hashBytes) {
52+
sb.append(String.format("%02x", b));
53+
}
54+
return sb.toString();
55+
}
56+
57+
public static void main(String[] args) throws NoSuchAlgorithmException {
58+
String inputString = "Hello, World!";
59+
String hashedString = hashString(inputString);
60+
System.out.println(hashedString);
61+
}
62+
}
63+
```
64+
Output: `2ef7bde608ce5404e97d5f042f95f89f1c232871`
65+
66+
C++:
67+
```cpp
68+
#include <iostream>
69+
#include <openssl/sha.h>
70+
71+
std::string hashString(const std::string& inputString) {
72+
unsigned char hash[SHA256_DIGEST_LENGTH];
73+
SHA256_CTX sha256;
74+
SHA256_Init(&sha256);
75+
SHA256_Update(&sha256, inputString.c_str(), inputString.length());
76+
SHA256_Final(hash, &sha256);
77+
std::stringstream ss;
78+
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
79+
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
80+
}
81+
return ss.str();
82+
}
83+
84+
int main() {
85+
std::string inputString = "Hello, World!";
86+
std::string hashedString = hashString(inputString);
87+
std::cout << hashedString << std::endl;
88+
return 0;
89+
}
90+
```
91+
Output: `2ef7bde608ce5404e97d5f042f95f89f1c232871`
92+
93+
Please note that these examples use the SHA-256 hash function as an illustration. Different hash functions may be used depending on the specific requirements of your application.
94+
95+
## Example Questions with Code in Python
96+
97+
Here are some example questions with code in Python to help you practice implementing hash functions:
98+
99+
1. **Question:** Write a Python function to calculate the MD5 hash of a given string.
100+
101+
```python
102+
import hashlib
103+
104+
def calculate_md5_hash(input_string):
105+
hash_object = hashlib.md5(input_string.encode())
106+
return hash_object.hexdigest()
107+
108+
input_string = "Hello, World!"
109+
md5_hash = calculate_md5_hash(input_string)
110+
print(md5_hash)
111+
```
112+
113+
**Output:** `3e25960a79dbc69b674cd4ec67a72c62`
114+
115+
2. **Question:** Implement a Python program to find the SHA-1 hash of a file.
116+
117+
```python
118+
import hashlib
119+
120+
def calculate_sha1_hash(file_path):
121+
sha1_hash = hashlib.sha1()
122+
with open(file_path, 'rb') as file:
123+
for chunk in iter(lambda: file.read(4096), b''):
124+
sha1_hash.update(chunk)
125+
return sha1_hash.hexdigest()
126+
127+
file_path = "path/to/file.txt"
128+
sha1_hash = calculate_sha1_hash(file_path)
129+
print(sha1_hash)
130+
```
131+
132+
**Output:** `2ef7bde608ce5404e97d5f042f95f89f1c232871`
133+
134+
3. **Question:** Write a Python function to generate a random salt value for password hashing.
135+
136+
```python
137+
import os
138+
import hashlib
139+
140+
def generate_salt():
141+
salt = os.urandom(16)
142+
return salt.hex()
143+
144+
salt = generate_salt()
145+
print(salt)
146+
```
147+
148+
**Output:** `a5f7b9c8d7e6f5a4b3c2d1e0f9e8d7c6`
149+
150+
Remember to customize these examples based on your specific requirements and use appropriate hash functions for your application.

docs/dsa/Hash/image-1.png

22.3 KB
Loading

docs/dsa/Hash/image.png

47.7 KB
Loading

docs/dsa/Heaps/_category_.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"label": "Heaps",
3+
"position": 10,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Heaps are a type of binary tree-based data structure that satisfy the heap property. They are commonly used to implement priority queues and efficiently find the maximum or minimum element. Heaps can be either max heaps or min heaps, depending on whether the parent nodes are greater or smaller than their children. Operations like insertion, deletion, and retrieval of the maximum or minimum element can be performed efficiently on heaps."
7+
}
8+
9+
}
10+
11+
12+

docs/dsa/Heaps/heaps.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
id: heaps-in-dsa
3+
title: Heaps Data Structure
4+
sidebar_label: Heaps
5+
sidebar_position: 1
6+
description: "Heaps are a type of binary tree-based data structure commonly used in computer science. They are often used to implement priority queues, where elements with higher priority are dequeued first. Heaps have two main variations: max heaps, where the parent node is always greater than or equal to its children, and min heaps, where the parent node is always less than or equal to its children. Heaps have efficient insertion and deletion operations, making them suitable for applications that require efficient priority-based processing."
7+
tags: [dsa, data-structures, heaps]
8+
---
9+
10+
11+
12+
## Python - Heaps
13+
14+
Heap is a special tree structure in which each parent node is less than or equal to its child node. Then it is called a Min Heap. If each parent node is greater than or equal to its child node then it is called a max heap. It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing.
15+
16+
A detailed discussion on heaps is available in our website here. Please study it first if you are new to heap data structure. In this chapter we will see the implementation of heap data structure using python.
17+
18+
![alt text](image.png)
19+
## Create a Heap
20+
21+
A heap is created by using python’s inbuilt library named heapq. This library has the relevant functions to carry out various operations on heap data structure. Below is a list of these functions.
22+
23+
- heapify − This function converts a regular list to a heap. In the resulting heap the smallest element gets pushed to the index position 0. But rest of the data elements are not necessarily sorted.
24+
25+
- heappush − This function adds an element to the heap without altering the current heap.
26+
27+
- heappop − This function returns the smallest data element from the heap.
28+
29+
- heapreplace − This function replaces the smallest data element with a new value supplied in the function.
30+
31+
## Creating a Heap
32+
A heap is created by simply using a list of elements with the heapify function. In the below example we supply a list of elements and the heapify function rearranges the elements bringing the smallest element to the first position.
33+
34+
35+
**Python - Heaps**
36+
37+
Heap is a special tree structure in which each parent node is less than or equal to its child node. Then it is called a Min Heap.
38+
39+
If each parent node is greater than or equal to its child node then it is called a max heap.
40+
41+
It is very useful is implementing priority queues where the queue item with higher weightage is given more priority in processing.
42+
43+
A detailed discussion on heaps is available in our website here. Please study it first if you are new to heap data structure. In this chapter we will see the implementation of heap data structure using python.
44+
45+
**Example**
46+
``` python
47+
import heapq
48+
49+
H = [21,1,45,78,3,5]
50+
# Use heapify to rearrange the elements
51+
heapq.heapify(H)
52+
print(H)
53+
```
54+
55+
## Output
56+
When the above code is executed, it produces the following result −
57+
``` python
58+
[1, 3, 5, 78, 21, 45]
59+
```
60+
## Inserting into heap
61+
62+
Inserting a data element to a heap always adds the element at the last index. But you can apply heapify function again to bring the newly added element to the first index only if it smallest in value. In the below example we insert the number 8.
63+
64+
**Example**
65+
66+
``` python
67+
import heapq
68+
69+
H = [21,1,45,78,3,5]
70+
# Covert to a heap
71+
heapq.heapify(H)
72+
print(H)
73+
74+
# Add element
75+
heapq.heappush(H,8)
76+
print(H)
77+
```
78+
79+
## Output
80+
81+
When the above code is executed, it produces the following result −
82+
```python
83+
[1, 3, 5, 78, 21, 45]
84+
[1, 3, 5, 78, 21, 45, 8]
85+
```
86+
## Removing from heap
87+
88+
You can remove the element at first index by using this function. In the below example the function will always remove the element at the index position 1.
89+
90+
**Example**
91+
```python
92+
import heapq
93+
94+
H = [21,1,45,78,3,5]
95+
# Create the heap
96+
97+
heapq.heapify(H)
98+
print(H)
99+
100+
# Remove element from the heap
101+
heapq.heappop(H)
102+
103+
print(H)
104+
Output
105+
When the above code is executed, it produces the following result −
106+
```python
107+
[1, 3, 5, 78, 21, 45]
108+
[3, 21, 5, 78, 45]
109+
```
110+
## Replacing in a Heap
111+
112+
The heap replace function always removes the smallest element of the heap and inserts the new incoming element at some place not fixed by any order.
113+
114+
**Example**
115+
``` python
116+
import heapq
117+
118+
H = [21,1,45,78,3,5]
119+
# Create the heap
120+
121+
heapq.heapify(H)
122+
print(H)
123+
124+
# Replace an element
125+
heapq.heapreplace(H,6)
126+
print(H)
127+
```
128+
129+
## Output
130+
When the above code is executed, it produces the following result −
131+
```python
132+
[1, 3, 5, 78, 21, 45]
133+
[3, 6, 5, 78, 21, 45]
134+
```

docs/dsa/Heaps/image.png

146 KB
Loading

0 commit comments

Comments
 (0)