Skip to content

Instantly share code, notes, and snippets.

View viveksyngh's full-sized avatar

Vivek Kumar Singh viveksyngh

View GitHub Profile
@viveksyngh
viveksyngh / REVERSE.py
Created August 5, 2015 19:34
Reverse an Integer
#Reverse digits of an integer.
#Return 0 if the result overflows and does not fit in a 32 bit signed integer
def reverse(self, A):
rev = 0
flag = False
if A < 0 :
flag = True
A = abs(A)
while A != 0 :
rev = rev * 10 + (A%10)
@viveksyngh
viveksyngh / GCD.py
Created August 5, 2015 19:45
Greatest Common Divisor (GCD) of two number
#Given 2 non negative integers m and n, find gcd(m, n)
def gcd(A, B):
while B != 0 :
A , B = B , A%B
return A
@viveksyngh
viveksyngh / FACTORIAL.py
Created August 5, 2015 19:47
Given an integer n, return the number of trailing zeroes in n!.
#Given an integer n, return the number of trailing zeroes in n!.
def trailingZeroes(self, A):
numOfZero = 0
i = 1
while 5**i <= A :
numOfZero += A//(5 ** i)
i = i + 1
return numOfZero
@viveksyngh
viveksyngh / COUNTELEMENTS.py
Last active August 29, 2015 14:26
Given a sorted array of integers, find the number of occurrences of a given target value.
__author__ = 'Vivek'
def binarySearch(A, B, flagSearchFirst) :
"""
Modified version of Binary Search to find first or last index of an element when it is repeated
:param A: List of sorted integers
:param B: integer to be searched
:param flagSearchFirst: Whether to search for first or last index of integer B in array
:return: index (if nott found then -1)
"""
@viveksyngh
viveksyngh / INSERTPOS.py
Created August 5, 2015 22:07
Given a sorted array and integer find the index at which element will be inserted
__author__ = 'Vivek'
#Given a sorted array and a target value, return the index if the target is found.
# If not, return the index where it would be if it were inserted in order.
#You may assume no duplicates in the array.
def searchInsert(A, B):
"""
:param: A List of integers , B integer to be inserted
:return: return index if B is already present in A , otherwise index at which B will be inserted
"""
low = 0
@viveksyngh
viveksyngh / SQRT.py
Created August 6, 2015 21:22
Finds SQRT of an integer
__author__ = 'Vivek'
#Find Square root of an inetger , if Square root is not integer returns floor of sqrt
def sqrt(A) :
if 2 > A :
return A
low = 0
high = A
while high > low + 1 :
num = (low + high)/2
if num**2 < A :
@viveksyngh
viveksyngh / StrStr.py
Created August 8, 2015 16:12
Return the first index of needle in haystack otherwise -1
__author__ = 'Vivek'
def strStr(self, haystack, needle):
"""
:param:
:haystack: A string
:needle: A string
:return: index of the first ocurrence of needle in haystack
"""
if len(needle) == 0 or len(haystack)==0 :
return -1
@viveksyngh
viveksyngh / LENLASTWORD.py
Created August 8, 2015 18:16
Finds Length of the Last word, if Last word is not present then return 0
__author__ = 'Vivek'
def lengthOfLastWord(A):
start =0
end = 0
if len(A) == 0 :
return 0
lastLength = 0
curLength = 0
for i in range(len(A)) :
if A[i] != ' ' :
@viveksyngh
viveksyngh / REVWORDS.py
Created August 9, 2015 19:24
Reverse a string Word by Word
__author__ = 'Vivek'
#reverse the string word by word.
def reverseWords(A):
"""
Reverse the string word by word, removes leading and trailing spaces,
:param: string to be reversed
:return: reversed string word by word
"""
curWord = ''
listOfWords = []
@viveksyngh
viveksyngh / SEARCHRANGE.py
Created August 10, 2015 05:49
Search for the Range of a number in a sorted array
__author__ = 'Vivek'
#Find starting and ending index of an element in sorted list of elements
def binarySearch(A, B, searchFirst) :
"""
Modified version of binary search
:param: Sorted List of integers, target element, boolean value (True for first index, False for last index)
:return: List of first index and last index, if target element not found returns [-1, -1]
"""
low = 0
high = len(A) - 1