Posts

Showing posts from July, 2017

Hackerrank - Greedy - Jim and the Orders

Jim's Burgers has n hungry burger fans waiting in line. Each unique order,i, is placed by a customer at time t i , and the order takes d i units of time to process.

Hackerrank - Search - Pairs

Given N integers, count the number of pairs of integers whose difference is k. Sample Input 5 2 1 5 3 4 2 Sample Output 3 Explanation There are 3 pairs of integers in the set with a difference of 2. Solution N, k = map(int, raw_input().split()) a = map(int,raw_input().split()) print len(set(a) & set(x + k for x in a))

Hackerrank - Cipher

Jack and Daniel are friends.They want to encrypt their conversation so that they can save themselves from interception by a detective agency. So they invent a new cipher. Every message is encoded to its binary representation B of length N. Then it is written down K times, shifted by 0,1….,K-1 bits. If B=1001010 and K=4 it looks so : Input 1001010   1001010     1001010       1001010 Then calculate XOR in every column and write it down. This number is called S. For example, XOR-ing the numbers in the above example results in Output 1110100110 Input Format The first line contains two integers N and K. The second line contains string S of length N+K-1 consisting of ones and zeros. Output Format Decoded message of length N, consisting of ones and zeros. Sample Input 7 4 1110100110 Output 1001010 Explanation 1001010   1001010     1001010       1001010 ------------------- 1110100110 Solution #!/bin/python l, r = map(int, raw_input().split(" "

Hackerrank - Algorithm - Fibonacci Modified

A series is defined in the following manner: Given the nth and (n+1)th terms, the (n+2)th can be computed by the following relation T(n+2) = (Tn+1)^2 + T(n) So, if the first two terms of the series are 0 and 1: the third term = 1^2 + 0 = 1 fourth term = 1^2 + 1 = 2 fifth term = 2^2 + 1 = 5 ... And so on. Given three integers A, B and N, such that the first two terms of the series (1st and 2nd terms) are A and B respectively, compute the Nth term of the series. Input Format You are given three space separated integers A, B and N on one line. Input Constraints 0 <= A,B <= 2 3 <= N <= 20 Output Format One integer. This integer is the Nth term of the given series when the first two terms are A and B respectively. Sample Input : 0 1 5 Output : 5 Solution #!/bin/python t1,t2,n = map(int,raw_input().split()) for i in range(1,n-1): ans = t1 + t2**2 t1 = t2 t2= ans print(ans)

LeetCode - Single Number

Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Solution : Using XOR logic #!/bin/python def singleNumber(self, a): uniq = 0 for k in a: uniq ^= k return uniq """ :type nums: List[int] :rtype: int """

LeetCode - Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0 . Note: A word is defined as a character sequence consists of non-space characters only. Example: Given s = "Hello World ", return 5 . Solution #!/bin/python def lengthOfLastWord(self, a): a = a.strip() if not a: return 0 return len(a.split()[-1]) """ :type s: str :rtype: int """

Number of ways to add up to a sum S with N numbers

Given an array of unique number and sum. There are many combinations and subsets of an array that come up with given sum. Example: Input_1 : A[] = {1,2,4,5,9} sum = 15 Output_1 : 2 Combinations are {1,5,9} and {1,2,4,5} . Input_2 : A[] = {2,3,5,6,8,10} Sum = 10 Output_2 : 3 Combinations are {2,3,5}, {2,8} and {10} . #!/bin/python A = [2,3,5,8,10] total = 10 #initialize all elements to zero first arr = [[0 for x in range(len(A)+1)] for y in range(len(A)+1)] arr[0][0] = 1 index_dict = {0:0} #Stores element and its index in array for ind, y in enumerate(A): index_dict[y] = ind+1 for i in range(1,len(A)+1): arr[i] = [x for x in arr[i-1]] #Duplicate upper row for j in range(i,len(A)+1): diff = A[j-1]-A[i-1] if diff in index_dict: #If difference found in present list indx = index_dict[diff] arr[i][j] = arr[i-1][j] + arr[i-1][indx] #Update with its possible combination print arr[-1][-1] Explanation : Array Elem

LeetCode - Maximum contiguous subarray sum

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. Example: given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6 #!/bin/python def maxSubArray(self, a): cur_max = a[0] max_sofar = a[0] for k in range(1, len(a)): if cur_max < 0: cur_max = a[k] else: cur_max += a[k] if max_sofar < cur_max: #if current max increases than max so far, new max so far would be current max max_sofar = cur_max return max_sofar """ :type nums: List[int] :rtype: int """

LeetCode - Roman to Integer conversion

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Input format is ‘String’ Example: Sample Input: ‘XXVII’ Output : 27 #!/bin/python #num is input Roman number def romanToInt(self, num): dic = {'I': 1, 'V': 5, 'X': 10, 'L':50, 'C':100, 'D': 500, 'M': 1000} l = len(num) integer = dic[num[l-1]] for i in range(l-1,0,-1): if dic[num[i]] <= dic[num[i-1]]: integer += dic[num[i-1]] else: integer -= dic[num[i-1]] return integer

LeetCode - Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Input format is 'String' --------------------------------------------------------------------------------------------------------- Sample Input: 'XXVII' Output : 27 ------------------------------------------------------------------------------------------------------------ def romanToInt(self, num): dic = {'I': 1, 'V': 5, 'X': 10, 'L':50, 'C':100, 'D': 500, 'M': 1000} l = len(num) integer = dic[num[l-1]] for i in range(l-1,0,-1): if dic[num[i]] <= dic[num[i-1]]: integer += dic[num[i-1]] else: integer -= dic[num[i-1]] return integer

AIRTYPE machine learning enabled virtual computer keyboard

Image
airtype keyboard , sensor gloves, vr gloves, virtual keyboard[/caption] As the product becomes more popular, more and more innovations are required to ease the current challenges and provide more features. It is important that product should keep its pace and keep on upgrading. Since the advent of computers, a keyboard has undergone a vast change in its design and features. The current generation of keyboard takes it to a new level with the idea being to do away with the physical keyboard. A different variant of this scheme in the form of finger sensor keyboard is being proposed in this report. Now every computer user has their own unique typing pattern, with some using their Index finger to type most of the characters while others use all of their fingers to type. This correlation between fingers and the keys is known as the Keystroke Dynamics. Every user will have his own unique Keystroke Dynamic. AIRtype Keyboard uses this correlation to become their personal keyboard by le

Hackerrank - Dynamic Programming - The Coin Change Problem

You have   types of coins available in infinite quantities where the value of each coin is given in the array  . Can you determine the number of ways of making change for   units using the given types of coins? For example, if  , and  , we can make change for   units in three ways:  ,  , and  . Given  ,  , and  , print the number of ways to make change for   units using any number of coins having the values given in  . Input Format The first line contains two space-separated integers describing the respective values of   and  .  The second line contains   space-separated integers describing the respective values of   (the list of distinct coins available in infinite amounts). Constraints Each   is guaranteed to be distinct. Hints Solve overlapping subproblems using  Dynamic Programming  (DP):   You can solve this problem recursively but will not pass all the test cases without optimizing to eliminate the  overlapping subproblems . Think of a way

Best Programming language for machine learning.