Dangling exercises for midterm

This commit is contained in:
Claudio Maggioni (maggicl) 2019-04-23 13:35:05 +02:00
parent 0151274467
commit d724db3cca
3 changed files with 102 additions and 0 deletions

30
midterm_prep/183-2.py Normal file
View file

@ -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]))

47
midterm_prep/184-1.py Executable file
View file

@ -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)

25
midterm_prep/184-1.txt Normal file
View file

@ -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