Циклическое вращение массива в решениях кодируемости Python и C ++, Урок 2

Циклическое вращение массива в решениях кодируемости Python и C ++, Урок 2

CodeTrading

3 года назад

12,500 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@elexawi
@elexawi - 13.07.2021 13:24

Hello,

Thanks for sharing your creative solutions for Codility!

This video is not working, kindly check it :)

Ответить
@wonderjaxe
@wonderjaxe - 21.10.2021 16:39

That modulo is tricky to figure out, I first used minus instead. All the test cases ran correctly but I got 62% because it wouldn't work for K > 2*len(A). Guess moral of the story is to always test a couple of extreme cases at least once, like K = 1000 or something like that.

Ответить
@hunterxx6744
@hunterxx6744 - 13.11.2021 15:35

arr [:n]+ arr[n:] ??? sir can we do in this way or whats problem??

Ответить
@shlokkmoolchandani9980
@shlokkmoolchandani9980 - 17.11.2021 22:44

Would this work?
def cyclic_array(arr,k):
k_new = k%len(arr)
new_index = 0
new_arr = [0 for i in range(len(arr))]
for i in range(len(arr)):
new_index = i + k_new
if new_index > len(arr)-1:
new_index = new_index - len(arr)
new_arr[new_index] = arr[i]
else:
new_arr[new_index] = arr[i]
print(new_arr)

Ответить
@xtremeNarutoPK3
@xtremeNarutoPK3 - 02.02.2022 01:01

Am i cheating by doing this:

N = len(A)
counter = 0

while counter != K:
A.insert(0, A[N-1])
A = A[:-1]
counter += 1

It seems a lot less complicated.

Ответить
@iliaprikhodko7587
@iliaprikhodko7587 - 09.02.2022 21:19

Can somebody explain me how this function work B[(i+K)%N] = A[i] i didn't understand logic, thank you very much.

Ответить
@houssemgharbi6206
@houssemgharbi6206 - 12.02.2022 14:56

Thanks a lot for the explanation !
this is my try in python

def solution(A,k):
n = len(A)
k_1 = k %n
return A[n-k_1:] + A[:n-k_1]

Ответить
@Tesmond256
@Tesmond256 - 13.11.2022 12:05

def cyclic(k: int, arr: list) -> list:
size: int = len(arr)
movement: int = k % size
return arr[-movement:] + arr[:-movement]

tested the above version and it was twice as fast as the version below

def cyclic(k: int, arr: list) -> list:
size: int = len(arr)
cycled: list = [None] * size
for i, v in enumerate(arr):
cycled[(i+k)%size] = v
return cycled

Ответить
@Adydobre
@Adydobre - 16.02.2023 20:01

Compilation successful.

Example test: ([3, 8, 9, 7, 6], 3)
OK

Example test: ([0, 0, 0], 1)
OK

Example test: ([1, 2, 3, 4], 4)
OK

Your test case: def solution(A, K):
RUNTIME ERROR (invalid input, unexpected character: :)

Your test case: if len(A) == 0:
RUNTIME ERROR (invalid input, unexpected character: =)

Your test case: return A
RUNTIME ERROR (invalid input, unexpected 'return', expecting integer)

Your test case: K = K % len(A)
RUNTIME ERROR (invalid input, unexpected character: =)

Your test case: return A[- K:] + A[:-K]
RUNTIME ERROR (invalid input, unexpected character: -)

Detected some errors.

Can you please explain me why codility detected those errors?

Ответить
@saikono9812
@saikono9812 - 24.02.2023 21:52

def solution(A, K):
# This function returns an array of elements shifted to the right k times
# 100% solution

if not(A): # Check if there are elements in the list
return A # Return empty list if no elements in the list

num_elements = len(A) # Initialize number of elements in list
for _ in range(K): # Loop through times to shift each element (e.g. shift position of elements 3 times)
A.insert(0, A[num_elements-1]) # Insert element of last index in first index of the list (done each time)
A.pop() # Remove last element from list each time
return A

Ответить
@wojtek-nh1oh
@wojtek-nh1oh - 18.04.2023 18:51

Is it good?
def cyclic_array_rotation(k, a):
for x in range(k):
holder = a.pop()
a.insert(0, holder)

Ответить
@srudsalam6970
@srudsalam6970 - 27.07.2023 02:59

function cyclicRotation(A: number[], K: number): number[] {
const L = A.length;
const R = L - (K % L);
const arr = A.splice(R);
return [...arr, ...A];
}

console.log("==> ", cyclicRotation([3, 8, 9, 7, 6], 3)); // [9, 7, 6, 3, 8]
console.log("==> ", cyclicRotation([0, 0, 0], 1)); // [9, 7, 6, 3, 8]
console.log("==> ", cyclicRotation([1, 2, 3, 4], 4)); // [9, 7, 6, 3, 8]
console.log("==> ", cyclicRotation([1, 1, 2, 3, 5], 42)); // [3, 5, 1, 1, 2]

Ответить
@ripperx444
@ripperx444 - 02.12.2023 17:25

This is the best channel to explain the logic of the algo... practice these enough in your language then ask for 200k!

Ответить