Codility-type coding challenge walkthrough: array rotation
and why doing these questions are worth your time
Back to my series of coding challenge questions. I’ll be posting a few of these individually, as they are basically required practice if you are an entry-level data analyst or data scientist looking for your first position.
Think from the hiring manager’s perspective. They see you might have some projects and coding experience, but that doesn’t completely cut it for them. As one respectable senior data scientist once told me, who will the hiring person choose: the one who can code the right answers after scouring through stack overflow and google or the one who can code the answer just from the knowledge in his head? I think you know the answer to that.
These questions might seem mindless and unreasonably challenging at times, but your answers to them demonstrate the skills in your head. And once you start to get the right answers, you will feel good. How you approach these problems is also key to successfully completing them.
Here is a question on a code challenge site that is a decent example of something you might see:
You are presented array A consisting of N integers. Let’s say that rotating the array means that each element is shifted right by one place/index, and the last element of the array is moved to the first place. For example, the rotation of array A = [9, 3, 5, 2, 1] is [1, 9, 3, 5, 2] (elements are shifted right by one index and 6 is moved to the first place like a conveyer belt).
Write a function to rotate the array A S times; that is, each element of A will be shifted to the right S times.
Once again, let’s break this down into manageable steps.
Let’s first create an input box so we can test our own array. Input() will create the form to write an value. Using map(int, input()) will coerce every input value into an integer. ‘.strip()’ will strip all spaces from the ends of the input. And ‘split()’ alone will split the remaining values up by the default separator (if nothing is specified), which is a space, into a list. We still have to coerce that with list() though, otherwise, A will just be a map object.
We will also make a separate input box for S.
A = list(map(int,input("\nEnter the numbers : ").strip().split()))
S = int(input())#output:
#Enter the numbers : 3 5 4 2 0
#[3, 5, 4, 2, 0]
How do we start approaching this? The idea of this task is to shift the elements to the right, however many values S is.
Well I know we can take the last element of the list out and return it with “.pop()”. Remember, [-1] is how computers read the very last element of an array. We can assign the output of that to a variable we c:
last = (A.pop(-1))last#output:
We can just check what happened by returning the array.
#[3, 5, 4, 2]
Since we assigned that popped element to a variable, we can reinsert that variable using “.insert()”.With .insert(), you can specify the place in which you want to insert your new element.
#0 specifies the insert location as the first placeA.insert(0, last)#output
#[0, 3, 5, 4, 2]
Now, the thing is we need to do all of this S number of times.
What is the first thing that comes to my mind? For loop! Almost anytime you hear of doing something N number of times, you will probably utilize some form of for loop.
Now we want to combine this code into a function.
def solution(A, K):
for i in range(K):
last = (A.pop(-1))
There we go!
See how walking through the logic step-by-step was making this much easier? This is my general approach for all of these types of questions. The more you do this, the more fluid your capacity to answer questions using Python becomes!