From 201ab6837030ec3c720888179232072448d31302 Mon Sep 17 00:00:00 2001 From: Tatiana Muromtseva Date: Mon, 15 Dec 2025 15:24:39 +0300 Subject: [PATCH 1/3] add code files --- src/test_2/hamming_code.py | 79 ++++++++++++++++++++++++++++++++++++ src/test_2/walkers_scheme.py | 27 ++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/test_2/hamming_code.py create mode 100644 src/test_2/walkers_scheme.py diff --git a/src/test_2/hamming_code.py b/src/test_2/hamming_code.py new file mode 100644 index 0000000..ee6633d --- /dev/null +++ b/src/test_2/hamming_code.py @@ -0,0 +1,79 @@ +def hamming_encode(data): + m = len(data) + r = 0 # это контрольные биты + while (2**r) < (m + r + 1): + r += 1 + total_length = m + r + + encoded = [0] * total_length + + j = 0 + for i in range(1, total_length + 1): + # если позиции - степени двойки + if (i & (i - 1)) == 0: + continue + else: + encoded[i - 1] = data[j] + j += 1 + + for i_power in range(r): + index = 2**i_power + count_ones = 0 + for i in range(1, total_length + 1): + if i & index: + if encoded[i - 1] == 1: + count_ones += 1 + if count_ones % 2 != 0: + parity = 1 + else: + parity = 0 + # значение контрольного бита + encoded[index - 1] = parity + return encoded + + +def hamming_decode(encoded): + total_length = len(encoded) + r = 0 + while (2**r) <= total_length: + r += 1 + + error_position = 0 + + for i_power in range(r): + index = 2**i_power + count_ones = 0 + for i in range(1, total_length + 1): + if i & index: + if encoded[i - 1] == 1: + count_ones += 1 + if count_ones % 2 != 0: + error_position += index + + data = [] + for i in range(1, total_length + 1): + if (i & (i - 1)) != 0: + data.append(encoded[i - 1]) + + return data, error_position + + +input_str = input("Введите строку битов для шифрования: ").strip() + +if not all(c in "01" for c in input_str): + print("Ошибка! строка должна состоять только из 0 и 1") + exit() + +data_bits = [int(c) for c in input_str] + +# Кодируем +encoded_message = hamming_encode(data_bits) +print("Закодированное сообщение:", "".join(map(str, encoded_message))) + +# Декодируем +corrected_data, error_pos = hamming_decode(encoded_message) + +if error_pos == 0: + print("Ошибок не обнаружено") +else: + print(f"Обнаружена ошибка в позиции: {error_pos}") diff --git a/src/test_2/walkers_scheme.py b/src/test_2/walkers_scheme.py new file mode 100644 index 0000000..49b88ba --- /dev/null +++ b/src/test_2/walkers_scheme.py @@ -0,0 +1,27 @@ +import random + + +class WalkerScheme: + def __init__(self, events): + # Проверяем, что список не пустой + if not events: + raise ValueError("Пустой список событий") + total_prob = 0 + self.cumulative = [] + + # пороги, где заканчивается вероятность каждого события + for event, prob in events: + if prob < 0: + raise ValueError("Вероятность не может быть отрицательной") + total_prob += prob + self.cumulative.append((total_prob, event)) + # Проверка суммы вероятностей + if abs(total_prob - 1) > 1e-6: + raise ValueError("Сумма вероятностей должна быть равна 1") + + def get_random(self): + r = random.random() + for boundary, event in self.cumulative: + if r <= boundary: + return event + return self.cumulative[-1][1] From a5237beadec67a0c870b6c3444cbcc59cf41dd50 Mon Sep 17 00:00:00 2001 From: Tatiana Muromtseva Date: Mon, 15 Dec 2025 15:52:06 +0300 Subject: [PATCH 2/3] add test wor walkers scheme --- tests/test_walkers_scheme.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/test_walkers_scheme.py diff --git a/tests/test_walkers_scheme.py b/tests/test_walkers_scheme.py new file mode 100644 index 0000000..1a7c11c --- /dev/null +++ b/tests/test_walkers_scheme.py @@ -0,0 +1,25 @@ +import pytest +from src.test_2.walkers_scheme import WalkerScheme + + +def test_empty(): + with pytest.raises(ValueError): + WalkerScheme([]) + + +def test_negative_prob(): + with pytest.raises(ValueError): + WalkerScheme([("a", 0.5), ("b", -0.1)]) + + +def test_not_one_sum(): + with pytest.raises(ValueError): + WalkerScheme([("a", 0.3), ("b", 0.3)]) # суммарно 0.6 + + +def test_returns_valid_event(): + scheme = WalkerScheme([("a", 0.5), ("b", 0.5)]) + # Проверка случайного выбора + for _ in range(10): + event = scheme.get_random() + assert event in ("a", "b") From 228750cb6312a833de69d59c8166db7139b10eb6 Mon Sep 17 00:00:00 2001 From: Tatiana Muromtseva Date: Mon, 15 Dec 2025 15:53:48 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20CI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..56c2ac4 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,19 @@ +name: Ruff +on: push +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Python + uses: actions/setup-python@v5 + with: + python-version: "3.13.7" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install ruff pytest + - name: Run Ruff + run: ruff check --output-format=github . + - name: Run unit-tests + run: python -m pytest tests/*