Skip to content

Commit 05c7204

Browse files
committed
Alignment
1 parent 8514e3c commit 05c7204

File tree

10 files changed

+252
-72
lines changed

10 files changed

+252
-72
lines changed

DSA/Array.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Array:
2-
# An array is a type of linear data structure
3-
# that is defined as a collection of elements with same or different data types.
4-
# They exist in both single dimension and multiple dimensions.
5-
# These data structures come into picture when there is a necessity
2+
# An array is a type of linear data structure
3+
# that is defined as a collection of elements with same or different data types.
4+
# They exist in both single dimension and multiple dimensions.
5+
# These data structures come into picture when there is a necessity
66
# to store multiple elements of similar nature together(data type) at one place.
77

8-
# The difference between an array index and a memory address
9-
# array index acts like a key value to label the elements in the array.
8+
# The difference between an array index and a memory address
9+
# array index acts like a key value to label the elements in the array.
1010
# a memory address is the starting address of free memory available.
1111

1212
# Following are the important terms to understand the concept of Array.
@@ -15,20 +15,20 @@
1515
# Index − Each location of an element in an array has a numerical index, which is used to identify the element.
1616

1717
# An array is a collection of items stored at contiguous memory locations.
18-
# The idea is to store multiple items of the same type together.
19-
# This makes it easier to calculate the position of each element by simply adding an offset to a base value,
18+
# The idea is to store multiple items of the same type together.
19+
# This makes it easier to calculate the position of each element by simply adding an offset to a base value,
2020
# i.e., the memory location of the first element of the array (generally denoted by the name of the array).
2121

2222
# Creating an Array
23-
# Array in Python can be created by importing an array module.
24-
# array(data_type, value_list) is used to create an array with data type and value list specified in its arguments.
23+
# Array in Python can be created by importing an array module.
24+
# array(data_type, value_list) is used to create an array with data type and value list specified in its arguments.
2525

2626
# There are thre ways to import array module:
2727
# Using import (import array)
2828
# Using import and call alias name (import array as arr)
2929
# from modules import all methods ( from array import * )
3030

31-
# Some of the data types are mentioned below which will help in creating an array of different data types.
31+
# Some of the data types are mentioned below which will help in creating an array of different data types.
3232
# i - signed Int - allow positive and negative values
3333
# I - unsigned Int - allow only positive values
3434
# d - double - allow double values
@@ -40,13 +40,13 @@
4040

4141
from array import *
4242

43-
a=array('i',[-1,2,3,4,5])
43+
a = array("i", [-1, 2, 3, 4, 5])
4444

45-
print(a) # array('i', [1, 2, 3, 4, 5])
45+
print(a) # array('i', [1, 2, 3, 4, 5])
4646

47-
a=array('I',[1,2,3,4,5])
47+
a = array("I", [1, 2, 3, 4, 5])
4848

49-
print(a) # array('I', [1, 2, 3, 4, 5])
49+
print(a) # array('I', [1, 2, 3, 4, 5])
5050

5151
# a=array('i',[1,2,3,4,5,'a'])
5252

@@ -56,21 +56,21 @@
5656

5757
# print(a) OverflowError: can't convert negative value to unsigned int
5858

59-
a=array('u',['a','b','c'])
59+
a = array("u", ["a", "b", "c"])
6060

61-
print(a) # array('u', 'abc')
61+
print(a) # array('u', 'abc')
6262

63-
a=array('b',[-1,2,3,3]) # array('b', [-1, 2, 3, 3])
63+
a = array("b", [-1, 2, 3, 3]) # array('b', [-1, 2, 3, 3])
6464

65-
print(a) #TypeError: 'str' object cannot be interpreted as an integer
65+
print(a) # TypeError: 'str' object cannot be interpreted as an integer
6666

67-
a=array('f',[-1,2,3,3.0]) # array('f', [-1.0, 2.0, 3.0, 3.0])
67+
a = array("f", [-1, 2, 3, 3.0]) # array('f', [-1.0, 2.0, 3.0, 3.0])
6868

69-
print(a) # convert int to float
69+
print(a) # convert int to float
7070

71-
a=array('d',[-1,2,3,3.00500050]) # array('d', [-1.0, 2.0, 3.0, 3.0050005])
71+
a = array("d", [-1, 2, 3, 3.00500050]) # array('d', [-1.0, 2.0, 3.0, 3.0050005])
7272

73-
print(a) # convert int to double
73+
print(a) # convert int to double
7474

7575
# a=array('B',[-1,2,3])
7676

@@ -84,40 +84,42 @@
8484
# Seaching - Best O(1) got in first position, Average O(n-i) may got in middle ,Worst O(n) got at last postion
8585

8686

87-
print("------------------------------------------ Adding Element to a array ------------------------------------------")
87+
print(
88+
"------------------------------------------ Adding Element to a array ------------------------------------------"
89+
)
8890
# Adding Elements to a Array
89-
# Elements can be added to the Array by using built-in insert() function.
91+
# Elements can be added to the Array by using built-in insert() function.
9092

9193
# Insert():
92-
# Insert is used to insert one or more data elements into an array.
93-
# a new element can be added at the beginning, end, or any given index of array.
94+
# Insert is used to insert one or more data elements into an array.
95+
# a new element can be added at the beginning, end, or any given index of array.
9496

95-
# append():
96-
# append() is also used to add the value mentioned in its arguments at the end of the array.
97+
# append():
98+
# append() is also used to add the value mentioned in its arguments at the end of the array.
9799

98100

99101
import array as arr
100-
a = arr.array('i', [1, 2, 3])
102+
103+
a = arr.array("i", [1, 2, 3])
101104
print("Array before insertion : ", end=" ")
102105
for i in range(0, 3):
103-
print(a[i], end=" ")
106+
print(a[i], end=" ")
104107
print()
105108

106109
a.insert(1, 4)
107110
print("Array after insertion : ", end=" ")
108-
for i in (a):
109-
print(i, end=" ")
111+
for i in a:
112+
print(i, end=" ")
110113
print()
111114

112-
b = arr.array('d', [2.5, 3.2, 3.3])
115+
b = arr.array("d", [2.5, 3.2, 3.3])
113116
print("Array before insertion : ", end=" ")
114117
for i in range(0, 3):
115-
print(b[i], end=" ")
118+
print(b[i], end=" ")
116119
print()
117120

118121
b.append(4.4)
119122
print("Array after insertion : ", end=" ")
120-
for i in (b):
121-
print(i, end=" ")
123+
for i in b:
124+
print(i, end=" ")
122125
print()
123-

DSA/Arrays/array.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Array
2+
# Arrays are not native data structures in Python.
3+
# Instead, we use list, which are similar but more flexible.
4+
5+
# However, there are models available in Python
6+
# for creating more efficient arrays with a specific data
7+
# type, mainly for numeric data type.
8+
9+
# We can use numpy module and array module to create an array data structure in Python.
10+
11+
# They store elements as contiguous block of memory without pointers,
12+
# reducing memory overhead.
13+
14+
# array objects can only store elements of same data type.
15+
16+
# The simple examples of array data structures:
17+
# collections of books,
18+
# clothes in wardrobe or
19+
# pens in your pen stand.
20+
21+
# Real world Examples of Array
22+
# box of macarons.
23+
24+
# So this is very similar to the array data structure.
25+
26+
# If you look at this box very carefully,
27+
# we can easily see what are the properties of this box.
28+
29+
# First is it's a box of macarons which holds macarons in it.
30+
31+
# This means that this box is produced for macarons.
32+
33+
# You cannot store,
34+
# for example,
35+
36+
# 1. this cream puff into this box because it's not going to fit in.
37+
38+
# 2. The next property is that all macarons in this box are next to each other.
39+
40+
# 3. There is no gap between them, which means that they are contiguous.
41+
42+
# 4.And the next property is that each macaroon here can be identified
43+
# uniquely based on their location.
44+
45+
# So this is the first macaroon.This is the second.This is the third.
46+
# And it continues like this.
47+
48+
# So based on their location, we can identify them.
49+
50+
# So these are the common properties of this box and can be seen very easily.
51+
52+
# If you compare this box of macarons with the array data structure,
53+
# we can see that they are very similar.
54+
55+
# So a typical array structure looks like this.
56+
57+
# You can easily see that the appearance of the macarons box and
58+
# array data structure somehow looks each other.
59+
60+
## properties of this array
61+
62+
# The first property is an array can store data of specified type.
63+
64+
# This means that when we create an array of integers,
65+
# we cannot store string values in the array.
66+
67+
# As you see, in case of macaron box,
68+
# we cannot store any other suite which will not fit in this box.
69+
70+
# So here the data type is macaroon. We cannot store this creampuff in this array.
71+
72+
# The data type is integer, so we cannot store the string.
73+
74+
# Now the next property is elements of array are located in a contiguous location in memory.
75+
76+
# This means that the elements are located next to each other.
77+
78+
# There is no gap between them, so they have to be located contiguously in the memory.
79+
80+
# Now the next property is each element of array has a unique index.
81+
82+
# For example, the index of ten over here is going to be two and the index of four is going to be zero.
83+
84+
# And keep in mind that the first element's index starts from zero and increase sequentially.
85+
86+
# So these are the main properties of the array data structure and we have compared it with the real life
87+
88+
# example to understand it very well.
89+
90+
# why do we need array data structure?
91+
92+
# So consider a situation where we need to create three integer variables.
93+
# So if we create them, we can name them like this.
94+
95+
# Number one, Number two, Number three.
96+
97+
# Now, this is very simple because we have to store just three integer numbers.
98+
99+
# Now let's assume we have to store 500 integer numbers.
100+
101+
# Are we going to use 500 variables? Of course not.
102+
103+
# To handle such situations, all programming languages provide a concept called array.
104+
105+
# An array is a data structure which can store collection of elements of the same type.
106+
107+
# So array is used to store a collection of data, but it is more often useful to think of array as a
108+
109+
# collection of variables of the same type.
110+
111+
# So instead of declaring individual 500 variables, we are just going to create an array and we are going
112+
113+
# to access the elements of array as we need them.
114+
115+
116+
# What are arrays?
117+
# An array is a container which can hold a fix number of items
118+
# and these items should be of the same type.
119+
# Each item stored in an array is called an element
120+
# and they can be of any type including integers, floats, strings, etc.
121+
122+
# These elements are stored at contiguous memory location.
123+
# Each location of an element in an array has a numerical index starting from 0.
124+
# These indices are used to identify and access the elements.
File renamed without changes.
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Insertion Sort:
2-
# Insertion sort is a very simple method to sort numbers in an ascending or descending order.
3-
# This method follows the incremental method.
2+
# Insertion sort is a very simple method to sort numbers in an ascending or descending order.
3+
# This method follows the incremental method.
44
# It can be compared with the technique how cards are sorted at the time of playing a game.
5-
# This is an in-place comparison-based sorting algorithm.
6-
# Here, a sub-list is maintained which is always sorted.
7-
# For example, the lower part of an array is maintained to be sorted.
8-
# An element which is to be 'inserted' in this sorted sub-list, has to find its appropriate place
5+
# This is an in-place comparison-based sorting algorithm.
6+
# Here, a sub-list is maintained which is always sorted.
7+
# For example, the lower part of an array is maintained to be sorted.
8+
# An element which is to be 'inserted' in this sorted sub-list, has to find its appropriate place
99
# and then it has to be inserted there. Hence the name, insertion sort.
1010

1111

@@ -22,23 +22,28 @@
2222

2323
# code:
2424
def insertionSort(arr):
25-
n = len(arr) # Get the length of the array
26-
27-
if n <= 1:
28-
return # If the array has 0 or 1 element, it is already sorted, so return
29-
30-
for i in range(1, n): # Iterate over the array starting from the second element
31-
key = arr[i] # Store the current element as the key to be inserted in the right position
32-
j = i-1
33-
while j >= 0 and key < arr[j]: # Move elements greater than key one position ahead
34-
arr[j+1] = arr[j] # Shift elements to the right
35-
j -= 1
36-
arr[j+1] = key # Insert the key in the correct position
25+
n = len(arr) # Get the length of the array
26+
27+
if n <= 1:
28+
return # If the array has 0 or 1 element, it is already sorted, so return
29+
30+
for i in range(1, n): # Iterate over the array starting from the second element
31+
key = arr[
32+
i
33+
] # Store the current element as the key to be inserted in the right position
34+
j = i - 1
35+
while (
36+
j >= 0 and key < arr[j]
37+
): # Move elements greater than key one position ahead
38+
arr[j + 1] = arr[j] # Shift elements to the right
39+
j -= 1
40+
arr[j + 1] = key # Insert the key in the correct position
41+
3742

3843
# Sorting the array [12, 11, 13, 5, 6] using insertionSort
3944
arr = [12, 11, 13, 5, 6]
4045
insertionSort(arr)
4146
print(arr)
4247

43-
# If the given numbers are sorted, this algorithm runs in O(n) time.
44-
# If the given numbers are in reverse order, the algorithm runs in O(n2) time.
48+
# If the given numbers are sorted, this algorithm runs in O(n) time.
49+
# If the given numbers are in reverse order, the algorithm runs in O(n2) time.

DSA/Sorting Algorithm/quick sort.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@
4343
# print(sorted_arr)
4444

4545

46+
def quick_sort(arr, low, high):
47+
if low < high:
48+
p = partition(arr, low, high)
49+
quick_sort(arr, low, p)
50+
quick_sort(arr, p + 1, high)
51+
52+
53+
def partition(arr, low, high):
54+
pivot = arr[(low + high) // 2]
55+
i = low - 1
56+
j = high + 1
57+
while True:
58+
i += 1
59+
while arr[i] < pivot:
60+
i += 1
61+
j -= 1
62+
while arr[j] > pivot:
63+
j -= 1
64+
if i >= j:
65+
return j
66+
arr[i], arr[j] = arr[j], arr[i]
67+
68+
4669
def quick_sort(arr):
4770
if len(arr) <= 1:
4871
return arr
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Higher Order Function

Notes/3.Advance/4.Decorators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Decorators in Python
2-
# Decorators are a very powerful and useful tool in Python
3-
# since it allows programmers to modify the behaviour of a function or class.
4-
# Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function,
2+
# Decorators are a very powerful and useful tool in Python
3+
# since it allows programmers to modify the behaviour of a function or class.
4+
# Decorators allow us to wrap another function in order
5+
# to extend the behaviour of the wrapped function,
56
# without permanently modifying it.
67

Notes/Data_type/List/1.List.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
# Allows Duplicate Members: A list can have two or more elements that are identical.
1616

17-
# Heterogeneous: Lists can contain elements of different types,
17+
# Heterogeneous: Lists can contain elements of different types,
1818
# although this is generally not recommended for most applications.
1919
print("-----------------------------------------------------------")
2020

2121

2222
# Creation of Lists object
23-
# You can create a list by enclosing its elements in square brackets [], separated by commas:
23+
# You can create a list by enclosing its elements in square brackets [],
24+
# separated by commas:
2425

2526
# 1.
2627
my_list = []

0 commit comments

Comments
 (0)