This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Vivek' | |
def isPrime(A): | |
""" | |
:param: An integer | |
:return: Returns integer True if it's prime otherwise false | |
""" | |
if A == 1: | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |