Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Петров, 5, 15, 16, 17 #69

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lab10/Petrov/lab10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from sys import stdin

def f(A):
if len(A) == 0:
return []
A.sort()
b = []
for i in A:
if not i in b:
b.append(i)
A = b
res = 0
for c in A:
l = len(A) - 1
for a in A:
while (l > 0 and c ** 2 - a ** 2 < A[l] ** 2):
l -= 1
if (c ** 2 - a ** 2 == A[l] ** 2 and A[l] < a):
res += 1
return res
31 changes: 31 additions & 0 deletions lab10/Petrov/test10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest
import random
from lab10 import f

class TestPythagoras(unittest.TestCase):
def testmain(self, c):
B = []
for i in c:
if not i in B:
B.append(i)
expected = 0
for i in range(len(B)):
for j in range(i + 1, len(B)):
for k in range(j + 1, len(B)):
if B[i]**2 + B[j]**2 == B[k]**2:
expected += 1
res = f(B)
self.assertEqual(res, expected)

def test_empty(self):
self.testmain([])

def test_std(self):
self.testmain([23, 247, 19, 96, 264, 265, 132, 265, 181])

def test_duplicates(self):
self.testmain([3, 4, 3, 3, 5, 4, 1, 1, 1, 4, 5, 3, 4, 2, 2, 2, 5, 5, 4, 3, 2])

def test_random(self):
c = [randint(-11111, 11111) for i in range(111)]
self.testmain(c)
26 changes: 26 additions & 0 deletions lab11/Petrov/lab11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from sys import stdin
#a = list(map(int, stdin.readline().split()))

def f(A):
res = 0
c = -1
s = 0
for i in range(len(A)):
if A[i] >= c:
res = max(res, s)
s = 0
c = A[i]
else:
s += c - A[i]
c = -1
s = 0
for i in range(0, len(A), -1):
if A[i] >= c:
res = max(res, s)
s = 0
c = A[i]
else:
s += c - A[i]
return res

#print(f(a))
38 changes: 38 additions & 0 deletions lab11/Petrov/test11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest
import random
import lab11

class TestSorting(unittest.TestCase):

def test_trivial(self):
arr = [1, 333, 22]
res = lab11.f(arr)
expected = 0
self.assertEqual(expected, res)

def test_empty(self):
arr = []
res = lab11.f(arr)
expected = 0
self.assertEqual(expected, res)

def test_moun(self):
arr = ([1, 10, 0, 0, 0, 11])
res = lab11.f(arr)
expected = 30
self.assertEqual(expected, res)

def test_std(self):
arr = [2, 5, 1, 2, 3, 4, 7, 7, 6]
res = lab11.f(arr)
expected = 10
self.assertEqual(expected, res)

def test_moun1(self):
arr = [10, 1, 1, 1, 1, 1, 1, 1, 1, 10]
res = lab11.f(arr)
expected = 72
self.assertEqual(expected, res)

if __name__ == '__main__':
unittest.main()
88 changes: 88 additions & 0 deletions lab9/lab9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import random


class Heap:
a = []
def _down(self, i):
while 2 * i + 1 < len(self.a):
left = 2 * i + 1
right = 2 * i + 2
j = left
if right < len(self.a) and self.a[right] < self.a[left]:
j = right
if self.a[i] <= self.a[j]:
break
self.a[i], self.a[j] = self.a[j], self.a[i]
i = j
def _up(self, i):
while i > 0 and self.a[i] < self.a[(i - 1) // 2]:
self.a[i], self.a[(i - 1) // 2] = self.a[(i - 1) // 2], self.a[i]
i = (i - 1) // 2
def min(self):
res = self.a[0]
if (len(self.a) > 1):
self.a[0] = self.a.pop()
else:
self.a.pop()
self._down(0)
return res
def insert(self, k):
self.a.append(k)
self._up(len(self.a) - 1)

def part1(a, k):
h = Heap()
for i in a:
h.insert(i)
if (len(h.a) > k):
h.min()
return h.a

def _partition(a, l, r):
t = [a[random.randint(l, r)], a[l], a[r]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вы нашли медиану между 3 случайными точками, кажется этого не достаточно, чтобы добиться O(n), как требовалось в задании

if t[0] > t[1]:
t[0], t[1] = t[1], t[0]
if t[1] > t[2]:
t[1], t[2] = t[2], t[1]
if t[0] > t[1]:
t[0], t[1] = t[1], t[0]
x = t[1]
i = l
j = r
while 1:
while a[i] < x:
i = i + 1
while a[j] > x:
j = j - 1
if i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
else:
return j


def part2(a, n):
left = 0
right = len(a) - 1
k = len(a) - n - 1
b = sorted(a)
while 1:
mid = _partition(a, left, right)
if mid == k:
return a[-n:]
elif k < mid:
right = mid
else:
left = mid + 1


def test():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тесты в отдельном файле с помощью unittest, пожалуйста
и консольный интерфейс сделайте, пожалуйста, к каждой реализации.

a = random.sample(range(100), 50) + random.sample(range(100), 50)
r1 = part1(a, 40)
r2 = part2(a, 40)
r = sorted(a)[-40:]
print(r == sorted(r1))
print(r == sorted(r2))

test()