DEV Community

darkmage
darkmage

Posted on

HackerRank: Circular Array Rotation in Python

Circular Array Rotation

So I am on HackerRank, and SPOILER ALERT

https://www.hackerrank.com/challenges/circular-array-rotation/problem?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign

This took me about five minutes.

This isn't really about how I solved the problem, though I'll briefly talk about that.

https://stackoverflow.com/questions/2150108/efficient-way-to-shift-a-list-in-python

I asked myself/google "python array shift right" and was reminded that collections and deque exist. I've used them before. Python has so many cool tools I forget about all the time. You can't penalize someone for not remembering every single thing.

def circularArrayRotation(a, k, queries):
    from collections import deque 
    items = deque(a) 
    items.rotate(k)
    ret_list = []
    for q in queries:
        #print(items[q])
        ret_list.append(items[q])
    return ret_list
Enter fullscreen mode Exit fullscreen mode

Now, something interesting you'll note is that I have commented-out the print statement in the loop.

When I first wrote the function, it looked like this:

def circularArrayRotation(a, k, queries):
    from collections import deque 
    items = deque(a) 
    items.rotate(k)
    ret_list = []
    for q in queries:
        print(items[q])
Enter fullscreen mode Exit fullscreen mode

Now, this is exactly what the problem-description called for.

"For each query, print the value of the element at index of the rotated array on a new line."

I thought I did that, but then the HackerRank compiler/tester thingy errored out:

Compiler Message
Runtime Error
Error (stderr)
Traceback (most recent call last):
  File "Solution.py", line 42, in <module>
    fptr.write('\n'.join(map(str, result)))
TypeError: 'NoneType' object is not iterable
Input (stdin)
3 2 3
1 2 3
0
1
2
Enter fullscreen mode Exit fullscreen mode

So, I go "Hmmm..." and check the main:

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nkq = input().split()

    n = int(nkq[0])

    k = int(nkq[1])

    q = int(nkq[2])

    a = list(map(int, input().rstrip().split()))

    queries = []

    for _ in range(q):
        queries_item = int(input())
        queries.append(queries_item)

    result = circularArrayRotation(a, k, queries)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()
Enter fullscreen mode Exit fullscreen mode

The important part is this:

result = circularArrayRotation(a, k, queries)
Enter fullscreen mode Exit fullscreen mode

It seems they wanted a return value! Aha! I then understood what I did wrong!

See, debugging is easy :D

So, I commented-out the print statement, and constructed a list of items to return given the parameters, and BAM! 100% on the tests.

A couple nights ago, I went from like rank 900,000 to rank 600,000 on HackerRank. Just now, I moved up to 544328.

Honestly, this whole discussion brings up a thing I will rage/blog about in the near future if someone doesn't do it before me, but it is largely about the philosophical differences of oldschool and newschool development-styles.

Long ago, we did not have hyper-fast internet to look things up with, and developers had to really know everything hyper-efficiently or be able to reference things in physical books.

If there is one thing I got out of public school, it was how to use the book's index and find the answers to questions, or the solutions to problems.

This is different from, say, engineering the utility of deque and rotate on your own. But, HackerRank didn't ask me to engineer it from scratch. They just ask you to solve the problem.

Is it cheating to be able to look things up? Are you really a programmer if you rely upon pre-made tools?

I think: once you have the ability to build the tools yourself, you are greenlit to use pre-made tools. Often the problem for me is "what tools to use?", but so far, Python has been it.

And, well, technically, Google as well.


If you need a Computer Science tutor, code reviewer, or just someone to pair program with, hit me up

Top comments (0)