This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findDivergePointForOfTwoLinkedLists(list1, list2): | |
# Assuming the list class has a method length to give the length of the list | |
lengthList1 = list1.length() | |
lengthList2 = list2.length() | |
differnce = lengthList2 - lengthList1 | |
if(lengthList2>=lengthList1): | |
while(differnce!=0): | |
list2 = list2.next | |
differnce -= 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#method to check for anagrams within a set | |
from operator import itemgetter | |
def checkForAnagrams(input): | |
temp = dict() | |
#Sort the input values individually | |
for k,v in input.items(): | |
temp[k]=''.join(sorted(v)) | |
#Now sort the entire list based on the value of the key | |
temp2 = sorted(temp.items(),key = itemgetter(1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#max heap construction | |
def heapify(items,idx): | |
left = leftChild(idx) | |
right = rightChild(idx) | |
largest = -1 | |
if(left < len(items) and items[left] > items[idx]): | |
largest = left | |
else: | |
largest = idx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
1 | |
2 3 | |
4 5 6 7 | |
should print 1,2,4,5,6,7,3,1 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findLCA(node,value1,value2): | |
if(node == None): | |
return None | |
#move to right if values are greater | |
if(node.value < value1 and node.value < value2): | |
return findLCA(node.right,value1, value2) | |
#move to left if values are smaller | |
if(node.value > value1 and node.value > value2): | |
return findLCA(node.left,value1, value2) | |
# Whereever it diverges, return the node as its the LCA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://www.quora.com/Whats-a-math-trick-that-is-not-very-well-known/answers/4234574 | |
import sys | |
def verifySquaringTheorem(n): | |
for i in range(25,n): | |
nSquaredSupposed = (( i - 25 ) * 100) + pow(i-50,2) | |
actualSquare = pow(i,2); | |
if nSquaredSupposed == actualSquare: | |
print ("Pass for n = " + str(i) + " with value " + str(nSquaredSupposed)) | |
else: | |
print("Fail for n = " + str(i) + "with value " + str(nSquaredSupposed) + "and actualSquare value = " + str(actualSquare)) |
NewerOlder