This file contains hidden or 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 (x^n)%d | |
| def pow(self, x, n, d): | |
| """ | |
| :param x: integer | |
| :param n: integer | |
| :param d: integer |
This file contains hidden or 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' | |
| #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 : |
This file contains hidden or 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' | |
| #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)): |
This file contains hidden or 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' | |
| #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 |
This file contains hidden or 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' | |
| #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 : |
This file contains hidden or 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' | |
| #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] |
This file contains hidden or 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 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 |
This file contains hidden or 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' | |
| #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 : |
This file contains hidden or 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' | |
| #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' : |
This file contains hidden or 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' | |
| #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 |