Skip to content

Instantly share code, notes, and snippets.

@SahilKadam
SahilKadam / SparseArrays.py
Created October 23, 2016 18:45
There are strings. Each string's length is no more than characters. There are also queries. For each query, you are given a string, and you need to find out how many times this string occurred previously. Input Format The first line contains , the number of strings. The next lines each contain a string. The nd line contains , the number of queri…
N = int(input().strip())
def wordCount(word):
global words
res = [item for item in words if item == word]
return len(res)
words = [input().strip() for i in range(N)]
N_Q = int(input().strip())
query = [print (wordCount(input().strip())) for i in range(N_Q)]
@SahilKadam
SahilKadam / leftRotation.py
Created November 15, 2016 04:49
A left rotation operation on an array of size shifts each of the array's elements unit to the left. For example, if left rotations are performed on array , then the array would become . Given an array of integers and a number, , perform left rotations on the array. Then print the updated array as a single line of space-separated integers.
def array_left_rotation(a, n, k):
if k % n == 0:
return a
else:
return (a[k%n: ] + a[ :k%n])
n, k = map(int, input().strip().split(' '))
a = list(map(int, input().strip().split(' ')))
answer = array_left_rotation(a, n, k);
@SahilKadam
SahilKadam / Anagrams.py
Created November 15, 2016 17:43
Strings: Making Anagrams
def number_needed(a, b):
count = 0
if len(a) >= len(b):
for i in range(len(b)):
if b[i] in a:
temp = list(a)
temp.remove(b[i])
a = "".join(temp)
else:
count += 1
@SahilKadam
SahilKadam / linkedListCycle.py
Created November 16, 2016 05:35
Linked Lists: Detect a Cycle
"""
Detect a cycle in a linked list. Note that the head pointer may be 'None' if the list is empty.
A Node is defined as:
class Node(object):
def __init__(self, data = None, next_node = None):
self.data = data
self.next = next_node
"""
@SahilKadam
SahilKadam / balanceBrackets.py
Created November 16, 2016 05:59
Stacks: Balanced Brackets
def is_matched(expression):
stack = []
for item in expression:
if len(stack) == 0:
stack.append(item)
else:
if item == '{' or item == '[' or item == '(':
stack.append(item)
elif item == '}' and stack.pop() != '{':
return False
@SahilKadam
SahilKadam / queue2stacks.py
Created November 16, 2016 15:11
Implement queue using two stacks
class MyQueue(object):
def __init__(self):
self.stack1 = []
self.stack2 = []
def peek(self):
if (len(self.stack2) == 0):
while(len(self.stack1) > 0):
self.stack2.append(self.stack1.pop())
return self.stack2[len(self.stack2)-1]
@SahilKadam
SahilKadam / runningMedian.py
Created November 16, 2016 19:19
Find the Running Median
#!/bin/python3
import sys
def checkPosition(lst, i, j, num):
if i == j and lst[i]>num:
return i-1
if i == j and lst[i]<num:
return i
tmp = int((i+j)/2)
@SahilKadam
SahilKadam / bubbleSort.py
Created November 16, 2016 19:37
Sorting: Bubble Sort
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
count = 0
for i in range(len(a)):
swapCount = 0
for j in range(len(a)-1):
if a[j] > a[j+1]:
temp = a[j]
a[j] = a[j+1]
@SahilKadam
SahilKadam / minMaxSum.py
Created November 17, 2016 17:02
Mini-Max Sum
#!/bin/python
import sys
a,b,c,d,e = input().strip().split(' ')
a,b,c,d,e = [int(a),int(b),int(c),int(d),int(e)]
lst = [a,b,c,d,e]
@SahilKadam
SahilKadam / findTriplets.java
Created March 9, 2017 06:20
Given an array of distinct elements. The task is to find triplets in array whose sum is zero.
import java.util.*;
public class findTriplets{
public static void main(String []args){
int[] input = {1, -2, -1, 0, 2};
int result_n3 = findTriplets_n3(input);
int result_n2 = findTriplets_n2(input);
System.out.println(result_n3);
System.out.println(result_n2);
}