diff --git a/Chapter_1-strings-arrays-and-strings/1.2.py b/Chapter_1-strings-arrays-and-strings/1.2.py index 8698dfb..4d971d8 100644 --- a/Chapter_1-strings-arrays-and-strings/1.2.py +++ b/Chapter_1-strings-arrays-and-strings/1.2.py @@ -44,9 +44,7 @@ def isPermutation2(string1,string2): chars[ord(string2[i])]-=1 if chars[ord(string2[i])] < 0: # this will capture all cases since we already ruled out diff length strings return false - - - + ####### OUR IMPLEMENTATION 2 ENDS HERE ############## @@ -57,7 +55,6 @@ def isPermutation2(string1_,string2_): ####### OUR IMPLEMENTATION 2 ENDS HERE ############## - # Driver Code string1 = "this is a string" string2 = "is this a string" diff --git a/Chapter_1-strings-arrays-and-strings/1.3.py b/Chapter_1-strings-arrays-and-strings/1.3.py index 7c13219..e4906d2 100644 --- a/Chapter_1-strings-arrays-and-strings/1.3.py +++ b/Chapter_1-strings-arrays-and-strings/1.3.py @@ -11,7 +11,6 @@ ''' - ####### OUR IMPLEMENTATION 1 BEGINS HERE ############## def URLify1(string): str_arr = string.split() # split the string by spaces @@ -25,9 +24,6 @@ def URLify1(string): ####### OUR IMPLEMENTATION 1 ENDS HERE ############## - - - #Driver code string = "this string has a couple of spaces" URLify(string) diff --git a/Chapter_1-strings-arrays-and-strings/1.4.py b/Chapter_1-strings-arrays-and-strings/1.4.py index 6b565b5..776c321 100644 --- a/Chapter_1-strings-arrays-and-strings/1.4.py +++ b/Chapter_1-strings-arrays-and-strings/1.4.py @@ -48,14 +48,12 @@ def checkMaxOneOdd(arr): return False oddPresent = True return True - - + ####### OUR IMPLEMENTATION 1 ENDS HERE ############## - #Driver Code string ="Tact Cxoa" -print(isPalindromePerm(string)) \ No newline at end of file +print(isPalindromePerm(string)) diff --git a/Chapter_1-strings-arrays-and-strings/1.5.py b/Chapter_1-strings-arrays-and-strings/1.5.py index 5589f94..0de4b1b 100644 --- a/Chapter_1-strings-arrays-and-strings/1.5.py +++ b/Chapter_1-strings-arrays-and-strings/1.5.py @@ -35,8 +35,7 @@ def isOneAway(str_1,str_2): oddCount+=1 if oddCount>2: return False - return True - + return True ####### OUR IMPLEMENTATION 1 ENDS HERE ############## diff --git a/Chapter_1-strings-arrays-and-strings/1.6.py b/Chapter_1-strings-arrays-and-strings/1.6.py index 11ea237..265bc0c 100644 --- a/Chapter_1-strings-arrays-and-strings/1.6.py +++ b/Chapter_1-strings-arrays-and-strings/1.6.py @@ -31,8 +31,6 @@ def compress(string): ####### OUR IMPLEMENTATION 1 ENDS HERE ############## - - #Driver Code string = 'aabcccccaaa' compress(string) diff --git a/Chapter_2-LinkedLists/2.1.py b/Chapter_2-LinkedLists/2.1.py index 0618ead..0948643 100644 --- a/Chapter_2-LinkedLists/2.1.py +++ b/Chapter_2-LinkedLists/2.1.py @@ -51,8 +51,7 @@ def deleteDuplicateNodes(LinkedList): curr_node = curr_node.next #print(dictionary) - ######### our implementation 2 ends here ########### - + ######### our implementation 2 ends here ########### # Driver code diff --git a/Chapter_2-LinkedLists/2.2.py b/Chapter_2-LinkedLists/2.2.py index 3960e24..7669792 100644 --- a/Chapter_2-LinkedLists/2.2.py +++ b/Chapter_2-LinkedLists/2.2.py @@ -38,4 +38,4 @@ def return_Kth_to_last_element(LinkedList,K): LL.append(4) LL.append(5) -return_Kth_to_last_element(LL,3) \ No newline at end of file +return_Kth_to_last_element(LL,3) diff --git a/Chapter_2-LinkedLists/2.3.py b/Chapter_2-LinkedLists/2.3.py index aa0c2b4..babafd1 100644 --- a/Chapter_2-LinkedLists/2.3.py +++ b/Chapter_2-LinkedLists/2.3.py @@ -50,4 +50,4 @@ def delete_middle_node(middle_node): mid_node = return_Kth_node(LL,2) delete_middle_node(mid_node) -LL.printList() \ No newline at end of file +LL.printList() diff --git a/Chapter_2-LinkedLists/2.4.py b/Chapter_2-LinkedLists/2.4.py index 686a236..9d5f2e4 100644 --- a/Chapter_2-LinkedLists/2.4.py +++ b/Chapter_2-LinkedLists/2.4.py @@ -38,7 +38,6 @@ def partition_linked_list_aroud_pivot(LinkedList,x): tail.next = None return head - ######### our implementation ends here ########### @@ -55,4 +54,4 @@ def partition_linked_list_aroud_pivot(LinkedList,x): new_head = partition_linked_list_aroud_pivot(LL,4) LL.head = new_head -LL.printList() \ No newline at end of file +LL.printList() diff --git a/Chapter_2-LinkedLists/2.5.py b/Chapter_2-LinkedLists/2.5.py index 2a816b1..609454d 100644 --- a/Chapter_2-LinkedLists/2.5.py +++ b/Chapter_2-LinkedLists/2.5.py @@ -41,7 +41,6 @@ def sum_lists_backwards(linkedlist1, linkedlist2): return int(string1)+int(string2) - ######### our implementation ends here ########### ''' @@ -86,7 +85,6 @@ def sum_lists_forwards(linkedlist1, linkedlist2): ######### our implementation ends here ########### - # Driver code LL1 = LinkedList() diff --git a/Chapter_3-Queues-and_stacks/3.2.py b/Chapter_3-Queues-and_stacks/3.2.py index 2b8a5e7..3772127 100644 --- a/Chapter_3-Queues-and_stacks/3.2.py +++ b/Chapter_3-Queues-and_stacks/3.2.py @@ -4,7 +4,6 @@ How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time. - pop(): Remove top item from stack push(): Add item to top of stack peek(): Return the top of the stack @@ -32,7 +31,6 @@ def push(self,item): self.min = min(self.min,item) self.min_at_each_step.append(self.min) self.stack.append(item) - def pop(self): @@ -60,7 +58,6 @@ def isEmpty(self): ######### our implementation ends here ########### - # Driver code s = Stack_withMin() @@ -74,4 +71,4 @@ def isEmpty(self): s.peek_min() s.pop() s.pop() -s.peek_min() \ No newline at end of file +s.peek_min() diff --git a/Chapter_3-Queues-and_stacks/3.6.py b/Chapter_3-Queues-and_stacks/3.6.py index 4433b02..d11e8e9 100644 --- a/Chapter_3-Queues-and_stacks/3.6.py +++ b/Chapter_3-Queues-and_stacks/3.6.py @@ -96,8 +96,6 @@ def enqueue(self,name,type): self.queue_cat.append(new_animal) else: self.queue_dog.append(new_animal) - - def dequeueAny(self): @@ -109,8 +107,7 @@ def dequeueAny(self): return self.dequeueDog() else: return self.dequeueCat() - - + def dequeueDog(self): if len(self.queue_dog)==0: @@ -124,8 +121,6 @@ def dequeueCat(self): ######### our implementation ends here ########### - - # Driver code AS= AnimalShelter2() @@ -138,4 +133,4 @@ def dequeueCat(self): AS.enqueue('Lillt','cat') AS.dequeueAny() -AS.dequeueCat() \ No newline at end of file +AS.dequeueCat() diff --git a/Chapter_3-Queues-and_stacks/queue_implementation_array.py b/Chapter_3-Queues-and_stacks/queue_implementation_array.py index 0cc38dc..9dbead6 100644 --- a/Chapter_3-Queues-and_stacks/queue_implementation_array.py +++ b/Chapter_3-Queues-and_stacks/queue_implementation_array.py @@ -41,4 +41,4 @@ def isEmpty(self): q.add(2) q.peek() q.add(3) -q.remove() \ No newline at end of file +q.remove() diff --git a/Chapter_3-Queues-and_stacks/queue_implementation_linkedlist.py b/Chapter_3-Queues-and_stacks/queue_implementation_linkedlist.py index e14a856..079732d 100644 --- a/Chapter_3-Queues-and_stacks/queue_implementation_linkedlist.py +++ b/Chapter_3-Queues-and_stacks/queue_implementation_linkedlist.py @@ -52,4 +52,4 @@ def isEmpty(self): q.remove() q.peek() q.remove() -q.isEmpty() \ No newline at end of file +q.isEmpty() diff --git a/Chapter_3-Queues-and_stacks/stack_implementation_array.py b/Chapter_3-Queues-and_stacks/stack_implementation_array.py index 149807c..d89def0 100644 --- a/Chapter_3-Queues-and_stacks/stack_implementation_array.py +++ b/Chapter_3-Queues-and_stacks/stack_implementation_array.py @@ -46,5 +46,6 @@ def isEmpty(): stack.push(2) stack.peek() stack.pop() + \ No newline at end of file diff --git a/Chapter_3-Queues-and_stacks/stack_implementation_linkedlist.py b/Chapter_3-Queues-and_stacks/stack_implementation_linkedlist.py index c2b5ed9..96454e0 100644 --- a/Chapter_3-Queues-and_stacks/stack_implementation_linkedlist.py +++ b/Chapter_3-Queues-and_stacks/stack_implementation_linkedlist.py @@ -47,7 +47,6 @@ def isEmpty(self): ######### our implementation ends here ########### - # Driver code stacker = MyStack_LinkedList() @@ -56,4 +55,4 @@ def isEmpty(self): stacker.push(3) stacker.peek() stacker.pop() -stacker.peek() \ No newline at end of file +stacker.peek() diff --git a/Chapter_4-trees-and-graphs/4.1.ipynb b/Chapter_4-trees-and-graphs/4.1.ipynb index 19e5820..89c5baf 100644 --- a/Chapter_4-trees-and-graphs/4.1.ipynb +++ b/Chapter_4-trees-and-graphs/4.1.ipynb @@ -54,7 +54,6 @@ "\n", "######### our implementation starts here ###########\n", "\n", - "\n", "def search(graph_,start_node,end_node):\n", " \n", " #if the start and end are the same, then there is a path\n", @@ -92,13 +91,10 @@ " \n", " #otherwise add it to the queue\n", " queue.append(adj_node)\n", - " \n", - " \n", + " \n", " return False\n", + " \n", " \n", - " \n", - " \n", - "\n", "######### our implementation ends here ############\n", "\n", "\n", diff --git a/Chapter_4-trees-and-graphs/4.1.py b/Chapter_4-trees-and-graphs/4.1.py index 4bc0e4f..697a2d0 100644 --- a/Chapter_4-trees-and-graphs/4.1.py +++ b/Chapter_4-trees-and-graphs/4.1.py @@ -76,21 +76,13 @@ def search(graph_,start_node,end_node): return False - - - + ######### our implementation ends here ############ - - - - - # Driver code - # Create a graph using the class Graph above g = Graph() @@ -103,6 +95,5 @@ def search(graph_,start_node,end_node): g.addEdge(3, 3) - # check if a route exists -search(g,2,3) \ No newline at end of file +search(g,2,3) diff --git a/Chapter_4-trees-and-graphs/4.2.py b/Chapter_4-trees-and-graphs/4.2.py index bf083be..711fb3a 100644 --- a/Chapter_4-trees-and-graphs/4.2.py +++ b/Chapter_4-trees-and-graphs/4.2.py @@ -48,8 +48,7 @@ def createMIN_BST_Helper(array,start,end): node.right = createMIN_BST_Helper(array,midpoint+1,end) return node - - + #print(len(array)//2) #node = Node(3) @@ -69,4 +68,4 @@ def createMIN_BST_Helper(array,start,end): MIN_bst = createMIN_BST(array) #print(array) -#print(bst.left.right.right.data)=12 \ No newline at end of file +#print(bst.left.right.right.data)=12 diff --git a/Chapter_4-trees-and-graphs/4.4.py b/Chapter_4-trees-and-graphs/4.4.py index a26503e..3049afb 100644 --- a/Chapter_4-trees-and-graphs/4.4.py +++ b/Chapter_4-trees-and-graphs/4.4.py @@ -57,9 +57,7 @@ def get_length(node): def check_balanced_2(rootnode): return (True if get_length_2(rootnode)!="ERROR_CODE" else False) #false if error code, true otherwise - - - + def get_length_2(node): if node is None: @@ -92,7 +90,6 @@ def get_length_2(node): lright = right.left = Node(6) rright = right.right = Node(7) - # tree above is balanced # uncomment code below to create unbalanced tree @@ -102,4 +99,4 @@ def get_length_2(node): check_balanced_2(rootNode) #True -check_balanced(rootNode) #True \ No newline at end of file +check_balanced(rootNode) #True diff --git a/README.md b/README.md index 5df6806..faf6b6a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # Cracking_the_coding_interview_python +-------------------------------------------------------------------------------------------------------------------------------------------- Python implementation of cracking the coding interview questions