Seite 1 von 1

Python Array Problem Solution

Verfasst: Dienstag 29. März 2022, 14:01
von codebuster
Hey,

I Newly Joined this community, hope all are staying well.

A little background: I am a noob in python programming and for the last few weeks had been practising a few Python programs on Arrays. I need help understanding this very problem, it would really mean a lot if someone could comment across each code line and share the code snippet using any online compiler.

Problem Statement:
Q) Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements.
Input : arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2
Output : arr[] = [3, 4, 5, 6, 7, 1, 2]

Code: Alles auswählen

# Python program for reversal algorithm of array rotation
  
# Function to reverse arr[] from index start to end
def rverseArray(arr, start, end):
    while (start < end):
        temp = arr[start]
        arr[start] = arr[end]
        arr[end] = temp
        start += 1
        end = end-1
  
# Function to left rotate arr[] of size n by d
def leftRotate(arr, d):
    n = len(arr)
    rverseArray(arr, 0, d-1)
    rverseArray(arr, d, n-1)
    rverseArray(arr, 0, n-1)
  
# Function to print an array
def printArray(arr):
    for i in range(0, len(arr)):
        print (arr[i])
  
# Driver function to test above functions
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2) # Rotate array by 2
printArray(arr)
Thanks..!

Re: Python Array Problem Solution

Verfasst: Dienstag 29. März 2022, 14:17
von __deets__
Du hast den Code hier gefunden https://www.pythonforengineers.in/2021/ ... m-for.html und wir sollen den jetzt erklaeren, damit du deinem Lehrer/Dozenten erklaeren kannst, was da vor sich geht?

Re: Python Array Problem Solution

Verfasst: Dienstag 29. März 2022, 14:51
von __blackjack__
@codebuster: That code you copied has nothing to do with arrays because it is using lists. And it uses them like if they were arrays in a quite unpythonic way. Also it is expressed unneccesarly complicated with that reverse function. If I were you I would forget that code and write a `rotate()` function that is really written in Python and not some lower level procedural language like C oder Pascal in Python syntax.

BTW the problem statement doesn't say anything about values for `n` that are greater than the length of `arr` and about negative values for `n`. So either you make sure that the code does something sensible in those cases, or document that such values for `n` are undefined and so is the behaviour of the function.

Re: Python Array Problem Solution

Verfasst: Dienstag 29. März 2022, 17:54
von ThomasL
The code you posted is horrible. Coding in Python is knowing the language and the available libraries.
Here a little sample with two functions that are only wrapper for collections.deque.
If you have a project where you need to rotate data a lot it would be wiser to use deque as main datastructure.

Code: Alles auswählen

from collections import deque

def rotate_right(iterable, steps):
    queue = deque(iterable)
    queue.rotate(steps)
    return list(queue)
    
def rotate_left(iterable, steps):
    return rotate_right(iterable, -steps)

def main():
    inp = [1, 2, 3, 4, 5, 6, 7]
    
    left2 = [3, 4, 5, 6, 7, 1, 2] # rotate left 2 steps
    assert rotate_left(inp, 2) == left2
    
    right2 = [6, 7, 1, 2, 3, 4, 5] # rotate right 2 steps
    assert rotate_right(inp, 2) == right2
    
if __name__ == "__main__":
    main()

Re: Python Array Problem Solution

Verfasst: Donnerstag 31. März 2022, 06:48
von codebuster
__deets__ hat geschrieben: Dienstag 29. März 2022, 14:17 Du hast den Code hier gefunden https://www.pythonforengineers.in/2021/ ... m-for.html und wir sollen den jetzt erklaeren, damit du deinem Lehrer/Dozenten erklaeren kannst, was da vor sich geht?
Hey had been using this resource to practice:
https://www.interviewbit.com/snippet/54 ... e98905382/
https://www.geeksforgeeks.org/python-pr ... otation-2/

Re: Python Array Problem Solution

Verfasst: Donnerstag 31. März 2022, 06:51
von codebuster
ThomasL hat geschrieben: Dienstag 29. März 2022, 17:54 The code you posted is horrible. Coding in Python is knowing the language and the available libraries.
Here a little sample with two functions that are only wrapper for collections.deque.
If you have a project where you need to rotate data a lot it would be wiser to use deque as main datastructure.

Code: Alles auswählen

from collections import deque

def rotate_right(iterable, steps):
    queue = deque(iterable)
    queue.rotate(steps)
    return list(queue)
    
def rotate_left(iterable, steps):
    return rotate_right(iterable, -steps)

def main():
    inp = [1, 2, 3, 4, 5, 6, 7]
    
    left2 = [3, 4, 5, 6, 7, 1, 2] # rotate left 2 steps
    assert rotate_left(inp, 2) == left2
    
    right2 = [6, 7, 1, 2, 3, 4, 5] # rotate right 2 steps
    assert rotate_right(inp, 2) == right2
    
if __name__ == "__main__":
    main()
Thanks, this is helpful..! Am working on getting my basics strong:)