-
Notifications
You must be signed in to change notification settings - Fork 20
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
Кабанова №7,14,16 #19
Open
catr1ne55
wants to merge
31
commits into
MikhailErofeev:master
Choose a base branch
from
catr1ne55:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
01fef8f
Task 2 completed
fa9271c
Delete package
catr1ne55 3d7abb0
Task 3
fefbe34
Merge branch 'master' of https://github.com/ekabanova/a3200-2015-algs
10a69e1
Task 4 completed
8092125
Task 5 completed
6469d0a
Task 6
1a521d7
Task 4. Added k
ee3d42c
Changed coins.py in task 3
aa61ce4
Task 8. Part 1
2b7d143
Task 11
77cb81b
Task 8
946b271
Merge branch 'lab11'
167cfa8
Delete queue.py
catr1ne55 550304a
Task 12
5054e0f
Merge branch 'master' of https://github.com/ekabanova/a3200-2015-algs
d0502cd
Task 10
06231ae
Task 16
d6c66fa
Modified lab6 and lab11
675dadb
Modifed lab10
3d35260
Task 13
fdac92b
Task 18
62c644a
Task 12 modifed
3c6e713
Task 19
40f2678
Task 20
8eb92f3
Task 14
95c58d4
Delete dejkstra.py
catr1ne55 3b35d51
Delete test_dejkstra.py
catr1ne55 5ac96a0
Task 16 modifed
27b03e8
Merge branch 'master' of https://github.com/ekabanova/a3200-2015-algs
747d779
Task 7
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import sys | ||
|
||
|
||
def find_triples(array): | ||
number_of_triples = 0 | ||
array.sort() | ||
length = len(array) | ||
for i in range(0, length): | ||
array[i] = array[i]*array[i] | ||
for i in range(0, length): | ||
j = 0 | ||
k = i | ||
while j != k: | ||
if array[j] + array[k] == array[i]: | ||
number_of_triples += 1 | ||
k -= 1 | ||
else: | ||
if array[j] + array[k] < array[i]: | ||
j += 1 | ||
else: | ||
k -= 1 | ||
return number_of_triples | ||
|
||
|
||
if __name__ == "__main__": | ||
array_of_elements = [int(k) for k in sys.stdin.readline().split(' ')] | ||
sys.stdout.write(str(find_triples(array_of_elements))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import unittest | ||
|
||
from pythagorean_triples import find_triples | ||
|
||
|
||
class TestTriples(unittest.TestCase): | ||
|
||
def test_empty_array(self): | ||
array = [] | ||
result = find_triples(array) | ||
expected = 0 | ||
self.assertEqual(expected, result) | ||
|
||
def test_example(self): | ||
array = [23, 247, 19, 96, 264, 265, 132, 181] | ||
result = find_triples(array) | ||
expected = 2 | ||
self.assertEqual(expected, result) | ||
|
||
def test_another(self): | ||
array = [3, 4, 6, 10, 56] | ||
result = find_triples(array) | ||
expected = 0 | ||
self.assertEqual(expected, result) | ||
|
||
def test_another_1(self): | ||
array = [4, 3, 5] | ||
result = find_triples(array) | ||
expected = 1 | ||
self.assertEqual(expected, result) | ||
|
||
def test_another_2(self): | ||
array = [4, 3, 5, 144, 17, 145] | ||
result = find_triples(array) | ||
expected = 2 | ||
self.assertEqual(expected, result) | ||
|
||
def test_another_3(self): | ||
array = [4, 3, 5, 144, 17] | ||
result = find_triples(array) | ||
expected = 1 | ||
self.assertEqual(expected, result) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import sys | ||
|
||
|
||
def max_square(array): | ||
max_elem = array[0] | ||
square = 0 | ||
maxsquare = 0 | ||
if len(array) == 1: | ||
return 0 | ||
else: | ||
for i in range(1, len(array)): | ||
if max_elem <= array[i]: | ||
if square > maxsquare: | ||
maxsquare = square | ||
square = 0 | ||
max_elem = array[i] | ||
else: | ||
square += max_elem - array[i] | ||
return maxsquare | ||
|
||
if __name__ == "__main__": | ||
array_of_elements = [int(k) for k in sys.stdin.readline().split(' ')] | ||
sys.stdout.write(str(max_square(array_of_elements))) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import unittest | ||
|
||
from histogram import max_square | ||
|
||
|
||
class TestMaxSquare(unittest.TestCase): | ||
def test_empty_array(self): | ||
array = [] | ||
result = max_square(array) | ||
expected = 0 | ||
self.assertEqual(expected, result) | ||
|
||
def test_one_elem(self): | ||
array = [2] | ||
result = max_square(array) | ||
expected = 0 | ||
self.assertEqual(expected, result) | ||
|
||
def test_three_elem(self): | ||
array = [2, 5, 1] | ||
result = max_square(array) | ||
expected = 4 | ||
self.assertEqual(expected, result) | ||
|
||
def test_example(self): | ||
array = [2, 5, 1, 2, 3, 4, 7, 7, 6] | ||
result = max_square(array) | ||
expected = 10 | ||
self.assertEqual(expected, result) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import unittest | ||
|
||
from bst_tree import UnbalancedBinarySearchTree | ||
|
||
|
||
class TestSorting(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тесты падают |
||
def test_empty(self): | ||
bst_tree_set = UnbalancedBinarySearchTree() | ||
result = [] | ||
for v in bst_tree_set: | ||
result.append(v) | ||
expected = [] | ||
self.assertEqual(expected, result) | ||
|
||
def test_1(self): | ||
bst_tree_set = UnbalancedBinarySearchTree() | ||
bst_tree_set.add(1) | ||
result = [] | ||
for v in bst_tree_set: | ||
result.append(v) | ||
expected = [1] | ||
self.assertEqual(expected, result) | ||
|
||
def test_2(self): | ||
bst_tree_set = UnbalancedBinarySearchTree() | ||
bst_tree_set.add(1) | ||
bst_tree_set.add(2) | ||
bst_tree_set.add(9) | ||
bst_tree_set.add(4) | ||
bst_tree_set.add(3) | ||
result = [] | ||
for v in bst_tree_set: | ||
result.append(v) | ||
expected = [1, 2, 3, 4, 9] | ||
self.assertEqual(expected, result) | ||
|
||
def test_3(self): | ||
bst_tree_set = UnbalancedBinarySearchTree() | ||
bst_tree_set.add(4) | ||
bst_tree_set.add(2) | ||
bst_tree_set.add(0) | ||
bst_tree_set.add(1) | ||
bst_tree_set.add(3) | ||
result = [] | ||
for v in bst_tree_set: | ||
result.append(v) | ||
expected = [0, 1, 2, 3, 4] | ||
self.assertEqual(expected, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
__author__ = 'catherinekabanova' | ||
|
||
|
||
class Set: | ||
def add(self, value): | ||
pass | ||
|
||
def iterate(self): | ||
pass | ||
|
||
|
||
class Node: | ||
def __init__(self, val): | ||
self.val = val | ||
self.left = None | ||
self.right = None | ||
self.parent = None | ||
|
||
|
||
class UnbalancedBinarySearchTree(Set): | ||
def __init__(self): | ||
self.root = None | ||
|
||
def add(self, value): | ||
if self.root is None: | ||
self.root = Node(value) | ||
return | ||
z = Node(value) | ||
y = None | ||
x = self.root | ||
while x is not None: | ||
y = x | ||
if value < x.val: | ||
x = x.left | ||
elif value > x.val: | ||
x = x.right | ||
else: | ||
return | ||
z.parent = y | ||
if value < y.val: | ||
y.left = z | ||
else: | ||
y.right = z | ||
|
||
def search(self, x, value): | ||
if x is None: | ||
return False | ||
elif x.value == value: | ||
return True | ||
if value < x.value: | ||
return self.search(x.left, value) | ||
else: | ||
return self.search(x.right, value) | ||
|
||
def iterate(self): | ||
tree_iterator = TreeIterator(self.root) | ||
while tree_iterator.has_next(): | ||
yield tree_iterator.next() | ||
|
||
def __iter__(self): | ||
return self.iterate() | ||
|
||
|
||
class TreeIterator: | ||
def __init__(self, root): | ||
root = self.find_smallest(root) | ||
self.min = Node(None) | ||
self.min.parent = root | ||
|
||
def has_next(self): | ||
slider = self.min | ||
if slider.right is not None: | ||
return True | ||
while slider.parent is not None and slider is slider.parent.right: | ||
slider = slider.parent | ||
if slider.parent is None: | ||
return False | ||
return True | ||
|
||
def next(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. у этого генератора нет завершения |
||
self.min = self.find_next(self.min) | ||
return self.min.val | ||
|
||
def find_smallest(self, root): | ||
if root is None: | ||
return None | ||
while root.left is not None: | ||
root = root.left | ||
return root | ||
|
||
def find_next(self, node): | ||
if node.right is not None: | ||
node = node.right | ||
while node.left is not None: | ||
node = node.left | ||
return node | ||
else: | ||
while node.parent is not None and node is node.parent.right: | ||
node = node.parent | ||
return node.parent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
__author__ = 'catherinekabanova' | ||
import unittest | ||
import splay_tree | ||
|
||
|
||
class TestSplayTree(unittest.TestCase): | ||
|
||
def test_0_empty(self): | ||
tree = splay_tree.SplayTree() | ||
self.assertFalse(tree.contains(6)) | ||
|
||
def test_1_iter(self): | ||
tree = splay_tree.SplayTree() | ||
tree.insert(0) | ||
tree.insert(6) | ||
tree.insert(2) | ||
tree.insert(9) | ||
array = [] | ||
for v in tree: | ||
array.append(v.key) | ||
expected = [0, 2, 6, 9] | ||
self.assertEquals(expected, array) | ||
|
||
def test_2_contain(self): | ||
tree = splay_tree.SplayTree() | ||
array = [5, 75, 63, 89, 10, 11] | ||
for elem in array: | ||
tree.insert(elem) | ||
self.assertFalse(tree.contains(100)) | ||
self.assertTrue(tree.contains(11)) | ||
|
||
def test_3_remove(self): | ||
tree = splay_tree.SplayTree() | ||
array = [5, 75, 63, 89, 10, 11] | ||
for elem in array: | ||
tree.insert(elem) | ||
tree.remove(89) | ||
tree.remove(5) | ||
array_out = [] | ||
for v in tree: | ||
array_out.append(v.key) | ||
self.assertEqual(array_out, [10, 11, 63, 75]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тесты похоже не запускались, первые 3 падают
но алгоритм правильный