Last active
October 27, 2020 12:55
-
-
Save perevertysh/7b5846cddd7de7c38520247d6b7e5178 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from typing import List, Set | |
from heapq import nlargest | |
def ad_least_twice(ranks: List) -> bool: | |
return large_minimum_twice(ranks) | |
def exactly_twice(ranks: List) -> bool: | |
ranks = set(ranks) | |
largest = max(ranks) | |
ranks.remove(largest) | |
second = max(ranks) | |
ranks.remove(second) | |
if largest >= second * 2: | |
return True | |
return False | |
def constrained_exactly_twice(ranks: List) -> bool: | |
pass | |
########################################################################### | |
# Возможно, имелась в виду эта задача: | |
# https://stackoverflow.com/questions/50543265/find-whether-the-largest-element-in-the-array-is-at-least-twice-as-much-as-every | |
def large_minimum_twice(ranks: List) -> bool: | |
a, b = nlargest(2, range(len(ranks)), key=ranks.__getitem__) | |
return True if ranks[a] >= 2 * ranks[b] else False |
This file contains hidden or 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
import time | |
import unittest | |
from room import (exactly_twice, | |
ad_least_twice, | |
large_minimum_twice, | |
constrained_exactly_twice) | |
class TestRoomMethods(unittest.TestCase): | |
def check_room_method(self, method, items_true, items_false): | |
start = time.time() | |
check_true = method(items_true) | |
print(method, time.time() - start) | |
start = time.time() | |
check_false = method(items_false) | |
print(method, time.time() - start, "\n") | |
self.assertTrue( | |
check_true, | |
f"True != {check_true}, {items_true}" | |
) | |
self.assertFalse( | |
check_false, | |
f"False != {check_false}, {items_false}" | |
) | |
def test_at_least_twice(self): | |
"""Check method Room.at_least_twice""" | |
rooms_true = [5, 6, 0, 7, 8, 16] | |
rooms_false = [5, 5, 0, 45, 40, 10] | |
print(f"""Check method at_least_twice: \n""") | |
self.check_room_method( | |
ad_least_twice, | |
rooms_true, | |
rooms_false | |
) | |
def test_exactly_twice(self): | |
"""Check method exactly_twice""" | |
rooms_true = [5, 6, 0, 7, 8, 16] | |
rooms_false = [50, 0, 35, 45, 40] | |
print(f"""Check method exactly_twice: \n""") | |
self.check_room_method( | |
exactly_twice, | |
rooms_true, | |
rooms_false | |
) | |
def test_constrained_exactly_twice(self): | |
"""Check method constrained_exactly_twice""" | |
rooms_true = [5, 6, 0, 7, 8, 16] | |
rooms_false = [50, 0, 35, 45, 40] | |
print(f"""Check method constrained_exactly_twice: \n""") | |
self.check_room_method( | |
constrained_exactly_twice, | |
rooms_true, | |
rooms_false | |
) | |
def test_largest_minimum_twice(self): | |
"""Check method largest_minimum_twice""" | |
rooms_true = [5, 6, 0, 7, 8, 16] | |
rooms_false = [5, 5, 0, 45, 40, 10] | |
print(f"""Check method largest_minimum_twice: \n""") | |
self.check_room_method( | |
large_minimum_twice, | |
rooms_true, | |
rooms_false | |
) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment