Source Link:
https://codility.com/programmers/lessons/2-arrays/
Some Notes:
Array is a data-structure that can be used to store many items in one place.
2.4. Iterating over an array
Knowing that the array contains of N elements, we can iterate over consecutive integers from index 0 to index N −1.
2.6. Exercise
Problem: Given array A consisting of N integers, return the reversed array.
Solution: We can iterate over the first half of the array and exchange the elements with those in the second part of the array.
Reversing an array.
1 def reverse(A):
2 N = len(A)
3 for i in xrange(N // 2): // i from first to last until N/2 <--- important
4 k = N - i - 1 // k from last to first until N/2 (let k = N-i-1)
5 A[i], A[k] = A[k], A[i] // then, swap A[i] A[k]
6 return A // reverse the array
Subscribe to:
Post Comments (Atom)
Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments
Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...

-
Source Link: https://app.codility.com/programmers/lessons/15-caterpillar_method/count_distinct_slices/ Question: An integer M and a...
-
Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/tie_ropes/ Question: There are N ropes numbered from 0 ...
-
Source Link: https://app.codility.com/programmers/lessons/15-caterpillar_method/count_triangles/ Question: A zero-indexed array A consi...
No comments:
Post a Comment