0

I am new to hackerrank and programming, the left rotate question from the data structure section is my third question I am trying to complete. My code as follow:

#!/bin/python3

import math
import os
import random
import re
import sys

def rotateLeft(d, arr, n):
    # Write your code here
    x=0
    while x<d:
      tmp=arr[0]
      for i in range(n):
        if i==(n-1):
           arr[i]=tmp
           break
        arr[i]=arr[i+1]
      x+=1
    
    return arr
       

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

    first_multiple_input = input().rstrip().split()

    n = int(first_multiple_input[0])

    d = int(first_multiple_input[1])

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

    result = rotateLeft(d, arr, n)

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

    fptr.close()

After i submitted my question, hackerrank indicated that the time limit exceeded and i should optimize the code. Hence, I tried using the n variable provided instead of using len(arr), hoping to quicken it, but i guess it doesn't help. Hope you guys can give me some tips on optimizing the code. Thanks a lot!

6
  • I would suggest you look into array splicing to speed up your code for this specific question. With python at least, there is no need to actually iterate over all the elements in the array to get a solution here. Commented Apr 30, 2024 at 10:02
  • 1
    And secondly, think of what you need to do when d is much greater than the size of the list. Do you really want to rotate and rotate and rotate... with steps of one? Or do you see a shortcut? Commented Apr 30, 2024 at 10:03
  • Does this answer your question? hackerrank Circular Array Rotation Python Commented Apr 30, 2024 at 10:13
  • Thanks a lot for your comments! It was very helpful. My current idea is that i could use slicing to slice the array excluding the first element an assign it to the left side of the array excluding the last element and assign the last element using the first element. However when i ran the code it seemed even slower and the system warned me about time limit exceeded. I will try again though as i think this idea is really good because it works without needing to iterate through all the elements. Commented Apr 30, 2024 at 10:26
  • @Roger, Just letting you know, there is no reason to move it one element at a time (as that would indeed be slow). You can try to move it a few more elements at once (trincot provided a hint for calculating the same) Commented May 1, 2024 at 4:04

2 Answers 2

2

Simply Use this rotLeft Function:

def rotLeft(a, d):
    return a[d:]+a[:d]
Sign up to request clarification or add additional context in comments.

Comments

0

You could simply use the list slicing for this problem.

def rotateLeft(d, arr, n):
    return arr[d:]+arr[:d]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.