Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have a list of numbers stored in a 1-D array named A that can contain up to

ID: 3813951 • Letter: Y

Question

You have a list of numbers stored in a 1-D array named A that can contain up to N elements. Your program needs to shift each element in the array one position to the left. The original left most element should be inserted into the last array location. The combined operation is known as "rotate left". Assume the array is indexed from 1 to N, and that the array has already been initialized with some values. Example for N = 5: Original array A (before rotation): Processed array A (after rotating to the left): In the area below, type in your. It must work for an array of arbitrary length N.

Explanation / Answer

import fractions # rotates an array in-place i positions to the left, in linear time def rotate(arr,i): n = len(arr) reps = fractions.gcd(n,i) swaps = n / reps for start in xrange(reps): ix = start tmp = arr[ix] for s in xrange(swaps-1): previx = ix ix = (ix + i) % n arr[previx] = arr[ix] arr[ix] = tmp return arr

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote