Skip to content

Commit 7f88fab

Browse files
authored
Merge branch 'main' into main
2 parents 49ee8d7 + cc33678 commit 7f88fab

16 files changed

+1544
-168
lines changed

blog/Introduction to Cyber Security and Web Explosion.md

Lines changed: 179 additions & 0 deletions
Large diffs are not rendered by default.

blog/authors.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ nayanikamukherjee:
4646
title: Full Stack Developer
4747
url: "https://github.com/Nayanika1402"
4848
image_url: https://avatars.githubusercontent.com/u/132455412?v=4
49+
50+
pujan-sarkar:
51+
name: Pujan Sarkar
52+
title: Cyber Security Enthusiast
53+
url: "https://github.com/Pujan-sarkar"
54+
image_url: https://avatars.githubusercontent.com/u/144250917?v=4

docs/Julia/dicti.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
id: dictionary-julia
3+
title: Dictionary
4+
sidebar_label: Julia Dictionary
5+
sidebar_position: 7
6+
tags: [Julia,Dictionary]
7+
8+
---
9+
Dictionary is a built-in data structure that allows you to associate keys with corresponding values. It is similar to a hash table or hash map in other programming languages. Dictionaries are particularly useful when you need to store and retrieve data quickly based on some unique identifier (the key).
10+
11+
### Creating a Dictionary
12+
13+
You can create a dictionary in Julia using curly braces `{}` and specifying key-value pairs separated by `=>` (called a pair constructor). Here’s how you can create a dictionary:
14+
15+
```julia
16+
# Creating a dictionary with city names as keys and their populations as values
17+
city_populations = Dict("New York" => 8336817, "Los Angeles" => 3979576, "Chicago" => 2693976)
18+
```
19+
Certainly! Here's an additional important note about dictionaries in Julia:
20+
21+
### Dictionary Keys and Types
22+
23+
In Julia, keys in a dictionary must be of a type that is both **hashable** and **immutable**. This requirement ensures that dictionaries can efficiently manage and retrieve values based on their keys.
24+
25+
#### Hashable and Immutable Keys:
26+
27+
1. **Hashable**: This means that the type of the key must have a stable and consistent way to compute its hash value. Most built-in types in Julia, such as `Int`, `String`, `Symbol`, and `Tuple` (if their elements are hashable), are hashable by default.
28+
29+
Example of hashable types:
30+
- `Int`
31+
- `String`
32+
- `Symbol`
33+
- `Tuple` (if elements are hashable)
34+
35+
2. **Immutable**: Once a key is placed in a dictionary, its value should not be able to change. This ensures that the hash value remains consistent. Immutable types in Julia include basic types like `Int`, `Float64`, `String`, and `Tuple`.
36+
37+
Example of immutable types:
38+
- `Int`
39+
- `Float64`
40+
- `String`
41+
- `Tuple`
42+
43+
#### Example
44+
45+
Some types in Julia are mutable or do not have a stable hash computation, making them unsuitable as dictionary keys:
46+
47+
- `Array`: Arrays are mutable and can change their contents, so they cannot be used as dictionary keys.
48+
- `Dict`: Dictionaries themselves cannot be keys in other dictionaries due to mutability.
49+
- Custom mutable types: If you define a custom type that is mutable, you need to ensure it implements proper hashing methods (`hash()` function) for it to be used as a dictionary key.
50+
51+
#### Example
52+
53+
Here’s an example illustrating the use of types as dictionary keys:
54+
55+
```julia
56+
# Valid keys
57+
dict_valid = Dict(1 => "One", "two" => 2.0, (3, "three") => "Tuple key")
58+
59+
# Invalid keys (will throw an error)
60+
dict_invalid = Dict([1, 2, 3] => "Array key")
61+
```
62+
63+
In this example:
64+
- `1`, `"two"`, and `(3, "three")` are valid keys because they are of types that are hashable and immutable.
65+
- `[1, 2, 3]` is an invalid key because arrays are mutable in Julia.
66+
67+
### Accessing Values in a Dictionary
68+
69+
To access a value in the dictionary, you use square brackets `[]` with the key:
70+
71+
```julia
72+
println(city_populations["New York"]) # Output: 8336817
73+
```
74+
75+
If the key doesn't exist in the dictionary, Julia will throw a `KeyError`.
76+
77+
### Adding and Modifying Entries
78+
79+
You can add new entries or modify existing ones in a dictionary using the same square bracket notation:
80+
81+
```julia
82+
city_populations["Houston"] = 2320268 # Adding a new entry
83+
city_populations["Chicago"] = 2716000 # Modifying an existing entry
84+
```
85+
86+
### Removing Entries
87+
88+
To remove an entry from a dictionary, use the `delete!()` function:
89+
90+
```julia
91+
delete!(city_populations, "Los Angeles")
92+
```
93+
94+
### Checking Key Existence
95+
96+
You can check if a key exists in a dictionary using the `haskey()` function:
97+
98+
```julia
99+
if haskey(city_populations, "New York")
100+
println("New York is in the dictionary.")
101+
end
102+
```
103+
104+
### Iterating Over a Dictionary
105+
106+
You can iterate over all key-value pairs in a dictionary using a `for` loop:
107+
108+
```julia
109+
for (city, population) in city_populations
110+
println("$city has a population of $population")
111+
end
112+
```
113+
114+
### Dictionary Methods
115+
116+
Julia provides several useful functions to work with dictionaries:
117+
118+
- `keys(dict)`: Returns an iterator over the keys of the dictionary.
119+
- `values(dict)`: Returns an iterator over the values of the dictionary.
120+
- `isempty(dict)`: Returns `true` if the dictionary is empty, `false` otherwise.
121+
- `merge!(dict1, dict2)`: Merges `dict2` into `dict1`, modifying `dict1`.
122+
123+
### Example Code and Output
124+
125+
Here’s a complete example demonstrating the use of dictionaries in Julia:
126+
127+
```julia
128+
# Creating a dictionary
129+
city_populations = Dict("New York" => 8336817, "Los Angeles" => 3979576, "Chicago" => 2693976)
130+
131+
# Accessing a value
132+
println(city_populations["New York"]) # Output: 8336817
133+
134+
# Adding a new entry
135+
city_populations["Houston"] = 2320268
136+
137+
# Modifying an existing entry
138+
city_populations["Chicago"] = 2716000
139+
140+
# Removing an entry
141+
delete!(city_populations, "Los Angeles")
142+
143+
# Iterating over the dictionary
144+
for (city, population) in city_populations
145+
println("$city has a population of $population")
146+
end
147+
148+
# Checking key existence
149+
if haskey(city_populations, "New York")
150+
println("New York is in the dictionary.")
151+
end
152+
```
153+
154+
#### Output:
155+
```
156+
New York has a population of 8336817
157+
Chicago has a population of 2716000
158+
Houston has a population of 2320268
159+
New York is in the dictionary.
160+
```
161+
162+
### Conclusion
163+
Dictionary data structure in Julia offers a powerful mechanism for associating keys with corresponding values, akin to hash maps or associative arrays in other languages. It allows for efficient storage and retrieval of data pairs, accommodating keys of any immutable type and values of any type. Operations like insertion, deletion, and updating of entries are straightforward and optimized for performance. Iteration over keys and values, along with methods for checking existence and merging dictionaries, further enhance its utility. Overall, Dictionary in Julia is indispensable for managing and manipulating structured data, providing flexibility and efficiency in various programming contexts. Its versatility makes it well-suited for tasks ranging from simple data mappings to complex data aggregation and manipulation operations.

docs/Julia/set.md

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
id: set-julia
3+
title: Set
4+
sidebar_label: Julia Set
5+
sidebar_position: 7
6+
tags: [Julia, Set, Methods of Set]
7+
---
8+
9+
In Julia, a `Set` is a collection of unique elements where each element can only appear once within the set. Sets are useful when you need to store a collection of items without duplicates and perform operations such as membership testing and set operations (like union, intersection, etc.).
10+
11+
### Creating Sets
12+
13+
You can create a set in Julia using curly braces `{}` and separating elements with commas. Let's create a set with some integers and strings:
14+
15+
```julia
16+
# Creating a set of integers
17+
int_set = Set([1, 2, 3, 4, 5, 2, 3]) # Duplicate elements are automatically removed
18+
println("Integer Set: ", int_set)
19+
20+
# Creating a set of strings
21+
str_set = Set(["apple", "banana", "orange", "banana"]) # Duplicate "banana" is removed
22+
println("String Set: ", str_set)
23+
```
24+
25+
**Output:**
26+
```julia
27+
Integer Set: Set([4, 2, 3, 5, 1])
28+
String Set: Set(["orange", "apple", "banana"])
29+
```
30+
31+
### Operations on Sets
32+
33+
#### Membership Testing
34+
You can check if an element is present in a set using the `in` operator:
35+
36+
```julia
37+
fruit_set = Set(["apple", "banana", "orange"])
38+
39+
println("Is 'apple' in fruit_set? ", "apple" in fruit_set)
40+
println("Is 'grape' in fruit_set? ", "grape" in fruit_set)
41+
```
42+
43+
**Output:**
44+
```julia
45+
Is 'apple' in fruit_set? true
46+
Is 'grape' in fruit_set? false
47+
```
48+
49+
50+
In Julia, a `Set` is a collection type that stores unique elements, where each element can only appear once within the set. Sets are useful when you need to ensure uniqueness of elements and perform operations like membership testing, union, intersection, and difference efficiently.
51+
52+
### Methods and Functions for Sets in Julia
53+
54+
#### 1. **Adding and Removing Elements**
55+
56+
Sets in Julia are mutable, which means you can modify them by adding or removing elements.
57+
58+
- **Adding elements**: Use the `push!()` function to add an element to a set.
59+
60+
```julia
61+
fruits = Set(["apple", "banana", "orange"])
62+
push!(fruits, "grape")
63+
```
64+
65+
- **Removing elements**: Use the `pop!()` function to remove an element from a set.
66+
67+
```julia
68+
pop!(fruits, "banana")
69+
```
70+
71+
#### 2. **Set Operations**
72+
73+
Julia provides various functions for set operations. These functions can be called directly on sets or by using the infix operators ``, ``, and `-`.
74+
75+
- **Union**: Combines elements from two sets, removing duplicates.
76+
77+
```julia
78+
set1 = Set([1, 2, 3])
79+
set2 = Set([3, 4, 5])
80+
81+
union_set = union(set1, set2)
82+
```
83+
84+
Alternatively, using infix operator:
85+
86+
```julia
87+
union_set = set1 set2
88+
```
89+
90+
- **Intersection**: Finds common elements between two sets.
91+
92+
```julia
93+
intersection_set = intersect(set1, set2)
94+
```
95+
96+
Alternatively, using infix operator:
97+
98+
```julia
99+
intersection_set = set1 set2
100+
```
101+
102+
- **Difference**: Finds elements in one set but not in another.
103+
104+
```julia
105+
difference_set = setdiff(set1, set2)
106+
```
107+
108+
#### 3. **Membership Testing**
109+
110+
You can check if an element is present in a set using the `in` keyword or the `in()` function.
111+
112+
```julia
113+
fruits = Set(["apple", "banana", "orange"])
114+
115+
if "apple" in fruits
116+
println("Apple is in the set!")
117+
end
118+
```
119+
120+
#### 4. **Set Comparisons**
121+
122+
You can compare sets using `==` and `isequal()` functions to check if two sets contain the same elements.
123+
124+
```julia
125+
set1 = Set([1, 2, 3])
126+
set2 = Set([3, 2, 1])
127+
128+
println(set1 == set2) # true
129+
println(isequal(set1, set2)) # true
130+
```
131+
132+
#### 5. **Set Size and Emptiness**
133+
134+
You can get the number of elements in a set using `length()` function and check if a set is empty using `isempty()` function.
135+
136+
```julia
137+
println(length(set1))
138+
println(isempty(set1))
139+
```
140+
141+
### Example
142+
143+
```julia
144+
set1 = Set([1, 2, 3, 4])
145+
set2 = Set([3, 4, 5, 6])
146+
147+
println("Set 1: ", set1)
148+
println("Set 2: ", set2)
149+
150+
union_set = set1 set2
151+
println("Union: ", union_set)
152+
153+
intersection_set = set1 set2
154+
println("Intersection: ", intersection_set)
155+
156+
difference_set = setdiff(set1, set2)
157+
println("Difference (Set 1 - Set 2): ", difference_set)
158+
159+
push!(set1, 5)
160+
println("After adding 5 to Set 1: ", set1)
161+
162+
pop!(set2, 4)
163+
println("After removing 4 from Set 2: ", set2)
164+
165+
println("Is 3 in Set 1? ", 3 in set1)
166+
println("Is 7 in Set 2? ", 7 in set2)
167+
168+
println("Length of Set 1: ", length(set1))
169+
println("Is Set 2 empty? ", isempty(set2))
170+
```
171+
172+
**Output:**
173+
```julia
174+
Set 1: Set([4, 2, 3, 1])
175+
Set 2: Set([4, 3, 5, 6])
176+
Union: Set([4, 2, 3, 5, 6, 1])
177+
Intersection: Set([4, 3])
178+
Difference (Set 1 - Set 2): Set([2, 1])
179+
After adding 5 to Set 1: Set([4, 2, 3, 5, 1])
180+
After removing 4 from Set 2: Set([3, 5, 6])
181+
Is 3 in Set 1? true
182+
Is 7 in Set 2? false
183+
Length of Set 1: 5
184+
Is Set 2 empty? false
185+
```
186+
187+
188+
### Conclusion
189+
190+
Sets in Julia are versatile data structures for managing unique collections of elements. They offer efficient membership testing and set operations, making them suitable for various computational tasks where uniqueness and set operations are key requirements.

0 commit comments

Comments
 (0)