2019-03-28vmidterm prep

This commit is contained in:
Claudio Maggioni 2019-03-28 11:45:47 +01:00
parent 8e46832ff3
commit 4a2d768b07
3 changed files with 96 additions and 0 deletions

37
midterm_prep/182.py Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env python3
import sys
def UFM(A):
if len(A) == 0:
return None
m = 0
M = len(A)
while M - 1 != m:
if M - 2 == m:
if A[m] > A[m + 1]:
return A[m]
else:
return A[m + 1]
lh = (M - m) // 2
if A[lh - 1] < A[lh] and A[lh] > A[lh + 1]:
return A[lh]
elif A[lh - 1] < A[lh]:
m = lh
else:
M = lh
return A[m]
A = []
for line in sys.stdin:
line = line.strip()
if line == '' or line == '\n':
break
A.append(int(line))
print(UFM(A))

View file

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import sys
def maximal_distance(A):
if len(A) == 0:
return None
max_n = A[0]
min_n = A[0]
for i in range(1,len(A)):
if max_n < A[i]:
max_n = A[i]
elif min_n > A[i]:
min_n = A[i]
return max_n - min_n
A = []
for line in sys.stdin:
if line[0] == '\n':
break
A.append(int(line))
print(maximal_distance(A))

33
midterm_prep/207.py Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env python3
import sys
def better_algo_X(A):
B = A.copy()
B.sort() # assume mergesort for theta (n * log(n))
x = 1
y = B[0]
k = 1
print(B)
for k in range(1, len(B)):
if B[k] == B[k - 1]:
k = k + 1
else:
if k > x:
y = B[k - 1]
x = k
k = 1
return y
A = []
for line in sys.stdin:
line = line.strip()
if line == '' or line == '\n':
break
A.append(int(line))
print(better_algo_X(A))