Algorithms-Reference

Python Basics

Dictionaries

Sorting a Dictionary with Key or Value

Sort by Key

result = OrderedDict(sorted(unsorted.items(), key= lambda x:x[0]))

Sort by Value

result = OrderedDict(sorted(unsorted.items(), key= lambda x:x[1]))

Get items in Sorted Order

result = [unsorted[key] for key in sorted(unsorted.keys())]

Ordered Dict

import collections
orderes_dict = collections.OrderedDict() 

Can also take existing dictionary (won’t order the dictionary by itself)

Default Dict

import collections
dictionary = collections.defaultdict(set) # Replace 'set' with any data structure of choice

Creates a dictionary with default value(data-type). eg. list, set, dict, etc
It is accessed as a normal dictionary

Dictionary Operations

Delete a key in dictionary

del d[key]  # d is the name of dictionary

Heaps

Inbuilt heap data structure is a min-heap i.e. it returns smallest item of the list

Import the built-in heap using

import heapq

Create a heap from list

heapq.heapify([])

Insert an item into the heap

heapq.heappush([],item)

Return the smallest item from the heap

heapq.heappop([])

Insert an element into heap and return the smallest element

heapq.heappushpop([],item)

To convert the heap into max-heap (return the maximum element on pop(), just convert all the values into -ve while inserting

SET

Add

.add(x)
It adds the element to the set and returns ‘None’.

Remove

.remove(x)
This operation removes element x from the set.
If element does not exist, it raises a KeyError.
The .remove(x) operation returns ‘None’.

Discard

.discard(x)
This operation also removes element x from the set.
If element does not exist, it does not raise a KeyError.
The .discard(x) operation returns ‘None’.

Pop

.pop()
This operation removes and return an arbitrary element from the set.
If there are no elements to remove, it raises a KeyError.

Union

.union()
The .union() operator returns the union of a set and the set of elements in an iterable.
Sometimes, the | operator is used in place of .union() operator, but it operates only on the set of elements in set.
Set is immutable to the .union() operation (or | operation).
Usage:

ans= s.union(y)
# Returns a set which is union of 's' and 'y'

Intersection

.intersection() The .intersection() operator returns the intersection of a set and the set of elements in an iterable.
Sometimes, the & operator is used in place of the .intersection() operator, but it only operates on the set of elements in set.
The set is immutable to the .intersection() operation (or & operation).
Usage:

ans= s.intersection(y)
# Returns a set which is intersection of 's' and 'y'

Difference

.difference() The tool .difference() returns a set with all the elements from the set that are not in an iterable.
Sometimes the - operator is used in place of the .difference() tool, but it only operates on the set of elements in set.
Set is immutable to the .difference() operation (or the - operation).
Usage:

ans= s.difference(y)
# Returns a set which has all the elements of 's' which are not in 'y'

Symmetric Difference

.symmetric_difference() The .symmetric_difference() operator returns a set with all the elements that are in the set and the iterable but not both.
Sometimes, a ^ operator is used in place of the .symmetric_difference() tool, but it only operates on the set of elements in set.
The set is immutable to the .symmetric_difference() operation (or ^ operation).
Usage:

ans= s.symmetric_difference(y)
# Returns a set which has all the elements of 's' and 'y' without the common elements

Mutations

Update

.update() or |=
Update the set by adding elements from an iterable/another set.

intersection_update()

.intersection_update() or &=
Update the set by keeping only the elements found in it and an iterable/another set.

difference_update()

.difference_update() or -=
Update the set by removing elements found in an iterable/another set.

symmetric_difference_update()

.symmetric_difference_update() or ^=
Update the set by only keeping the elements found in either set, but not in both.

Regex

Import regex library using

import re

Compile Regex

regex = re.compile('your_regex_here')

Replace using Regex

import re 
regex = re.compile('your_regex_here')
string = regex.sub("replace_with_this", your_string)

Manipulation In Python

(0+1)/2
0.5
(0+1)//2
0