Skip to content

Commit 7ddde44

Browse files
committed
DSA Update and Some Code notes Design
1 parent 3e8b6f1 commit 7ddde44

34 files changed

+834
-48
lines changed

pages/Abstract Classes.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Explanation
2+
- An **Abstract Class** is a class that cannot be instantiated directly. It defines a blueprint for subclasses to follow, and may contain abstract methods (methods without implementation) that must be implemented by concrete subclasses.
3+
- **"Enforces a contract"**: Abstract classes ensure that subclasses implement the required methods.
4+
- **Use Case**: When you want to define a common interface for a group of related classes.
5+
-
6+
- # Steps
7+
- Create an abstract class using the `abc` module in Python.
8+
-
9+
- Define abstract methods using `@abstractmethod` decorator.
10+
-
11+
- Create concrete subclasses that implement the abstract methods.
12+
-
13+
- ```python
14+
from abc import ABC, abstractmethod
15+
16+
class Animal(ABC):
17+
@abstractmethod
18+
def sound(self):
19+
pass
20+
21+
class Dog(Animal):
22+
def sound(self):
23+
print("Woof!")
24+
25+
class Cat(Animal):
26+
def sound(self):
27+
print("Meow!")
28+
29+
dog = Dog()
30+
dog.sound() # Output: Woof!
31+
32+
cat = Cat()
33+
cat.sound() # Output: Meow!
34+
```

pages/Abstraction.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Explanation
2+
- Abstraction is the OOP concept where you hide the complex implementation details and show only the essential features of an object. It allows you to focus on what an object does rather than how it performs the task. In Python, abstraction is typically implemented using abstract classes and abstract methods, where the class provides a skeleton and the subclass implements the details.
3+
4+
**Key Points**:
5+
- **Abstract Class**: A class that cannot be instantiated directly and may contain abstract methods.
6+
- **Abstract Method**: A method in the abstract class that does not have any implementation and must be overridden in subclasses.
7+
-
8+
- # Steps
9+
- Create an abstract class using the `ABC` module and `abstractmethod`.
10+
-
11+
- Define abstract methods without implementation in the abstract class.
12+
-
13+
- Implement these abstract methods in subclasses.
14+
-
15+
- ```python
16+
from abc import ABC, abstractmethod
17+
18+
class Animal(ABC): # Abstract class
19+
@abstractmethod
20+
def sound(self): # Abstract method
21+
pass
22+
23+
class Dog(Animal): # Concrete class
24+
def sound(self):
25+
print("Woof!")
26+
27+
class Cat(Animal): # Concrete class
28+
def sound(self):
29+
print("Meow!")
30+
31+
dog = Dog()
32+
dog.sound() # Output: Woof!
33+
```

pages/Binary Search.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
-
44
- # Steps:
55
- The **list must be sorted** first.
6+
-
67
- Start with the low pointer at index 0 and the high pointer at the last index of the list.
78
-
89
- Find the middle element: `mid = (low + high) // 2`.

pages/Bubble Sort.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@
1313
- **Average and Worst Case:** **O(n^2)**.
1414
-
1515
- ```python
16-
def bubble_sort(arr):
17-
n = len(arr)
18-
for i in range(n):
19-
swapped = False
20-
for j in range(0, n-i-1):
21-
if arr[j] > arr[j+1]:
22-
arr[j], arr[j+1] = arr[j+1], arr[j]
23-
swapped = True
24-
if not swapped:
25-
break
26-
return arr
16+
arr = [64, 34, 25, 12, 22, 11, 90, 5]
2717

28-
# Example usage
29-
arr = [64, 34, 25, 12, 22, 11, 90]
30-
print("Sorted array is:", bubble_sort(arr))
18+
n = len(arr)
19+
for i in range(n-1):
20+
for j in range(n-i-1):
21+
if arr[j] > arr[j+1]:
22+
arr[j], arr[j+1] = arr[j+1], arr[j]
23+
24+
print("Sorted array:", my_array)
3125
```

pages/CSS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# History
2-
collapsed:: true
32
- **How**:
43
- Developed by **Håkon Wium Lie** and **Bert Bos** in **1994** at **W3C** (World Wide Web Consortium).
54
- Designed to separate **content** (HTML) from **presentation** (layout and style), enabling web developers to control the look and feel of a webpage without affecting its structure.
@@ -49,7 +48,7 @@ collapsed:: true
4948
- Explore the following links for valuable resources, communities, and tools to enhance your skills:-
5049
-
5150
- [CSS Learning](https://github.com/micromata/awesome-css-learning)
52-
- [How Degine Resources ?](https://github.com/bradtraversy/design-resources-for-developers)
51+
- [How Design Resources ?](https://github.com/bradtraversy/design-resources-for-developers)
5352
- [CSS Tips & Tricks ](https://github.com/AllThingsSmitty/css-protips)
5453
- [Useful modules in CSS](https://github.com/awesome-css-group/awesome-css)
5554
- [CSS Fremworks](https://github.com/troxler/awesome-css-frameworks)
@@ -60,4 +59,5 @@ collapsed:: true
6059
- [No Worry About JavaScript 😉](https://github.com/you-dont-need/You-Dont-Need-JavaScript)
6160
- [CSS Interview questions 😎](https://github.com/Devinterview-io/css-interview-questions)
6261
- [CSS Animations](https://animate.style/)
63-
- [Creative Buttons](https://tympanus.net/Development/CreativeButtons/)
62+
- [Creative Buttons](https://tympanus.net/Development/CreativeButtons/)
63+
- [animejs](https://animejs.com/)

pages/Class.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Explanation
2+
- A class is a blueprint for creating objects (instances). It defines the properties (attributes) and behaviors (methods) that the objects created from it will have. In Python, a class is created using the `class` keyword.
3+
-
4+
- # Steps
5+
- Use the `class` keyword to define a class.
6+
-
7+
- **Add a Constructor (`__init__`)**:
8+
- The constructor method is used to initialize the attributes of the class when an object is created.
9+
-
10+
- **Define Methods**:
11+
- Methods are functions defined inside a class to perform actions or computations. These methods can modify or interact with the object's attributes.
12+
-
13+
- **Create Objects**:
14+
- Once a class is defined, you can create objects (instances) of that class.
15+
-
16+
- ```python
17+
# Step 1: Define the class
18+
class Dog:
19+
# Step 2: Add a constructor to initialize attributes
20+
def __init__(self, name, age):
21+
self.name = name # attribute 'name'
22+
self.age = age # attribute 'age'
23+
24+
# Step 3: Define a method
25+
def bark(self):
26+
print(f"{self.name} says woof!")
27+
28+
# Another method
29+
def info(self):
30+
print(f"{self.name} is {self.age} years old.")
31+
32+
# Step 4: Create objects (instances) of the class
33+
dog1 = Dog("Buddy", 3)
34+
dog2 = Dog("Lucy", 5)
35+
36+
# Call methods on objects
37+
dog1.bark() # Output: Buddy says woof!
38+
dog1.info() # Output: Buddy is 3 years old.
39+
40+
dog2.bark() # Output: Lucy says woof!
41+
dog2.info() # Output: Lucy is 5 years old.
42+
```

pages/Composition.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Explanation
2+
- **Composition** is an OOP principle where one object is made up of other objects, i.e., it has instances of other classes as attributes. Composition represents a "has-a" relationship between objects.
3+
- **Has-A Relationship**: This means that an object can contain other objects, which may represent part of its functionality.
4+
-
5+
- # Steps
6+
- Define classes for both the containing class and the contained class.
7+
-
8+
- Create an instance of the contained class as an attribute in the containing class.
9+
-
10+
- Use the contained object's methods or attributes from the containing class.
11+
-
12+
- ```python
13+
class Engine:
14+
def start(self):
15+
print("Engine started.")
16+
17+
class Car:
18+
def __init__(self, brand):
19+
self.brand = brand
20+
self.engine = Engine() # Car has an engine (Composition)
21+
22+
def drive(self):
23+
self.engine.start()
24+
print(f"The {self.brand} is driving.")
25+
26+
car = Car("Toyota")
27+
car.drive()
28+
```

pages/Constructor.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Explanation
2+
- A **constructor** is a special method that is called automatically when an object is created. In Python, the constructor is defined using the `__init__()` method. The constructor is used to initialize the attributes of the object when it is created.
3+
4+
**Key Points**:
5+
- **`__init__` Method**: A special method that acts as a constructor in Python. It is automatically invoked when an object is created.
6+
- **Attribute Initialization**: Inside the constructor, you can initialize the object’s attributes with specific values.
7+
-
8+
- # Steps
9+
- Define the `__init__()` method in your class.
10+
-
11+
- Initialize the object’s attributes inside the constructor.
12+
-
13+
- ```python
14+
class Car:
15+
def __init__(self, brand, model, year): # Constructor
16+
self.brand = brand
17+
self.model = model
18+
self.year = year
19+
20+
car = Car("Toyota", "Corolla", 2022) # Constructor is automatically called
21+
print(car.brand) # Output: Toyota
22+
```

pages/DSA , Algo & System Design.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
# OOP consepts
22
- ## The **core concepts** of OOP are indeed **14** :-
3-
- **Class** - Blueprint for creating objects, defining properties and methods.
3+
- [[Class]] - Blueprint for creating objects, defining properties and methods.
44
logseq.order-list-type:: number
5-
- Object - An instance of a class containing data and behavior.
5+
- [[Constructor]] - Special method for initializing objects when created.
66
logseq.order-list-type:: number
7-
- **Encapsulation** - Bundling data and methods, restricting direct access to an object's internal state.
7+
- [[Destructors]] - Method for cleaning up when an object is destroyed or goes out of scope.
88
logseq.order-list-type:: number
9-
- **Abstraction** - Hiding complex implementation details and exposing only essential features.
9+
- [[Object]] - An instance of a class containing data and behavior.
1010
logseq.order-list-type:: number
11-
- **Inheritance** - Mechanism by which one class derives properties and behaviors from another.
11+
- [[Encapsulation]] - Bundling data and methods, restricting direct access to an object's internal state.
1212
logseq.order-list-type:: number
13-
- **Polymorphism** - Ability for objects of different types to be treated as instances of a common superclass.
13+
- [[Abstraction]] - Hiding complex implementation details and exposing only essential features.
1414
logseq.order-list-type:: number
15-
- **Constructor** - Special method for initializing objects when created.
15+
- [[Inheritance]] - Mechanism by which one class derives properties and behaviors from another.
1616
logseq.order-list-type:: number
17-
- **Destructors** - Method for cleaning up when an object is destroyed or goes out of scope.
17+
- [[Polymorphism]] - Ability for objects of different types to be treated as instances of a common superclass.
1818
logseq.order-list-type:: number
19-
- **Composition** - Creating complex objects by combining simpler objects, a "has-a" relationship.
19+
- [[Composition]] - Creating complex objects by combining simpler objects, a "has-a" relationship.
20+
id:: 67e63024-2a0a-4a19-aff1-05ad33f056cf
2021
logseq.order-list-type:: number
21-
- **Interface** - Defines a contract that classes must follow, without providing implementation.
22+
- [[Interface]] - Defines a contract that classes must follow, without providing implementation.
2223
logseq.order-list-type:: number
23-
- **Method Overloading** - Defining multiple methods with the same name but different parameters.
24+
- [[Method Overloading]] - Defining multiple methods with the same name but different parameters.
2425
logseq.order-list-type:: number
25-
- **Method Overriding** - Redefining a method in a subclass to change its behavior.
26+
- [[Method Overriding]] - Redefining a method in a subclass to change its behavior.
2627
logseq.order-list-type:: number
27-
- **Static Methods and Class Methods** - Methods that belong to the class rather than instances.
28+
- [[Static Methods and Class Methods]] - Methods that belong to the class rather than instances.
2829
logseq.order-list-type:: number
29-
- **Dynamic Binding (Late Binding)** - Resolving method calls at runtime based on the object type.
30+
- [[Dynamic Binding (Late Binding)]] - Resolving method calls at runtime based on the object type.
3031
logseq.order-list-type:: number
3132
-
3233
- ## **additional related concepts** or **design patterns** that can supplement or extend OOP are:-
33-
- **Delegation** - One object handing over responsibilities to another.
34-
- **Mixin** - A class that provides functionality to other classes without being a parent class.
35-
- **Abstract Classes** - Classes that cannot be instantiated and may contain abstract methods.
36-
- **Loose Coupling and High Cohesion** - Reducing dependencies between components and ensuring focused class responsibilities
37-
- **Factory Pattern**: Creating objects without specifying the exact class of object to be created.
38-
- **Observer Pattern**: Notifying multiple objects about state changes in another object.
39-
- **Singleton Pattern**: Ensuring a class has only one instance and providing a global point of access.
34+
- [[Delegation]] - One object handing over responsibilities to another.
35+
- [[Mixin]] - A class that provides functionality to other classes without being a parent class.
36+
- [[Abstract Classes]] - Classes that cannot be instantiated and may contain abstract methods.
37+
- [[Loose Coupling and High Cohesion]] - Reducing dependencies between components and ensuring focused class responsibilities
38+
- [[Factory Pattern]] - Creating objects without specifying the exact class of object to be created.
39+
- [[Observer Pattern]] - Notifying multiple objects about state changes in another object.
40+
- [[Singleton Pattern]] - Ensuring a class has only one instance and providing a global point of access.
4041
-
4142
- # Algorithms
4243
- ## Searching:
@@ -117,7 +118,7 @@
117118
- 15) [[Zobrist Hashing]] - hashing technique used in computer games
118119
- 16) [[FM-index]] - data structure used for efficient full-text searching
119120
- 17) [[Circular buffer]] - works like a fixed-size queue
120-
- 18) [[Hungarian / Kuhn–Munkres / Munkres Assignment-Algorithm]] - combinatorial optimization algorithm
121+
- 18) [[Hungarian - Kuhn–Munkres - Munkres Assignment-Algorithm]] - combinatorial optimization algorithm
121122
- 19) [[Dekker's Algorithm]] - first algorithms to solve the mutual exclusion
122123
- 20) [[Winged Edge]] - used to represent **polygonal meshes**
123124
- 21) [[Burrows–Wheeler Transform]] - string transformation algorithm
@@ -129,6 +130,11 @@
129130
- 27) [[Disjoint-set Data Structure]] - efficiently perform union and find operations
130131
- 28)[[Bloom Filter]] - space-efficient probabilistic data structure used to test whether
131132
-
133+
- # Short fourmulas
134+
- [[fibonacci]] - Short trick
135+
- [[palindrome]] - short method
136+
-
137+
-
132138
- # More Learn
133139
- Explore the following links for valuable resources, communities, and tools to enhance your skills:
134140
-
@@ -150,7 +156,4 @@
150156
- [System Design E-book](https://github.com/karanpratapsingh/system-design)
151157
- [Coding Interview Preparation](https://github.com/jwasham/coding-interview-university)
152158
- [SDE Interview Question](https://github.com/twowaits/SDE-Interview-Questions)
153-
-
154-
-
155-
-
156-
-
159+
-

pages/Delegation.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Explanation
2+
- **Delegation** is a design pattern where an object delegates responsibility for certain tasks to another object. Instead of doing the work itself, the delegating object asks another object (delegate) to perform the work.
3+
- **"Has-A" relationship**: The delegating object "has a" reference to the delegating object and uses it to delegate responsibilities.
4+
- **Use Case**: Useful when you want to separate concerns and avoid making a class too complex.
5+
-
6+
- # Steps
7+
- Create a class that performs the main task (Delegate).
8+
-
9+
- Create a class that delegates tasks to the delegate class.
10+
-
11+
- The delegator calls methods on the delegate class to perform specific tasks.
12+
-
13+
- ```python
14+
class Printer:
15+
def print(self):
16+
print("Printing document...")
17+
18+
class Computer:
19+
def __init__(self, printer):
20+
self.printer = printer # Delegating print functionality
21+
22+
def print_document(self):
23+
self.printer.print() # Delegates the printing task
24+
25+
printer = Printer()
26+
computer = Computer(printer)
27+
computer.print_document() # Output: Printing document...
28+
```

0 commit comments

Comments
 (0)