Skip to content

Commit f245d8c

Browse files
committed
Python notes
1 parent cd4ee2c commit f245d8c

File tree

5 files changed

+77
-17
lines changed

5 files changed

+77
-17
lines changed

DSA/Searching Algorithm/Binary_search.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
def binarysearch(arr,target):
2-
low,high=0,len(arr)-1
3-
while low<=high:
4-
mid=(low+high)//2
5-
if arr[mid]==target:
1+
def binarysearch(arr, target):
2+
low, high = 0, len(arr) - 1
3+
while low <= high:
4+
mid = (low + high) // 2
5+
if arr[mid] == target:
66
return mid
7-
elif arr[mid]<target:
8-
low=mid+1
7+
elif arr[mid] < target:
8+
low = mid + 1
99
else:
10-
high=mid-1
10+
high = mid - 1
1111
return -1
1212

1313

14-
arr=[int(x) for x in input("Enter the value: ").split(",")]
15-
target=int(input("enter the target value: "))
16-
print(binarysearch(arr,target))
14+
arr = [int(x) for x in input("Enter the value: ").split(",")]
15+
target = int(input("enter the target value: "))
16+
print(binarysearch(arr, target))
1717

1818

1919
# Binary search
2020

21-
# Divide the search space into two halves by finding the middle index “mid”.
22-
# Compare the middle element of the search space with the key.
21+
# Divide the search space into two halves by finding the middle index “mid”.
22+
# Compare the middle element of the search space with the key.
2323
# If the key is found at middle element, the process is terminated.
2424
# If the key is not found at middle element, choose which half will be used as the next search space.
2525
# If the key is smaller than the middle element, then the left side is used for next search.
@@ -71,7 +71,7 @@ def binarysearch(arr,target):
7171
# else:
7272
# print("Element is not present in array")
7373

74-
# Time Complexity:
74+
# Time Complexity:
7575
# Best Case: O(1)
7676
# Average Case: O(log N)
7777
# Worst Case: O(log N)
@@ -104,10 +104,7 @@ def binarysearch(arr,target):
104104
# return -1
105105

106106

107-
108107
# arr=[int(x)for x in input('array').split(",")]
109108
# v=int(input())
110109
# result=search(arr,0,len(arr),v)
111110
# print(result)
112-
113-

DSA/Sorting Algorithm/quick sort.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ def quick_sort(arr):
7878

7979
arr = [700, 200, 400, 100, 90]
8080
print(quick_sort(arr))
81+
82+
83+
def quick_sort(arr):
84+
if len(arr) == 1:
85+
return arr
86+
pivot = arr[len(arr) // 2]
87+
left = [x for x in arr if x < pivot]
88+
pivot = [x for x in arr if x == pivot]
89+
right = [x for x in arr if x > pivot]
90+
return quick_sort(left) + pivot + quick_sort(right)

Notes/Multi_threading.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Multi-threading in Python:
2+
# In Python, multithreading allows you to run multiple threads concurrently within a single process,
3+
# which is also known as thread-based parallelism.
4+
# This means a program can perform multiple tasks at the same time,
5+
# enhancing its efficiency and responsiveness.
6+
7+
# Multithreading in Python is especially useful for multiple I/O-bound operations,
8+
# rather than for tasks that require heavy computation.
9+
10+
# Generally, a computer program sequentially executes the instructions,
11+
# from start to the end. Whereas,
12+
# Multithreading divides the main task into more than one sub-task and executes them in an overlapping manner.

Notes/decorator.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Decorators in Python
2+
# A decorator is a function that takes another function as an argument,
3+
# extends its behavior, and returns a new function.
4+
# Decorators are often used to add functionality to existing functions
5+
# without modifying their code.
6+
7+
8+
def decorator_function(original_function):
9+
def wrapper_function(*args, **kwargs):
10+
print("Wrapper executed this before {}".format(original_function.__name__))
11+
return original_function(*args, **kwargs)
12+
13+
return wrapper_function
14+
15+
16+
@decorator_function
17+
def display():
18+
print("Display function executed.")
19+
20+
21+
display()
22+
23+
24+
# example 2:
25+
def login_required(func):
26+
def wrapper(*args, **kwargs):
27+
if args[0] == "admin":
28+
return func(*args, **kwargs)
29+
else:
30+
return "Access Denied"
31+
32+
return wrapper
33+
34+
35+
@login_required
36+
def view_dashboard(user):
37+
return "Welcome to the dashboard!"
38+
39+
40+
print(view_dashboard("admin")) # Output: Welcome to the dashboard!
41+
print(view_dashboard("guest")) # Output: Access Denied

Notes/thread.py

Whitespace-only changes.

0 commit comments

Comments
 (0)