Skip to content

Instantly share code, notes, and snippets.

View viveksyngh's full-sized avatar

Vivek Kumar Singh viveksyngh

View GitHub Profile
@viveksyngh
viveksyngh / POW.py
Created August 10, 2015 06:48
Pow(m, n, d)
__author__ = 'Vivek'
#Implementation of (x^n)%d
def pow(self, x, n, d):
"""
:param x: integer
:param n: integer
:param d: integer
@viveksyngh
viveksyngh / SEARCHROTATED.py
Created August 10, 2015 20:16
Search for an element in a Rotated array
__author__ = 'Vivek'
#Search for a target element in rotated array , if element present then return index otherwise -1
def binSearch(A, B) :
"""
Binary Search Algorithm
"""
low = 0
high = len(A) - 1
result = -1
while low <= high :
@viveksyngh
viveksyngh / LCP.py
Created August 12, 2015 06:24
Longest Common Prefix in a array of Strings
__author__ = 'Vivek'
#LCP LONGEST COMMON PREFIX
def longestCommonPrefix(A):
"""
Find the longest common prefix in an array of string
:param: Array of strings
:return: prefix string
"""
prevLCP = A[0]
for i in range(1, len(A)):
@viveksyngh
viveksyngh / ATOI.py
Created August 12, 2015 07:07
Converts a String to integer
__author__ = 'Vivek'
#Convert a string to number, if Integer Overflows return INT_MAX if number is positive , otherwise INT_MIN
def atoi(A):
"""
:param: A string which need to be converted to integer
:return: An integer
"""
num = 0
A = A.strip()
INT_MAX = 2**31 - 1
@viveksyngh
viveksyngh / ROMANTOINT.py
Created August 13, 2015 04:47
Convert roman numerals into integer
__author__ = 'Vivek'
#Given a roman numerals, convert it into integer
def romanToInt(A):
rtoi_dict = {'I': 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}
num = 0
for i in range(0, len(A)) :
if A[i] == 'I' and i < len(A) - 1 :
if A[i+1] == 'V' or A[i+1] == 'X' :
num += -(rtoi_dict[A[i]])
else :
@viveksyngh
viveksyngh / INTTOROMAN.py
Created August 13, 2015 05:19
Integer to roman
__author__ = 'Vivek'
#Convert an integer into Roman Numerals
def intToRoman(A):
value = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
numerals = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
res , i = "", 0
if A < 1 or A > 3999 :
return res
while A :
res += (A//value[i])*numerals[i]
@viveksyngh
viveksyngh / COUNTANDSAY.py
Created August 15, 2015 07:43
Generate nth count-and-say sequence
__author__ = 'Vivek'
#This will Generate count-and-say sequence
def countAndSay(A):
seq = ''
prevSeq = '1'
if A==1 :
return '1'
for i in range(0, A-1) :
seq = ''
count = 1
@viveksyngh
viveksyngh / ADDBINARY.py
Created August 15, 2015 10:06
Add two binary String
__author__ = 'Vivek'
#Add two binary numbers
def addBinary(A, B):
res = ""
i, j, k, c = len(A) - 1, len(B) - 1 , 0, 0
while i >= 0 and j >= 0 :
if int(A[i]) + int(B[j]) == 2 and c == 0:
c = 1
res = '0' + res
elif int(A[i]) + int(B[j]) == 2 and c == 1 :
@viveksyngh
viveksyngh / MULTIPLY.py
Created August 15, 2015 16:09
Given two numbers represented as strings, return multiplication of the numbers as a string.
__author__ = 'Vivek'
#Given two numbers represented as strings, return multiplication of the numbers as a string.
# DO NOT USE BIG INTEGER LIBRARIES ( WHICH ARE AVAILABLE IN JAVA / PYTHON ).
def multiply(A, B):
res = "0" * (len(A) + len(B))
if A == '0' or B == '0' :
return '0'
elif A == '1' :
return B
elif B == '1' :
@viveksyngh
viveksyngh / POWER.py
Created August 15, 2015 16:13
Find if Given number is power of 2 or not.
__author__ = 'Vivek'
#Find if Given number is power of 2 or not.
#More specifically, find if given number can be expressed as 2^k where k >= 1.
def power(A):
temp = int(A)
A = int(A)
Sum = 0
while A != 0 :
Sum += A%2
A = A/2