Skip to content

Instantly share code, notes, and snippets.

@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 / 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 October 23, 2016 18:24
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.
firstLine = [int(i) for i in input().strip().split(" ")]
secondLine = [int (j) for j in input().strip().split(" ")]
n = firstLine[1]%firstLine[0]
secondLine += secondLine[0:n]
secondLine = secondLine[n:]
[print (i, end = " ") for i in secondLine]
@SahilKadam
SahilKadam / DynamicArray.py
Created October 23, 2016 01:04
Create a list, , of empty sequences, where each sequence is indexed from to . The elements within each of the sequences also use -indexing. Create an integer, , and initialize it to . The types of queries that can be performed on your list of sequences () are described below: Query: Find the sequence, , at index in . Append integer to sequence .…
first_line = [int(item) for item in input().strip().split(" ")]
N = first_line[0]
seqList = [[] for i in range(N)]
lastAns = 0
def query1(x,y):
global N, seqList, lastAns
seqList[(x^lastAns) % N].append(y)
@SahilKadam
SahilKadam / FizzBuzz
Created October 15, 2016 00:10
Write a program that, given a number n from STDIN, prints out all numbers from 1 to n (inclusive) to STDOUT, each on their own line. But there's a catch: For numbers divisible by 3, instead of n, print Fizz For numbers divisible by 5, instead of n, print Buzz For numbers divisible by 3 and 5, just print FizzBuzz
n = int(input().strip())
for i in range(1,n+1):
if i%3 == 0 and i%5 == 0:
print ("FizzBuzz")
elif i % 3 == 0:
print ("Fizz")
elif i % 5 == 0:
print ("Buzz")
else:
@SahilKadam
SahilKadam / Balanced Delimiters
Created October 14, 2016 23:55
For this question, you will parse a string to determine if it contains only "balanced delimiters." A balanced delimiter starts with an opening character ((, [, {), ends with a matching closing character (), ], } respectively), and has only other matching delimiters in between. A balanced delimiter may contain any number of balanced delimiters.
array = input().strip()
lst = []
for i in range(len(array)):
if array[i] == '(' or array[i] == '[' or array[i] == '{':
lst.append(array[i])
elif array[i] == ')':
if len(lst) != 0 and lst[len(lst)-1] == '(':
lst.pop()
@SahilKadam
SahilKadam / Ladybug
Created October 14, 2016 21:57
Given the values of and for games of Happy Ladybugs, determine if it's possible to make all the ladybugs happy. For each game, print YES on a new line if all the ladybugs can be made happy through some number of moves; otherwise, print NO to indicate that no number of moves will result in all the ladybugs being happy.
#!/bin/python3
import sys
def get_num(n, strr):
num = [i for i in strr if i == n]
return (len(num))
def adjust(strr):
flag = True
@SahilKadam
SahilKadam / Fibonacci Returns
Created October 13, 2016 15:17
This question expands on our earlier Fibonacci Lite challenge. While the goal of Fibonacci Lite was to understand recursion, this challenge is about solving problems efficiently with dynamic programming. The difference in this challenge is that each test case will consist of many inputs instead of just one. Furthermore, we're allowing larger val…
from math import sqrt
def fibo(n):
return (((1+sqrt(5))**n - (1-sqrt(5))**n)/(2**n*sqrt(5)))
while True:
try:
n = int(input().strip())
print (int(fibo(n)))
except(EOFError):
@SahilKadam
SahilKadam / Apple and orange
Created October 13, 2016 04:25
Input Format The first line contains two space-separated integers denoting the respective values of and . The second line contains two space-separated integers denoting the respective values of and . The third line contains two space-separated integers denoting the respective values of and . The fourth line contains space-separated integers deno…
#!/bin/python3
import sys
s,t = input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = input().strip().split(' ')
a,b = [int(a),int(b)]
m,n = input().strip().split(' ')
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */