All done in python so far

This commit is contained in:
Vladan Popovic 2020-05-10 03:14:42 +02:00
commit 211e38a307
7 changed files with 255 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
#.#
*~
*.sw?

54
a2.py Normal file
View File

@ -0,0 +1,54 @@
"""
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:
class Solution { public int[] solution(int[] A, int K); }
that, given an array A consisting of N integers and an integer K, returns the
array A rotated K times.
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:
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].
In your solution, focus on correctness. The performance of your solution will
not be the focus of the assessment. Copyright 20092020 by Codility Limited.
All Rights Reserved. Unauthorized copying, publication or disclosure
prohibited.
"""
def solution(A, K):
if len(A) in (0, 1):
return A
for _ in range(K):
A = [A[-1]] + A[:-1]
return A

45
a3.py Normal file
View File

@ -0,0 +1,45 @@
"""
A non-empty array A consisting of N integers is given. The array
contains an odd number of elements, and each element of the array can
be paired with another element that has the same value, except for one
element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.
Write a function:
def solution(A)
that, given an array A consisting of N integers fulfilling the above
conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the function should return 7, as explained in the example above.
Write an efficient algorithm for the following assumptions:
N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.
"""
def solution(A):
s = set()
for x in A:
if x in s:
s.remove(x)
else:
s.add(x)
return s.pop()

0
cnt1.py Normal file
View File

40
tc1.py Normal file
View File

@ -0,0 +1,40 @@
"""
An array A consisting of N different integers is given. The array
contains integers in the range [1..(N + 1)], which means that exactly
one element is missing.
Your goal is to find that missing element.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0..100,000];
the elements of A are all distinct;
each element of array A is an integer within the range [1..(N + 1)].
"""
def solution(A):
last = len(A) + 1
S = set(A)
if last == 1 or last not in S:
return last
for x in range(1, last):
if x not in S:
return x

78
tc2.py Normal file
View File

@ -0,0 +1,78 @@
"""
A non-empty array A consisting of N integers is given. Array A represents
numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts:
A[0], A[1], ..., A[P 1] and A[P], A[P + 1], ..., A[N 1].
The difference between the two parts is the value of:
|(A[0] + A[1] + ... + A[P 1]) (A[P] + A[P + 1] + ... + A[N 1])|
In other words, it is the absolute difference between the sum of the first part
and the sum of the second part.
For example, consider array A such that:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
We can split this tape in four places:
P = 1, difference = |3 10| = 7
P = 2, difference = |4 9| = 5
P = 3, difference = |6 7| = 1
P = 4, difference = |10 3| = 7
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A of N integers, returns the minimal difference
that can be achieved.
For example, given:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
the function should return 1, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [2..100,000];
each element of array A is an integer within the range [1,000..1,000].
"""
def solution(A):
size = len(A) - 1
if size == 1:
return abs(A[0] - A[1])
l = [A[0]]
r = [A[-1]]
for i in range(size):
l.append(l[i] + A[i+1])
r.append(r[i] + A[size-i-1])
res = abs(l[0] - r[size-1])
for i in range(1, size):
res = min(res, abs(l[i] - r[size-i-1]))
return res
res = solution([1, 1, 3])
assert res == 1, "got {}".format(res)
res = solution([3, 1, 2, 4, 3])
assert res == 1, "got {}".format(res)
res = solution([-2, -3, -4, -1])
assert res == 0, "got {}".format(res)
res = solution([1, 8, -10, 8, 7, 9, 11])
assert res == 6, "got {}".format(res)
res = solution([-1, 1, 0, 1, -1, 0, -1, 1, -1, 0, 1, 0, 1, -1, 0])
assert res == 0, "got {}".format(res)
res = solution([14, 9])
assert res == 5, "got {}".format(res)

35
tc3.py Normal file
View File

@ -0,0 +1,35 @@
"""
A small frog wants to get to the other side of the road. The frog is currently
located at position X and wants to get to a position greater than or equal to
Y. The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its
target.
Write a function:
class Solution { public int solution(int X, int Y, int D); }
that, given three integers X, Y and D, returns the minimal number of jumps from
position X to a position equal to or greater than Y.
For example, given:
X = 10
Y = 85
D = 30
the function should return 3, because the frog will be positioned as follows:
after the first jump, at position 10 + 30 = 40
after the second jump, at position 10 + 30 + 30 = 70
after the third jump, at position 10 + 30 + 30 + 30 = 100
Write an efficient algorithm for the following assumptions:
X, Y and D are integers within the range [1..1,000,000,000];
X Y.
"""
def solution(X, Y, D):
distance = Y - X
return distance // D if distance % D == 0 else distance // D + 1