- #1
Taylor_1989
- 402
- 14
- TL;DR Summary
- I seem to be getting mixed result for my for loop and function, but cant seem to understand why.
I am currently doing some task on a website called Codility (link and bottom of post). The task basically ask for me to create an algorithm to shift an array to the left K times (Full details below). Which seem to work for a non function but I still seem to be shifting it wrong as the output is incorrect when compared to the given answer. Also I put the same code in the function and get a completely different result, but really unsure why.
My code can be show below question.
Can anyone give adivce to where I have gone wrong. Thank's in advance.
Codiility Question:
I have tested both codes simultaneously for the three given test arrays given in the question and the out put for both non zero arrays are as follows.
My code can be show below question.
Can anyone give adivce to where I have gone wrong. Thank's in advance.
Codiility Question:
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.def solution(A, K)
For example, given
A = [3, 8, 9, 7, 6] K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given
A = [0, 0, 0] K = 1
the function should return [0, 0, 0]
Given
A = [1, 2, 3, 4] K = 4
the function should return [1, 2, 3, 4]
Assume that:
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].
Comparsion between non function and function:
K = int(input('Please enter the number of rotation you wish to do on the given array : '))
for x in range(0, K):
temp = A[0] # Storing the intial element in the array
for y in range(0, len(A)-1):
A[y] = A[y+1] # Shifting each element within in the array to the left
# print(A[y])
A[len(A)-1] = temp # Subbing back in the intial element within the array to the end of the new array
print('===================================')
print('Result of Array shift by K = {}'.format(K))
print('===================================')
print()
print('Solution from for loop')
print(A)
print()
def solution(A, K):
K = int(input('Please enter the number of rotation you wish to do on the given array for function: '))
for x in range(0, K):
temp = A[0]
for y in range(0, len(A)-1):
A[y] = A[y+1]
A[len(A)-1] = temp
return(A)
pass
solution(A,4)
print()
print('Solution from function ')
print(A)
I have tested both codes simultaneously for the three given test arrays given in the question and the out put for both non zero arrays are as follows.
Output:
Oringal Array : [3, 8, 9, 7, 6]
Please enter the number of rotation you wish to do on the given array : 3
===================================
Result of Array shift by K = 3
===================================
Solution from for loop
[7, 6, 3, 8, 9]
Please enter the number of rotation you wish to do on the given array for function: 3
Solution from function
[8, 9, 9, 9, 3]
Output:
Oringal Array : [1, 2, 3, 4]
Please enter the number of rotation you wish to do on the given array : 4
===================================
Result of Array shift by K = 4
===================================
Solution from for loop
[1, 2, 3, 4]
Please enter the number of rotation you wish to do on the given array for function: 4
Solution from function
[4, 4, 4, 4]
Process finished with exit code 0