0

I am trying to solve problem 189 on leetcode

The question is:

Given an array, rotate the array to the right by k steps, where k is non-negative.

According to the solution, I have tried out to implement the idea given in approach 2: by using an "extra" array. Still, I am unable to get the desired result. What's the issue with this code?

I tried brute force approach as well but it's not accepted there, so trying this!! Any help would be appreciated!

i = 0
l = len(nums)
arr = [0]*l

while i < len(nums):
    split = l-k
    if i >=0 and i < split:
        arr[i+k] = nums[i]
    if i >= k+1 and i < l:
        arr[i-k-1] = nums[i]

    i+=1

nums = [0]*l

for a in range(0,l):
    nums[a] = arr[a]

print(arr)
print(nums)        

After getting some help, I tried with the slicing approach(as suggested in the comments) and this is the code I could come up with:

l = len(nums)

a = nums[:l-k] # appending part
nums = nums[l-k:] # real array

for i in a:
    nums.append(i)


print(nums)

Still, this is not running on that website. I am getting the correct output array but not exactly what's the requirement.

5
  • 3
    Hello there.. May be you can post the problem also from that site? That way, I think the question will be complete and clear. Commented Feb 10, 2020 at 1:07
  • 2
    I think you are overcomplicating things a bit with the rotation. Without spoiling the solution I'd suggest to try it using the modulo operator Commented Feb 10, 2020 at 1:44
  • 1
    I'd agree with @DJSchaffner.. One approach is to consider modulo operator. Also, you can consider python array slicing.. Commented Feb 10, 2020 at 1:47
  • 2
    I would recommend list slicing for this. That is simpler than creating a temporary list and copying elements. Think about where the front should start and append the tail from the start of the list. Commented Feb 10, 2020 at 2:43
  • @RomeoSierra thanks for the help. I tried with the slicing method but still confused. Can you take a look at the code? Commented Feb 10, 2020 at 4:27

1 Answer 1

1

Check the below out. I came up with the correction needed.

from typing import List

def rotate(nums: List[int], k: int) -> None:
    l = len(nums)

    a = nums[:l-k] # appending part
    # nums = nums[l-k:] # real array # -> How you have written
    nums[:] = nums[l-k:] # real array # -> How it should be


    for i in a:
        nums.append(i)


    # print(nums) # Not needed


nums = [1,2,3,4,5]

rotate(nums, 3)

print(nums)

Problem with you code is that, the task expects you to modify the list that is being passed into the function. However when you assign it like nums = nums[l-k:], that will be visible only to within the function. That's why when you print it you saw the expected result. But that assignment will not modify the list referred to by that variable. Instead, you should do the assignment like nums[:] = nums[l-k:], in order to modify the list, which is in the global scope.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting! it worked!! But I still did not understand. What exactly is nums[:] doing here? What's the difference between nums[:] and nums?
I have tried to find some reference for us to read on that. But unfortunately I couldn't find any. If I find one, I will post here for sure.. :)

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.