Python Array Problem Solution

Code-Stücke können hier veröffentlicht werden.
Antworten
codebuster
User
Beiträge: 3
Registriert: Dienstag 29. März 2022, 13:51
Kontaktdaten:

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..!
__deets__
User
Beiträge: 14529
Registriert: Mittwoch 14. Oktober 2015, 14:29

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?
Benutzeravatar
__blackjack__
User
Beiträge: 13080
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@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.
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Benutzeravatar
ThomasL
User
Beiträge: 1366
Registriert: Montag 14. Mai 2018, 14:44
Wohnort: Kreis Unna NRW

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()
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
codebuster
User
Beiträge: 3
Registriert: Dienstag 29. März 2022, 13:51
Kontaktdaten:

__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/
codebuster
User
Beiträge: 3
Registriert: Dienstag 29. März 2022, 13:51
Kontaktdaten:

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:)
Antworten