Skip to content

Commit e8e79dc

Browse files
authored
Merge branch 'main' into javascript-dom-method-querySelectorAll
2 parents 185da65 + 3f88711 commit e8e79dc

File tree

12 files changed

+559
-0
lines changed

12 files changed

+559
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
Title: '.find()'
3+
Description: 'Searches for a specific element in a C++ STL set.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Game Development'
7+
Tags:
8+
- 'Algorithms'
9+
- 'Iterators'
10+
- 'Sets'
11+
CatalogContent:
12+
- 'learn-c-plus-plus'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`.find()`** method in C++ searches for a specific element in a `std::set`. If the element is found, `.find()` returns an iterator pointing to the element; otherwise, it returns an iterator to `set.end()`. This method is part of the Standard Template Library (STL).
17+
18+
It allows efficient lookups in `std::set`, which is typically implemented as a balanced binary search tree. This ensures an average time complexity of **`O(log n)`** for lookups.
19+
20+
## Syntax
21+
22+
```pseudo
23+
std::set<data_type>::iterator it = set_name.find(value);
24+
```
25+
26+
**Parameters:**
27+
28+
- **`set_name`**: The name of the `std::set` being searched.
29+
- **`value`**: The element to search for in the set.
30+
31+
**Return Value:**
32+
33+
- If the element is found, it returns an iterator pointing to the element.
34+
- If the element is not found, it returns an iterator to `set.end()`.
35+
36+
## Examples
37+
38+
### Finding an Element in a Set
39+
40+
The following example demonstrates how to search for an element in a `std::set` and determine whether it exists.
41+
42+
```cpp
43+
#include <iostream>
44+
#include <set>
45+
46+
int main() {
47+
std::set<int> numbers = {10, 20, 30, 40, 50};
48+
int searchValue = 30;
49+
50+
auto it = numbers.find(searchValue);
51+
52+
if (it != numbers.end()) {
53+
std::cout << "Element found: " << *it << std::endl;
54+
} else {
55+
std::cout << "Element not found" << std::endl;
56+
}
57+
58+
return 0;
59+
}
60+
```
61+
62+
The output will be:
63+
64+
```shell
65+
Element found: 30
66+
```
67+
68+
### Checking if a Product Exists in Inventory
69+
70+
This example shows how `.find()` helps determine whether a product ID exists in the inventory.
71+
72+
```cpp
73+
#include <iostream>
74+
#include <set>
75+
76+
int main() {
77+
std::set<int> productIDs = {101, 202, 303, 404, 505};
78+
int searchID = 303;
79+
std::set<int>::iterator it = productIDs.find(searchID);
80+
81+
if (it != productIDs.end()) {
82+
std::cout << "Product ID " << searchID << " is available in inventory."
83+
<< std::endl;
84+
85+
} else {
86+
std::cout << "Product ID " << searchID << " is not in inventory."
87+
<< std::endl;
88+
}
89+
90+
return 0;
91+
}
92+
```
93+
94+
The output will be:
95+
96+
```shell
97+
Product ID 303 is available in inventory.
98+
```
99+
100+
### Searching for Employees in a Company Directory
101+
102+
This example uses `.find()` to search for an employee’s name in a directory, ensuring efficient access to records.
103+
104+
```cpp
105+
#include <iostream>
106+
#include <set>
107+
#include <string>
108+
109+
struct CompareIgnoreCase {
110+
bool operator()(const std::string &a, const std::string &b) const {
111+
return a < b;
112+
}
113+
};
114+
115+
int main() {
116+
std::set<std::string, CompareIgnoreCase> names = {"Alice", "Bob", "Charlie"};
117+
std::string searchName = "Bob";
118+
auto it = names.find(searchName);
119+
120+
if (it != names.end()) {
121+
std::cout << "Name found: " << *it << std::endl;
122+
123+
} else {
124+
std::cout << "Name not found." << std::endl;
125+
}
126+
127+
return 0;
128+
}
129+
```
130+
131+
The output will be:
132+
133+
```shell
134+
Name found: Bob
135+
```
136+
137+
## Codebyte Example
138+
139+
This example demonstrates a simple use of `.find()` in a C++ STL set. It checks for the presence of a specific integer in a set and prints whether the element was found or not.
140+
141+
```codebyte/cpp
142+
#include <iostream>
143+
#include <set>
144+
145+
int main() {
146+
std::set<int> numbers = {100, 200, 300, 400};
147+
int searchValue = 200;
148+
auto it = numbers.find(searchValue);
149+
150+
if (it != numbers.end()) {
151+
std::cout << "Found: " << *it << std::endl;
152+
153+
} else {
154+
std::cout << "Not found." << std::endl;
155+
}
156+
157+
return 0;
158+
}
159+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
Title: 'Huffman Coding'
3+
Description: 'A lossless data compression algorithm that assigns variable-length codes to input characters based on their frequencies.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Code Foundations'
7+
Tags:
8+
- 'Algorithms'
9+
- 'Data Structures'
10+
- 'Search Algorithms'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
**Huffman coding** is a popular algorithm for lossless data compression that assigns variable-length codes to input characters based on their frequencies. Characters that appear more frequently in the data are assigned shorter codes, while less frequent characters receive longer codes. This approach minimizes the overall space required to represent the original data.
17+
18+
The algorithm was developed by David A. Huffman in 1952 while he was a Ph.D. student at MIT. It has since become a fundamental technique in information theory and is used in many compression formats and transmission protocols.
19+
20+
## How Huffman Coding Works
21+
22+
Huffman coding works through the following steps:
23+
24+
- Frequency Analysis: Count the frequency of each character in the input data.
25+
- Build the Huffman Tree:
26+
- Create a leaf node for each character and add it to a priority queue, with priority based on frequency (lower frequency = higher priority).
27+
- While there is more than one node in the queue:
28+
- Remove the two nodes with the highest priority (lowest frequency).
29+
- Create a new internal node with these two nodes as children, with a frequency equal to the sum of both nodes' frequencies.
30+
- Add the new node back to the priority queue.
31+
- The remaining node is the root of the Huffman tree.
32+
- Generate Huffman Codes:
33+
- Traverse the tree from the root to each leaf.
34+
- Assign '0' for a left branch and '1' for a right branch.
35+
- The code for each character is the sequence of 0s and 1s on the path from the root to the character's leaf node.
36+
- Encoding: Replace each character in the original data with its corresponding Huffman code.
37+
38+
## Example
39+
40+
Let's use a simple example to illustrate Huffman coding. Consider encoding the string "ABRACADABRA":
41+
42+
### Step 1: Calculate Character Frequencies
43+
44+
| Character | Frequency |
45+
| --------- | --------- |
46+
| A | 5 |
47+
| B | 2 |
48+
| R | 2 |
49+
| C | 1 |
50+
| D | 1 |
51+
52+
### Step 2: Build the Huffman Tree
53+
54+
Starting with leaf nodes for each character:
55+
56+
```plaintext
57+
A(5) B(2) R(2) C(1) D(1)
58+
```
59+
60+
First, combine the two lowest frequency nodes (C and D):
61+
62+
![Huffman Coding Tree Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/huffman_coding_1.png)
63+
64+
```plaintext
65+
Remaining: A(5) B(2) R(2) [2]
66+
```
67+
68+
Next, combine the [2] node with another lowest frequency node (B or R, both have 2):
69+
70+
![Huffman Coding Tree Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/huffman_coding_2.png)
71+
72+
```plaintext
73+
Remaining: A(5) R(2) [4]
74+
```
75+
76+
Combine R with the [4] node:
77+
78+
![Huffman Coding Tree Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/huffman_coding_3.png)
79+
80+
```plaintext
81+
Remaining: A(5) [6]
82+
```
83+
84+
Finally, combine the remaining nodes:
85+
86+
![Huffman Coding Tree Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/huffman_coding_4.png)
87+
88+
## Step 3: Generate Huffman Codes
89+
90+
Assigning 0 for left branches and 1 for right branches:
91+
92+
| Character | Path from Root | Huffman Code |
93+
| --------- | ---------------------------- | ------------ |
94+
| A | Left | 0 |
95+
| R | Right → Left | 10 |
96+
| B | Right → Right → Right | 111 |
97+
| C | Right → Right → Left → Left | 1100 |
98+
| D | Right → Right → Left → Right | 1101 |
99+
100+
## Step 4: Encode the String
101+
102+
Now encode "ABRACADABRA":
103+
104+
```
105+
A B R A C A D A B R A
106+
0 111 10 0 1100 0 1101 0 111 10 0
107+
```
108+
109+
Without spaces: `01111001100011010111100`
110+
111+
The encoded string is 23 bits long, compared to 88 bits if using 8-bit fixed-length encoding (11 characters × 8 bits = 88 bits), achieving approximately 74% compression.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
Title: '.appendChild()'
3+
Description: 'Appends a node as the last child to a parent node in the DOM.'
4+
Subjects:
5+
- 'Web Development'
6+
- 'Web Design'
7+
Tags:
8+
- 'Arguments'
9+
- 'DOM'
10+
- 'ES6'
11+
- 'Functions'
12+
CatalogContent:
13+
- 'introduction-to-javascript'
14+
- 'paths/front-end-engineer-career-path'
15+
---
16+
17+
The **`.appendChild()`** method in JavaScript is a key DOM manipulation tool that appends a node (element or text node) as the last child of a specified parent. It allows developers to dynamically modify a webpage’s structure. If the node already exists in the DOM, it is moved to its new position. The method returns the appended node.
18+
19+
## Syntax
20+
21+
```pseudo
22+
parentNode.appendChild(childNode);
23+
```
24+
25+
- `parentNode`: The parent element to which the `childNode` will be appended.
26+
- `childNode`: The node to append (element or text node).
27+
28+
> **Notes:**
29+
>
30+
> - If `childNode` already exists in the DOM, it is moved to the new position instead of being duplicated.
31+
> - Unlike `.append()`, `.appendChild()` accepts only a single node and does not support multiple nodes or strings.
32+
33+
## Example
34+
35+
The following example shows how to use `.appendChild()` to add a paragraph to a `<div>`.
36+
37+
Here is the HTML code:
38+
39+
```html
40+
<div id="container">
41+
<p>This paragraph was added with appendChild!</p>
42+
</div>
43+
```
44+
45+
Here is the JavaScript code:
46+
47+
```js
48+
// Select the parent node
49+
const parentDiv = document.getElementById('container');
50+
51+
// Create a new paragraph element
52+
const newParagraph = document.createElement('p');
53+
54+
// Add text content to the element
55+
newParagraph.textContent = 'This paragraph was added with .appendChild()!';
56+
57+
// Append it as the last child to the parent node
58+
parentDiv.appendChild(newParagraph);
59+
```
60+
61+
The output is of this code is as following:
62+
63+
```plaintext
64+
This paragraph was added with .appendChild()!
65+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
Title: '.createElement()'
3+
Description: 'Generates a new element node of the specified type.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Design'
7+
Tags:
8+
- 'JavaScript'
9+
- 'DOM'
10+
- 'Methods'
11+
- 'Elements'
12+
CatalogContent:
13+
- 'introduction-to-javascript'
14+
- 'paths/front-end-engineer-career-path'
15+
---
16+
17+
In JavaScript, the **`.createElement()`** [method](https://www.codecademy.com/resources/docs/javascript/methods) of the `document` [object](https://www.codecademy.com/resources/docs/javascript/objects) creates a new element node of the specified type. This method returns an `HTMLElement` instance, which can be modified and appended to the DOM.
18+
19+
## Syntax
20+
21+
```pseudo
22+
document.createElement(type)
23+
```
24+
25+
- `type`: A string representing the tag name of the element to be created.
26+
27+
## Example
28+
29+
The following example demonstrates the usage of the `.createElement()` method:
30+
31+
```js
32+
function addElement() {
33+
// Create a div element
34+
const myDiv = document.createElement('div');
35+
36+
// Create a text node containing data
37+
const data = document.createTextNode('Hi, Codecademy!');
38+
39+
// Insert the data into the div element
40+
myDiv.appendChild(data);
41+
42+
// Add the element to the body
43+
document.body.appendChild(myDiv);
44+
}
45+
46+
// Call the function to add the element to the page
47+
addElement();
48+
```
49+
50+
The above code dynamically adds the following text to the webpage:
51+
52+
```plaintext
53+
Hi, Codecademy!
54+
```

0 commit comments

Comments
 (0)