DATA STURCTURS AND ALGORITHMS
ARRAY DATA STRUCTURE
1) arr=[10,20,30,40] :Used to define an array
2) np.array(data, index, columns, datatype ) : Used to define an array
3) arr_name.append(element):It is used to append an element to the arra
4) arr_name.insert(index,element): It inserts the the given element at the the
given index.
5) arr_name.pop(index): It removes the element from the given index, and returns
the removed element. If index not specified the last element is removed.
5) arr_name.index(i): It returns the first index where the element i appears first
in the array.
6) arr_name.reverse(): It reverses the original array.
6) arr_name.count(i): It counts the number of times the element i appears in the
array
7) arr_name.extend(iterable): The iterable is another list, array or tuple whose
elements are appended to the original array.
8) arr_name.fromlist(list): The given list and array have the same types of
elements. The function appends the elements of the list to the given array.
9) arr_name.tolist(): It converts the given array to a list.
10) arr_name.itemsize: It is an attribute not a function so no parenthesis. It
retirns the number of bytes used by the items present on the array. eg-> integer
uses 4 bytes so it gives 4 for an array with integer values.
11) arr_name.typecode: It returns the datatype used in the array.
12) arr_name.buffer_info(): It provides data about the array.
STRINGS DATA STRUCTURE
1) len(string): It returns the number of characters in the string. Eg->
len("hello")=5
2) str(i): It converts the given value to string. Eg-> str(1234)="1234"
3) string.upper(): It converts the characters to uppercase. Eg-> "Hello
World".upper()="HELLO WORLD"
4) string.lower(): It converts the characters to lowercase.Eg-> "Hello
World".lower()="lower world"
5) string.capitalize(): It converts the first charter in string to uppercase and
rest to lowercase. Eg-> "Hello World".capitalize()="Hello world"
6) string.title(): Capitalize each word.Eg-> "Hello World".title()="Hello World"
7) string.swapcase(): It changes the lowercase to uppercase and vice-versa.Eg-
>"Hello World".swapcase()="hELLO wORLD"
8)string.isalpha(): It checks if all the characters in the string are letters if
yes it gives True else False. Eg-> "Hello123".isalpha()=False
9)string.isdigit(): It checks if all the characters in the string are digits if yes
it gives True else False. Eg-> "Hello123".isdigit()=False
10)string.isalnum(): It checks if all the characters in the string are letters and
digits if yes it gives True else False. Eg-> "Hello123".isalnum()=True
11)string.isspace():It checks if there is only white space in the string if yes it
gives True else False. Eg-> " ".isspace()=False
12)string.isupper(): It checks if all characters in the string are in uppercase if
yes it gives True else False. Eg->"HELLO".isupper()=True
13)string.islower(): It checks if all characters in the string are in uppercase if
yes it gives True else False. Eg->"hello".islower()=True
14)string.istitle(): It checks if all words in string have first first word in
uppercase. Eg-> "Hello".istitle()= True
15)s1.startswith(s2): It checks whether the string s1 starts with strings2 if yes
it gives True else False.
16)s1.endswith(s2): It checks whether the string s1 ends with string s2 if yes it
gives True else False.
17)string.strip(): Removes spaces from both ends. Eg-> " hello ".strip()="hello"
18)string.lstrip(): Removes spaces from left end.Eg->" hello ".lstrip()="hello "
19)string.rstrip(): Removes spaces from right end.Eg->" hello ".rstrip()=" hello "
20)string.split(delimiter): It splits the string elements and converts it into
list. Eg->"apple,grape".split(",")=["apple","grape"]
LIST DATA STRUCTURE
1)list=[1,2,3,4] : list of integers
2)list=["a","b","c"] : list of strings
3)list=[1,3.14,"a"] : mixed list
4)list=[[2,4],[3,6]] : nested list
5)len(list): It returns the number of elements in the list
6)max(list): It returns the element with the max. value in the list.
7)min(list): It returns the element with the min. value in the list.
8)sum(list): It returns the sum of the elements in the list.
9)sorted(list): It returns a new sorted list.
10)list(): It converts an iterable to a list
11)list.append(x): It appends the element x to the list
12)list.insert(index,x): It inserts the element x at the given index
13)list.extend(iterable): It appends all the elements of the iterable
14)list.remove(x): It removes the first occurrence of the element x from the list
15)list.pop(index): It removes the element from the given index
16)del list(index): deletes the element at the given index
17)list.clear(): It removes all the elements from the list
18)list.index(x): Returns the index at the first occurrence of element x.
19)list.count(x): It counts the number of appearances of element x.
20)list.sort(): It sorts the list
21)list.reverse(): It reverses the entire list
22)list.copy(): Returns a shallow copy of the list
STACK DATA STRUCTURE
1)stack=[]: Defining a stack using list
2)from collections import deque
stack=deque()
3)stack.append(element): It is used to append the the given element to the stack
4)stack.pop(): It is used to remove the the last element from the stack and returns
the removed element.
5)len(stack): It counts the number of elements in the stack
6)stack.clear(): It removes all the elements from the stack
QUEUE DATA STRUCTURE
1)from collections import deque #maxsize can be defined using maxlen function
q=deque()
2)queue=[] #no maxsize.
3)from queue import Queue #max size can be defined using maxsize function
q=Queue()
4)queue.append(element): Used to append elements to the queue.
5)queue.popleft(): It is used to remove and return the first element in the queue.
6)queue.appendleft(append): It is used to add an element in front of the first
element.
7)queue.pop(index): It is used to remove and return the element at the given index.
8)queue.clear(): It removes all the elements from the queue.
9)len(queue): It provides the number of elements in the queue.
10)queue[index]: It access the element at the given index.
11)queue.get(): It is similar to queue.popleft() it removes the first first
element. # Only applicable for Queue
12)queue.put(x): It is similar to queue.append(x) it append the elements at the end
of the queue.# Only applicable for Queue
13)queue.empty(): It checks whether queue is empty or not # Only applicable for
Queue
14)queue.full(): It checks whether queue is full or not # Only applicable for Queue
HEAP DATA STRUCTURE ->Heap is special Binary Tree that satisfies the Heap property.
Max-Heap: Parent is greater than or equal to its child.
Min-Heap: Parent is less than or equal to its child
In a heap represented in the form of a list if Parent Index: i, Left Child Index:
2i+1,Right Child Index: 2i+2
1)import heapq
heapq.heapify(list) : It converts the list into a heap
2)import heapq
heapq.heappush(heap, item) : It pushes an element in the heap while maintain
the heap property
3)import heapq
heapq.heappop(heap) : Removes and Returns the smallest element from the heap.
4)import heapq
heapq.heappushpop(heap,item): It pushes an item into the heap and then pops the
smallest element from the heap
5)import heapq
heapq.heapreplace(heap,item): Removes the smallest element and then pushes the
given item
6)import heapq
heapq.nlargest(n,heap): Returns the n largest elements from the iterable
7)import heapq
heapq.nsmallest(n,heap): Returns the n smallest elements from the iterable
DICTIONARY DATA STRCTURE
1)my_dict={"name":"Alice","age":25,"city":"New York"}
2)print(dict[key]), print(dict.get[key]) : Used to access the Values in the
dictionary for the given key
3)dict["wrting"]="good" : It is used to add extra values and keys in the dictionary
4)dict["age"]=50,dict"name"="deepak" :It is used to update the keay and values
5)dict.pop["city"], del my_dict["name"] : It is used to remove the data
6)len(dict) : It is used to find the number of key value pairs.Eg-
>print(len(my_dict))=3
7)dict.keys() : Its used to all the keys from the dictionary. Eg-
>print(dict.keys()) ->dict(["name","age","city"])
8)dict.values() : Its used to all the values from the dictionary.Eg-
>print(dict.values())->dict(["Alice",25,"New York"])
9)dict.items() : Its used to all the items from the dictionary.Eg-
>print(dict.keys()) ->dict([("name":"Alice"),("age":25),("city":New York")])
10)dict.update() : It is used to upate the list
MAIN DATA STRUCTURES LEFT: Binary Tree, Tree