Skip to content

Instantly share code, notes, and snippets.

View viveksyngh's full-sized avatar

Vivek Kumar Singh viveksyngh

View GitHub Profile
@viveksyngh
viveksyngh / FACTORS.py
Created August 1, 2015 15:40
Finds all the factors of an integer
__author__ = 'Vivek'
#This function returns all the factors of an integer in sorted order
def allFactors(A):
"""
:param: A positive integer
:return: All the factors of the integer in sorted order.
"""
res = []
firstHalf = []
secondHalf = []
@viveksyngh
viveksyngh / ISPRIME.py
Created August 2, 2015 12:03
Check for prime
__author__ = 'Vivek'
def isPrime(A):
"""
:param: An integer
:return: Returns integer True if it's prime otherwise false
"""
if A == 1:
return False
@viveksyngh
viveksyngh / SEIVE.py
Created August 2, 2015 12:37
Implementation of sieve of eratosthenes
__author__ = 'Vivek'
#Implementation of Sieve of eratosthenes , It returns an array containing all prime numbers less than A ( Including A)
def sieve(A):
"""
:param: An integer A
:return: An array of prime number less than equal to A
"""
L = [1] * (A + 1)
L[0] = 0
L[1] = 0
@viveksyngh
viveksyngh / POWER2.py
Created August 3, 2015 12:28
Given a positive integer which fits in a 32 bit signed integer, find if it can be expressed as A^P where P > 1 and A > 0. A and P both should be integers.
#Given a positive integer which fits in a 32 bit signed integer,
#Find if it can be expressed as A^P where P > 1 and A > 0. A and P both should be integers.
def isPower(A):
"""
Returns True if number can be expressed as A^P where P > 1 and A > 0
:param: A an integer
:return: True if it can be expressed as A^P otherwise False
"""
if A == 1 :
return True
@viveksyngh
viveksyngh / ARRANGE.py
Created August 4, 2015 20:15
Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space.
def arrange(self, A):
for i in range(len(A)) :
A[i] += (A[A[i]]%len(A)) * len(A)
for i in range(len(A)) :
A[i] = A[i]/len(A)
# Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space.
# Input: arr[] = {3, 2, 0, 1}
# Output: arr[] = {1, 0, 3, 2}
@viveksyngh
viveksyngh / PATH.py
Created August 5, 2015 18:54
Find number of different possible path from Start to Finsih
#A robot is located at the top-left corner(Start) of an A x B grid
#The robot can only move either down or right at any point in time.
#The robot is trying to reach the bottom-right(Finish) corner of the grid
#How many possible unique paths are there ?
def nCr(self, n, r) :
f = math.factorial
return f(n)/f(r)/f(n -r)
def uniquePaths(self, A, B):
return self.nCr(A+B-2, B -1)
#Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number.
def primesum(n):
for i in xrange(2, n):
if is_prime(i) and is_prime(n - i):
return i, n - i
def is_prime(n):
if n < 2:
return False
@viveksyngh
viveksyngh / EXCEL1.py
Created August 5, 2015 19:22
Given a column title as appears in an Excel sheet, return its corresponding column number.
def titleToNumber(self, A):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
num = 0
for i in range(len(A)) :
num += (alphabet.index(A[i]) + 1) * (26 ** (len(A) - i - 1) )
return num
#A -> 1
#B -> 2
#C -> 3
@viveksyngh
viveksyngh / Excel2.py
Last active August 29, 2015 14:26
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
def convertToTitle(A):
aplhabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
column = ""
while A > 0 :
temp = A%26
#print temp, A
if temp == 0 :
temp = 26
A = A - 1
#print aplhabet[temp-1]
@viveksyngh
viveksyngh / PALINDROME.py
Created August 5, 2015 19:31
Determine whether number is palindrome or not
#Determine whether an integer is a palindrome. Do this without extra space.
#A palindrome integer is an integer x for which reverse(x) = x where reverse(x) is x with its digit reversed.
#Negative numbers are not palindromic.
def isPalindrome(self, A):
num = 0
if A < 0 :
return False
temp = A
while temp != 0 :
num = num*10 + (temp%10)