Codility – Is Permutation

The next Codility challenge I will be covering is the IsPermutation challenge in chapter 2. The question can be seen in the gist below:

The question asks us to return 1 if an inputted array is a permutation, or 0 if it is not a permutation. As we know, a permutation is an array of elements containing all elements from 1..n, once and only once. Using that, we know that an array will fail any tests if an element is less than 0, or greater than the lengths of the array. We also know that if an element appears more than once, it is not a permutation. A good solution to checking if an element has already been encountered is to use a counter. Either a boolean or an integer counter would be appropriate, as long as it is possible to determine if an element has been checked before. My solution can be found below, I scored 100% correctness and 100% performance:

As always, check out the Git for this problem.

Codility – Minimum Integer

This is the first solution I will be providing for chapter 2 on the Codility site. Te second chapter is about counting elements. The reading material is very interesting, if a bit short, and perhaps unclear. It took me a couple of read throughs, it briefly discusses methods of storing numerical data in arrays. Within the first couple of paragraphs, I was already surprised to discover a counter array; storing the occurrence of a number within an array. Using this method, you can store the same amount of data, in much less storage space.

Back to the challenge though. The question itself can be found here:

The question says, find the minimum missing positive Integer from within the array. The array may not be sorted and elements may appear multiple times. Checkout my solution to the problem below, this achieved 100% performance and 100% correctness. However, I am guilty of going back and changing my result due to using counter.length + 1, instead of counter.length at a point in my code. Unfortunately, this caused a index out of bounds error, giving me only 60% performance, initially.

Also, check out my Git repository with the solution in.

Codility – Missing Permutation Element

The second question within the chapter covering Time Complexity on Codility asks you to find a missing element within an Array. The question is below:

Each element within the Array is unique and within the range 1…n+1, however a single element is missing. The Array is initially unordered. The result of the method should return the missing element. The problem is simple to solve; reorder the array and iterate through until a number is found missing from it. When it is found, return the value. There are a couple of cases that can mean no further processing will be necessary:

  • The Array is empty – Return 1, the missing element should be between 1…n+1, or in this instance between 1..0+1->1…1. (This can happen before sorting the array.)
  • The first element in the array is not 1. Return 1. (Must happen after sorting the Array)
  • The last element of the array is not n+1. Return Array Length + 1. (Must happen after sorting)

Now you have read the brief explanation above, check out my solution below:

Also, check out the Git Repository.


Since writing this blog post, I have just searched up how other people resolved this issue, and whilst I got 100% on both score and functionality, I was way off the best solution. Going back to my solution for the first puzzle. The key is to calculate the expected element length for the number of elements, then subtract each element of the array, this gives you the remaining element. Here is a link to the Stack Exchange answer where I found the answer. I can’t believe I didn’t think of that.

Codility – Frog Jump

The Frog Jump challenge is the third challenge within Codility’s first chapter on time complexity. This challenge is pretty simple, the question is as follows:

The question simply says, how many jumps of D length will it take for the frog to cover at least distance between Y and X. The key part to take away from the question is that the distance covered won’t be exact; the distance covered by the number of jumps has to be greater than or equal to the distance.

Below is my solution to the issue; it scored 100% on accuracy and on performance.

I made a Git repository for the code on my GitHub, check it out here.

Codility – Tape Equilibrium

Codility is a great site for learning, and testing your knowledge regarding some complex programming theories. Whilst the site contains many challenges, but there is a helpful section called lessons. Each lesson covers a topic and provides challenges for you to test your knowledge on.

The first of the chapter within the Codility is about Time Complexity. The first challenege within this section is named TapeEquilibrium, with it’s short description being: Minimize the value |(A[0] + … + A[P-1]) – (A[P] + … + A[N-1])|. Huh. Let’s take a look at the problem and the final solution for it. The description of the issue is below:

The question is simple, an Array, A, with N elements is passed into a method. Loop through each element of the Array from A[0] … A[n-1] each time calculating the difference between the sums of each side of the current key in the array.

The question tells us that the worst case time complexity should be O(n). We know that this is linear time, but it will likely end if a certain condition is reached.

Below I have put my final solution into a GIST. Using this, I achieved 100% correctness and 100% on performance.

I made a Git repository for the code on my GitHub, check it out here.

Disable Mod Pagespeed

Recently at work I have been developing a large project on a WordPress site. Unfortunately, over night WordPress updated to 3.9.2 and a whole host of errors occurred simultaneously. One error was that my JavaScript wasn’t updating and a some strange console messages were appearing, nothing I did would fix the errors, I originally looked for errors relating to the WordPress update. The error was nothing to do with WordPress itself, I discovered in a fit of disgusted rage 5 hours later.

The issue was related to an Apache Module Mod Pagespeed. This module appeared to have been activated by the hosting company, the same night as a WordPress update.

Fortunately a bit of htaccess wizardry fixed the issue. Below are the 5 lines of code that saved my sanity.

Always break those case blocks!

This is a simple tip, that I thought I would clarify after spending some time scratching my head over a stupid, stupid mistake!

When you are using a switch statement make sure you are either returning or breaking at the end of a case statement. If you do not, your switch statement will run the expected case block AND the default block.

Below are a couple of GISTs showing what I mean.

The top GIST demonstrates how to do it, and the bottom shows you hoe to not do it. The main difference is that the second case block does not have a break. This means both the function ‘doSomethingElse()’ and ‘doDefault()’ will be executed. Not what we are after.


A basic jQuery plugin

jQuery is an extremely popular Javascript library that simplifies cross-browser Javascript into an easy to use API. As of May 2014, 50.1% of the top 1 million websites use jQuery in one for or another to help enhance their websites. For the same month 66.8% of the top 100,000 website and 78.5% of the top 10,000 websites use jQuery. Those stats can be found here. The popularity of the jQuery library means that as a developer you will likely encounter and work with jQuery.

You can develop with jQuery the same as if it were any bit of Javascript: write your code in an external .js file and include it onto your webpage. However, jQuery allows you to create plugins that extend jQuery and therefore can be reused through out a number of websites. jQuery plugins should be modular; it does one thing and one thing only. Similar to how a class in any programming language or CSS should only do one thing, creating reusable code.

To give you a head start with developing jQuery plugins below is a small template that you can use to build your jQuery plugin around.

The above is a compilation of these two tutorials provided on the jQuery website:

Relevant links: