Problem : Function that takes a head node and an integer value n and then returns the nth to last node in the linked list. Solution : class Node: def __init__(self, value): self.value = value self.nextnode = None def nth_to_last_node(n, head): node = head nodes = [] while node != None: nodes.append(node) node = node.nextnode return nodes[-n]

# Author: Vivek

# Recursive Binary Search | Python

Implement recursive binary search in Python def recursive_binary_search(arr,ele): if len(arr) == 0: return False else: mid = len(arr)/2 if arr[mid] == ele: return True else: if ele < arr[mid]: return recursive_binary_search(arr[:mid], ele) else: return recursive_binary_search(arr[mid+1:], ele) d = recursive_binary_search([1,2,3,4,5],22) print d

# Binary Search | Python

Binary Search implementation in Python def binary_search(arr,ele): """ binary_search works only on sorted array, also it follows divide and conquer fundamentals """ first = 0 last = len(arr) - 1 # as indexing starts at 0 found = False while first <= last and not found: mid = (first+last)/2 # to get mid point of … Continue reading Binary Search | Python

# Reverse Linked List | Python

class Node(object): def __init__(self,value): self.value = value self.nextnode = None def reverse(head): # Set up current,previous, and next nodes current = head previous = None nextnode = None # until we have gone through to the end of the list while current: nextnode = current.nextnode # Reverse the pointer ot the next_node current.nextnode = previous … Continue reading Reverse Linked List | Python

# Doubly Linked List | Python

Implementation of Doubly Linked List in Python class DoublyLinkedList(object): def __init__(self,val): self.value = val self.next_node = None self.prev_node = None a = DoublyLinkedList(1) b = DoublyLinkedList(2) c = DoublyLinkedList(3) a.next_node = b b.prev_node = a b.next_node = c c.prev_node = b

# Singly Linked Lists | Python

Implementation of singly linked lists in Python. class Node(object): def __init__(self,val): self.value = val self.nextNode = Node a = Node(1) b = Node(2) c = Node(3) a.nextNode = b b.nextNode = c print a.nextNode.value

# Array Pair Sum | Python

Problem Given an integer array, output all the unique pairs that sum up to a specific value k. So the input: pair_sum([1,3,2,2],4) would return 2 pairs: (1,3) (2,2) Solution def get_unique_pair(list_of_elems,k): if len(list_of_elems) < 2: print "Please increase data" else: seen = set() output = set() for num in list_of_elems: target = k - num … Continue reading Array Pair Sum | Python