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
from collections.abc import Sequence, Iterator | |
class RangeIterator(Iterator): | |
def __init__(self, range_object): | |
self.range_object = range_object | |
self.index = 0 | |
def __next__(self): | |
try: |
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
def chain(*args): | |
""" | |
>>> list(chain([[["test"]]])) | |
['t', 'e', 's', 't'] | |
""" | |
for arg in args: | |
if isinstance(arg, str): | |
yield from arg | |
elif isinstance(arg, Iterable): | |
yield from chain(*arg) |
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
from copy import deepcopy | |
def inverse_matrix(array): | |
augmented = deepcopy(array) | |
assert len(array) == len(array[0]) | |
n = len(array) | |
for i in range(n): | |
for j in range(n): | |
if i == j: |
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
class Ocean: | |
def __init__(self, init_state): | |
self.state = init_state | |
self.rows = len(self.state) | |
self.columns = len(self.state[0]) | |
def __str__(self): | |
return str(self.state) | |
def gen_next_quantum(self): |
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
class A: | |
_init = 0 | |
def __init__(self, a, **kwargs): | |
A._init += 1 | |
self.a = a | |
for key, value in kwargs.items(): | |
setattr(self, key, value) | |
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
from numbers import Real | |
from typing import List | |
def merge(left: List[Real], right: List[Real]) -> List[Real]: | |
result = [] | |
while left and right: | |
if left[0] >= right[0]: | |
result.append(right.pop(0)) | |
else: |
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
from math import log2 | |
import pytest | |
from binary_search import binary_search | |
class CounterElNumber: | |
_lt_global_counter = 0 | |
_eq_global_counter = 0 |
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
import pytest | |
import random | |
@pytest.fixture | |
def make_rnd_gen(): | |
def _get_result(seed, mode): | |
# Hint: use pytest.fixture | |
if mode == 0: | |
return random.Random(seed).randint(-1000, 1000) |
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
from lru_cache import LRUCache | |
from pytest import raises | |
def test_init(): | |
lru = LRUCache(4) | |
assert lru.maxsize == 4 | |
assert lru.cache == {} | |
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
import unittest | |
from math import sqrt | |
from vector import Vector | |
class TestVector(unittest.TestCase): | |
def test_repr(self): | |
v = Vector([1, 2, 3]) | |
self.assertEqual(str(v), "Vector([1, 2, 3])") |