Blogs

The 2nd Codility lesson is to search for the odd number of elements in an array that cannot be paired with any other element. Codility provided this example.

A[0] = 9, A[1] = 3, A[2] = 9
A[3] = 3, A[4] = 9, A[5] = 7
A[6] = 9

Apart from A[5], every other element has a match. The first thought is to iterate through the array and look for the pair and remove both elements once it is found. When an element cannot find its pair, break from the loop and return the element. Simple enough, so I coded. 

It works for a small number of elements but Codility needs this to run in less than 1 second when tested in big random sets with 999,999 elements. My solution runs in time for only tests with 2,001 elements or less. 

And this become a revision for bitwise operators after reading the comments in Codility. This was the final answer, adapted from what another programmer shared.

Codility is where programmers receive programming tests as interview questions. This is also where programmers could learn to code. I am at lesson 1 on Iterations, and here's my answer. It finds the binary gap of a number. A binary gap is the longest sequence of 0 of a number converted to binary. For example, the number 5 is 101 is binary, and it contains 1 zero, so it's binary gap is 1. The number 1041 is 10000010001 in binary. It contains 2 sequences of 0, the first of which is 5 zeros long and the second is 0 zeros long. So its binary gap is 5, since the first sequence is longer than the first.

I got 100% and did it in 15 minutes and....2 tries. During my first try, I got excited and forgot to initialize my numZero variable back to 0 after it found each sequence of zeros. Careless me!

I maintain sanity by running little projects in the evenings after the family goes to bed. Over the years, I wrote codes, built little gadgets and fiddled with anything that interest me at one particular point of time. Some of these projects eventually becomes part of what I use for my day job teaching technology in my school. Others become starting points for company-based projects.

I think it will be fun to document this process as I learn as I have become a lot more active in building stuff lately. If you find something you like or want, or if you found that I have been quite wrong to attempt things the way I did, feel free to tell me. 

If you are a company who sees commercial value in what I do, come and take it.

If you want me to do something for you, feel free to ping me.

 

 

Pages