Skip to content

Instantly share code, notes, and snippets.

View jatinsharrma's full-sized avatar

Jatin Sharma jatinsharrma

  • India
View GitHub Profile
'''
Recursion Function
For 3 disk (Here S= Source, D= Destination,T= Temp)
3[S,D,T]
/ | \
/ | \
2[S,T,D] 1[S,D,T] 2[T,D,S]
/ | \ / | \
/ | \ 1[T,S,D] 1[T,D,S] 1[S,D,T]
#-------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------INSERTION SORT-------------------------------------------------------------
#-------------------------------------------------------------------------------------------------------------------------
def insertion_sort(unsorted_list):
i = len(unsorted_list)
for num in range(i):
for num1 in range(num):
if unsorted_list[num] < unsorted_list[num1]:
'''-------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------MERGE SORT-------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------'''
def merge_sort(unsorted_list):
if len(unsorted_list) == 1:
return unsorted_list
mid = len(unsorted_list)//2
@jatinsharrma
jatinsharrma / designerPDF.py
Last active February 16, 2020 11:12
Designer PDF Viewer (Hackerrank)
#Find question here: https://www.hackerrank.com/challenges/designer-pdf-viewer/problem?h_r=internal-search
def designerPdfViewer(h, word):
big = 0
l_st = list(set(list(word)))
for ch in l_st:
val = ord(ch) - 97
if h[val] > big:
big = h[val]
return len(word) * big
@jatinsharrma
jatinsharrma / organizingBalls.py
Created February 16, 2020 08:17
Organizing Containers of Balls (Hackerrank)
#For question refer : https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem
''' logic
Suppose there are two container A and B and there are two type of balls in each container (qunatity may varry)
Type0 Type 1
A contains [ 1 , 1 ]
B contains [ 1 , 1 ]
First i am calculating total no of balls of each type.
In above example Type0 balls are 2 and Type1 balls are 2
@jatinsharrma
jatinsharrma / StrangeCounter.py
Created February 16, 2020 10:23
Strange Counter
#For question refer: https://www.hackerrank.com/challenges/strange-code/problem
def strangeCounter(t):
ti = 1
val = 3
while True:
new_ti = ti+val
new_val = 2*val
if t in range(ti, new_ti):
temp = t - ti
@jatinsharrma
jatinsharrma / AngryProfessor.py
Created February 16, 2020 10:59
Angry Professor (Hakerrank)
# for question refer: https://www.hackerrank.com/challenges/angry-professor/problem
def angryProfessor(a, k):
count = 0
for stud in a:
if stud < 1:
count += 1
if count >= k:
return "NO"
else:
return "YES"
@jatinsharrma
jatinsharrma / Biinary_Search.py
Created March 26, 2020 09:26
Binary Search (Recursive & Iterative)
#------------Recursive Solution----------------
def BinarySearch(array,target):
return BinarySearchHelper(array,target,0, len(array)-1)
def BinarySearchHelper(array, target, left, right):
middle = (right + left)//2
if left > right:
return -1
@jatinsharrma
jatinsharrma / FibanocciSeries.Py
Created March 28, 2020 04:02
Fibonacci Series: Iterative / Recursive / Dynamic Programming
print("//////////////////////////////////////////////////////////////////")
print("-------------------Fibanocci Series--------------------------------")
print("///////////////////////////////////////////////////////////////////\n\n\n")
print("-------------------Itertaive Solution------------------------------\n\n")
print("-------------------Using List--------------------------------------\n")
def fibonacciSeries1(n):
series = [0,1]
i = 2
@jatinsharrma
jatinsharrma / BST.py
Last active April 18, 2020 21:43
Binary Search Tree
# Uncomment code if you want to see what is actually going inside
'''
Documentation
(I Have numbered the steps in each method if you are not understanding any step refer to the number)
a) remove() and remove1()
1. Stores the target node
2. Stores the previous node to the target node
3. Help determining where to append replacement's node after removing target node
4. This loop spit out target node and previous node to the target node
5. If node becomes None that means target node is not in the tree so retune false