From d724db3cca38f6792293c615102d4dfaf595d48e Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Tue, 23 Apr 2019 13:35:05 +0200 Subject: [PATCH] Dangling exercises for midterm --- midterm_prep/183-2.py | 30 +++++++++++++++++++++++++++ midterm_prep/184-1.py | 47 ++++++++++++++++++++++++++++++++++++++++++ midterm_prep/184-1.txt | 25 ++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 midterm_prep/183-2.py create mode 100755 midterm_prep/184-1.py create mode 100644 midterm_prep/184-1.txt diff --git a/midterm_prep/183-2.py b/midterm_prep/183-2.py new file mode 100644 index 0000000..ae5060e --- /dev/null +++ b/midterm_prep/183-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys +import math + +def better_algo_X(A, k): + l = -1 * math.inf + r = math.inf + for i in range(0, len(A)): + c = 0 + for j in range(i, len(A)): + print("i =", i, "j =", j, "c =", c) + if A[i] == A[j]: + c = c + 1 + if c >= k: + if r - l > j - i: + l = i + r = j + print("l =", l, "r = ", r) + break + return [l, r] + +A = [] +for line in sys.stdin: + line = line.strip() + if line == '' or line == '\n': + break + A.append(int(line)) + +print(better_algo_X(A[1:], A[0])) diff --git a/midterm_prep/184-1.py b/midterm_prep/184-1.py new file mode 100755 index 0000000..24e2fda --- /dev/null +++ b/midterm_prep/184-1.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import sys +import math + +def three_way_partition(A, begin, end): + i = 0 + j = end - 2 + p = end - 1 + while i < j: + print(A[begin:i], A[i:j+1], A[j+1:p], A[p:end]) + if A[i] == A[p]: + p = p - 1 + A[i], A[p] = A[p], A[i] + if j == p: + j = j - 1 + elif A[j] == A[p]: + p = p - 1 + A[j], A[p] = A[p], A[j] + if j == p: + j = j - 1 + elif A[i] < A[p]: + i = i + 1 + elif A[j] > A[p]: + j = j - 1 + else: + A[i], A[j] = A[j], A[i] + + m = end - 1 + while i < p: + print(A) + A[i], A[m] = A[m], A[i] + m = m - 1 + i = i + 1 + + return [j, m + 1] + +if __name__ == "__main__": + A = [] + for line in sys.stdin: + line = line.strip() + if line == '' or line == '\n': + break + A.append(int(line)) + + print(three_way_partition(A, 0, len(A))) + print(A) diff --git a/midterm_prep/184-1.txt b/midterm_prep/184-1.txt new file mode 100644 index 0000000..968beaa --- /dev/null +++ b/midterm_prep/184-1.txt @@ -0,0 +1,25 @@ +1 +2 +3 +4 +5 +5 +5 +4 +32 +21 +6 +8 +34 +98 +3426 +56 +3 +67 +2 +6 +2 +6 +23 +65 +