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
from random import randint | |
board = [] | |
for x in range(5): | |
board.append(["O"] * 5) | |
def print_board(board): | |
for row in board: | |
print " ".join(row) |
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
# This is written after refering the CLRS book and hints from the site http://en.literateprograms.org/Merge_sort_(Python) | |
#The merge method takes in the two subarrays and creates a output array | |
def merge(left, right): | |
result = [] | |
i , j = 0 , 0 | |
while i < len (left) and j < len (right): # iterate through both arrays and arrange the elements in sorted order | |
if left[i] <= right [j]: | |
result.append(left[i]) | |
i+=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
#First partition the input array into two groups | |
# Partitioning alogorithm creates smaller array with elements on left and | |
# right being smaller and greater than the pivot | |
def partiton(a, left, right): | |
i = left + 1 # Initialiase i to one more assuming pivot is at position left | |
pivot = a[left] | |
for j in range(left+1, right+1): #Iterate over the remaining array | |
if (a[j] < pivot): |
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
for (NSString *familyName in [UIFont familyNames]) { | |
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) { | |
NSLog(@"%@", fontName); | |
} | |
} |
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)) |
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
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 |
OlderNewer