Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Binary file not shown.
17 changes: 17 additions & 0 deletions reports/Korol/lab6/src/hamming_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def hamming_distance(str1, str2):
if str1 is None and str2 is None:
raise TypeError("Both strings are None")

if str1 is None or str2 is None:
return -1

if len(str1) != len(str2):
raise ValueError("Strings must have equal length")

distance = 0

for char1, char2 in zip(str1, str2):
if char1 != char2:
distance += 1

return distance
24 changes: 24 additions & 0 deletions reports/Korol/lab6/src/shopping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Cart:
def __init__(self):
self.items = []

def add_item(self, name, price):
if price < 0:
raise ValueError("Price cannot be negative")

self.items.append(
{
"name": name,
"price": price,
}
)

def total(self):
return sum(item["price"] for item in self.items)

def apply_discount(self, discount):
if discount < 0 or discount > 100:
raise ValueError("Invalid discount")

for item in self.items:
item["price"] -= item["price"] * discount / 100
85 changes: 85 additions & 0 deletions reports/Korol/lab6/src/test_cart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from unittest.mock import patch

import pytest
import requests

from shopping import Cart


@pytest.fixture
def cart_fixture():
return Cart()


def test_add_item(cart_fixture):
cart_fixture.add_item("Apple", 10)

assert len(cart_fixture.items) == 1


def test_negative_price(cart_fixture):
with pytest.raises(ValueError):
cart_fixture.add_item("Apple", -10)


def test_total(cart_fixture):
cart_fixture.add_item("Apple", 10)

cart_fixture.add_item("Orange", 20)

assert cart_fixture.total() == 30


@pytest.mark.parametrize(
("discount", "result"),
[
(0, 100),
(50, 50),
(100, 0),
],
)
def test_apply_discount(discount, result):
cart = Cart()

cart.add_item("Apple", 100)

cart.apply_discount(discount)

assert cart.items[0]["price"] == result


@pytest.mark.parametrize(
"discount",
[-1, 101],
)
def test_invalid_discount(discount):
cart = Cart()

cart.add_item("Apple", 100)

with pytest.raises(ValueError):
cart.apply_discount(discount)


def log_purchase(item):
requests.post(
"https://example.com/log",
json=item,
timeout=10,
)


@patch("requests.post")
def test_log_purchase(mock_post):
item = {
"name": "Apple",
"price": 10,
}

log_purchase(item)

mock_post.assert_called_once_with(
"https://example.com/log",
json=item,
timeout=10,
)
45 changes: 45 additions & 0 deletions reports/Korol/lab6/src/test_hamming_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from hamming_distance import hamming_distance


def test_none_none():
with pytest.raises(TypeError):
hamming_distance(None, None)


def test_first_none():
assert hamming_distance(None, "abc") == -1


def test_second_none():
assert hamming_distance("abc", None) == -1


def test_different_length():
with pytest.raises(ValueError):
hamming_distance("abc", "abcd")


def test_empty_strings():
assert hamming_distance("", "") == 0


def test_equal_strings():
assert hamming_distance("father", "father") == 0


def test_one_difference():
assert hamming_distance("pip", "pop") == 1


def test_two_differences():
assert hamming_distance("abcd", "abab") == 2


def test_hello_hallo():
assert hamming_distance("hello", "hallo") == 1


def test_all_different():
assert hamming_distance("abcd", "efgi") == 4
Loading