Skip to main content

Posts

Showing posts from July, 2017

Codility Lesson 2: Arrays CyclicRotation (Solution in Python)

Question: Given a list of length N, print the elements of the list A after K right cyclic rotations Description of Input Data:  1. N,K are integers in the range of [0,100] 2. Elements in A lie in the range of [-1000,1000] Example:  Input Data: A=[3,8,9,7,6], N=5, K=3 Expected output: [9,7,6,3,8] Logic: 1. Elements in the list A need to be reordered according to the value of K and length of list A 2. K elements from the end of the list are the first to appear 3. The remaining elements in the same order are placed after the K elements from the end of the list Psedo Code: 1. Check the length of the input list( length = 0 or length > K or length < 0) 2. If length = 0 output is [] 3. If length > K: then the output is concatination of A[-K:],A[:-K] 4. If length < K: then the output is concatination of A[-(K%length) :], A[: -(K%length)] https://codility.com/programmers/lessons/2-arrays/cyclic_rotation/ Let us use some test cases to unde

Codility Lesson 2: Arrays OddOccurancesInArray (Solution in Python)

Question: Find the element in the array list that occurs odd number of times Description of input: All input lists will contain only one number that occurs odd number of times Example: input = [9,3,9,3,9,7,9] output = 7 Logic: 1. Sort the data set in ascending order 2. Check all the even indiced numbers in the list if they match the next element 3. In case they match that number(at the even index) is present even number of times, when the numbers dont match the number it means that the number(at the even index) occurs odd number of times Code flow or Pseudo Code: 1. Check for length of input list if length is 1 output is that number else go to 2 2. For even numbered index check the sorted list if the next element matches the number in this position 3. If it doesnot match we have the number which occurs odd number of times else we have to repeat step 2 with the next even index 4. If there is no match until the last even index then the number that has odd occurances is t

Codility Lesson 1: Iterations BinaryGap (Solution in Python)

Codility Question Iterations Lesson: Given a number, find the maximum number of zeros, in its binary representation, between "1"s Example: input |     binary         | output    9     | 1001                 |    2  529   | 1000010001     |   4 Logic: Binary number notation involves just 2 numbers '0's and '1's. For any given number the placement of '1's and '0's could be one of the following options a) '1' followed by a '1' b) '1' followed by '0's c) '0' followed by '0'. The question requires us to find the maximum number of '0's flanked by '1's within a binary number. So to find the number of '0's between '1's we will do the following steps: 1) Find the location of the next occurring '1' with respect to the current '1' 2)To find the number of zeros between them we must find the difference between indices in which the current and next &#