From 211e38a3072444fc2f0d5e3751345e4668d390df Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Sun, 10 May 2020 03:14:42 +0200 Subject: [PATCH] All done in python so far --- .gitignore | 3 +++ a2.py | 54 +++++++++++++++++++++++++++++++++++++ a3.py | 45 +++++++++++++++++++++++++++++++ cnt1.py | 0 tc1.py | 40 ++++++++++++++++++++++++++++ tc2.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tc3.py | 35 ++++++++++++++++++++++++ 7 files changed, 255 insertions(+) create mode 100644 .gitignore create mode 100644 a2.py create mode 100644 a3.py create mode 100644 cnt1.py create mode 100644 tc1.py create mode 100644 tc2.py create mode 100644 tc3.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ca5afe --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +#.# +*~ +*.sw? diff --git a/a2.py b/a2.py new file mode 100644 index 0000000..f6d5f0b --- /dev/null +++ b/a2.py @@ -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 2009–2020 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 diff --git a/a3.py b/a3.py new file mode 100644 index 0000000..9aca566 --- /dev/null +++ b/a3.py @@ -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() diff --git a/cnt1.py b/cnt1.py new file mode 100644 index 0000000..e69de29 diff --git a/tc1.py b/tc1.py new file mode 100644 index 0000000..9812fea --- /dev/null +++ b/tc1.py @@ -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 + diff --git a/tc2.py b/tc2.py new file mode 100644 index 0000000..6f9c5a2 --- /dev/null +++ b/tc2.py @@ -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) diff --git a/tc3.py b/tc3.py new file mode 100644 index 0000000..512fef6 --- /dev/null +++ b/tc3.py @@ -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