Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.
Merged
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
54 changes: 39 additions & 15 deletions src/bisearch/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ class NotFound(Exception):


def bisect_right(origin: List[str], search: str = "") -> int:
"""Return the most right one index matching the search value
"""
Return the most right one index matching the search value

Current function implements common ``bisect_right`` algorithms.
The search value represents prefix the string should starts with,
that's why current string is trimmed before comparison operation.

:param origin: a list of strings
:type origin: list[str]
:type origin: list
:param search: a prefix to search
:type search: str

:return: the index after the last string starting with search prefix
:rtype: int

:raise: NotFound
:raise NotFound: if the searched value isn't in the list

Current function implements common ``bisect_right`` algorithms.
The search value represents prefix the string should starts with,
that's why current string is trimmed before comparison operation.
Usage:

>>> data = ["apple", "apple", "banana", "banana", "cherry"]
>>> assert bisect_right(data, "a") == 2
>>> assert bisect_right(data, "b") == 4
>>> assert bisect_right(data, "c") == 5

"""

Expand All @@ -55,21 +63,29 @@ def bisect_right(origin: List[str], search: str = "") -> int:


def bisect_left(origin: List[str], search: str = "") -> int:
"""Return the most left one index matching the search value
"""
Return the most left one index matching the search value

Current function implements common ``bisect_left`` algorithms.
The search value represents prefix the string should starts with,
that's why current string is trimmed before comparison operation.

:param origin: a list of strings
:type origin: list[str]
:type origin: list
:param search: a prefix to search
:type search: str

:return: the index of the first string starting with search prefix
:rtype: int

:raise: NotFound
:raise NotFound: if the searched value isn't in the list

Current function implements common ``bisect_left`` algorithms.
The search value represents prefix the string should starts with,
that's why current string is trimmed before comparison operation.
Usage:

>>> data = ["apple", "apple", "banana", "banana", "cherry"]
>>> assert bisect_left(data, "a") == 0
>>> assert bisect_left(data, "b") == 2
>>> assert bisect_left(data, "c") == 4

"""

Expand All @@ -93,15 +109,23 @@ def bisect_left(origin: List[str], search: str = "") -> int:


def find_all(origin: List[str], search: str = "") -> List[str]:
"""Return strings starting with prefix
"""
Return strings starting with prefix

:param origin: the list of strings
:type origin: list[str]
:type origin: list
:param search: the prefix to search, defaults to empty string
:type search: str

:return: the list of strings starting with the search prefix
:rtype: list[str]
:rtype: list

Usage:

>>> data = ["apple", "apple", "banana", "banana", "cherry"]
>>> assert find_all(data, "a") == ["apple", "apple"]
>>> assert find_all(data, "b") == ["banana", "banana"]
>>> assert find_all(data, "c") == ["cherry"]

"""

Expand Down
54 changes: 33 additions & 21 deletions src/calc/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@


def get_factorial(number: int, /) -> int:
"""Return the factorial value for a given number
"""
Return the factorial value for a given number

In mathematics the factorial is the product of all positive integers
less than or equal to given number.
E.g. 5! = 5 * 4! = 5 * 4 * 3 * 2 * 1 = 120.
The value of 0! = 1 according to the convention of an empty product.

:param number:
:type number: int

:return: the factorial value
:rtype: int

In mathematics the factorial is the product of all positive integers
less than or equal to given number.
E.g. 5! = 5 * 4! = 5 * 4 * 3 * 2 * 1 = 120.
The value of 0! = 1 according to the convention of an empty product.

Usage examples:
Usage:

>>> assert get_factorial(0) == 1
>>> assert get_factorial(5) == 120
Expand All @@ -32,19 +33,20 @@ def get_factorial(number: int, /) -> int:


def get_fibonacci_number(idx: int, /) -> int:
"""Return a Fibonacci's sequence number at a specified index
"""
Return a Fibonacci's sequence number at a specified index

The Fibonacci number is a number from the Fibonacci sequence, in which
each number is the sum of the two preceding ones. This sequence commonly
starts from 0 and 1: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...

:param idx: a Fibonacci sequence index starting from 0
:type idx: int

:return: a sequence's member
:rtype: int

The Fibonacci number is a number from the Fibonacci sequence, in which
each number is the sum of the two preceding ones. This sequence commonly
starts from 0 and 1: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...

Usage examples:
Usage:

>>> assert get_fibonacci_number(0) == 0
>>> assert get_fibonacci_number(1) == 1
Expand All @@ -64,16 +66,25 @@ def get_fibonacci_number(idx: int, /) -> int:


def get_fibonacci_number_nr(idx: int, /) -> int:
"""Return a Fibonacci's sequence number at a specified index
"""
Return a Fibonacci's sequence number at a specified index

This function implements the non-recursive algorithm, which is more
efficient, since it does not have multiple recursive calls.

:param idx: a Fibonacci sequence index starting from 0
:type idx: int

:return: a sequence's member
:rtype: int

This function implements the non-recursive algorithm, which is more
efficient, since it does not have multiple recursive calls.
Usage:

>>> assert get_fibonacci_number(0) == 0
>>> assert get_fibonacci_number(1) == 1
>>> assert get_fibonacci_number(2) == 1
>>> assert get_fibonacci_number(3) == 2
>>> assert get_fibonacci_number(4) == 3

"""

Expand All @@ -91,7 +102,11 @@ def get_fibonacci_number_nr(idx: int, /) -> int:


def get_sum_of_strings(number_1: str, number_2: str, /) -> str:
"""Return the sum of two numbers of string type as string
"""
Return the sum of two numbers of string type as string

Valid input is a string of any length containing numeric characters from
0 to 9. Empty strings are allowed as well and should be considered as 0.

:param number_1: first number
:type number_1: str
Expand All @@ -101,10 +116,7 @@ def get_sum_of_strings(number_1: str, number_2: str, /) -> str:
:return: the sum of two numbers
:rtype: str

Valid input is a string of any length containing numeric characters from
0 to 9. Empty strings are allowed as well and should be considered as 0.

Usage examples:
Usage:

>>> assert get_sum_of_strings("123", "456") == "579"
>>> assert get_sum_of_strings("099", "001") == "100"
Expand Down
15 changes: 13 additions & 2 deletions src/chess/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@


def within_board(position: Tuple[int, int]) -> bool:
"""Check if position is within a chess board
"""
Check if position is within a chess board

:param position: a position to check
:type position: tuple

:return: True if position is within a chess board, otherwise False
:rtype: bool

Usage:

>>> assert within_board((2, 2)) is True
>>> assert within_board((3, 3)) is True
>>> assert within_board((4, 4)) is True
>>> assert within_board((5, -5)) is False
>>> assert within_board((-5, 5)) is False
>>> assert within_board((5, 50)) is False

"""

position_x, position_y = position
Expand All @@ -29,7 +39,8 @@ def within_board(position: Tuple[int, int]) -> bool:

def filter_can_move(pieces: List[Piece],
position: Tuple[int, int]) -> List[Piece]:
"""Filter the list of chess piece
"""
Filter the list of chess piece

:param pieces: a list of chess pieces
:type pieces: list
Expand Down
19 changes: 12 additions & 7 deletions src/chess/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@


class Piece:
"""Chess piece model
"""
Chess piece model

:ivar position: the position on a chess board
:type position: tuple
Expand All @@ -25,12 +26,13 @@ class Piece:
def __init__(self,
is_white: bool = True,
position: Tuple[int, int] = (0, 0)) -> None:
"""Initialize instance
"""
Initialize instance

:param is_white: indicating if a piece is white. Defaults to True.
:type is_white: bool
:type is_white: bool, optional
:param position: initial piece position. Defaults to (0, 0).
:type position: tuple
:type position: tuple, optional

"""

Expand All @@ -48,7 +50,8 @@ def swap_color(self) -> None:
self.is_white = not self.is_white

def set_position(self, position: Tuple[int, int]) -> None:
"""Change piece position to a specified one
"""
Change piece position to a specified one

:param position: new position for a piece
:type position: tuple
Expand All @@ -62,7 +65,8 @@ def set_position(self, position: Tuple[int, int]) -> None:
logger.warning("Position %s is outside the board", position)

def can_move(self, position: Tuple[int, int]) -> bool:
"""Check if a move to a specified position is valid
"""
Check if a move to a specified position is valid

:param position: a position to check
:type position: tuple
Expand All @@ -75,7 +79,8 @@ def can_move(self, position: Tuple[int, int]) -> bool:
raise NotImplementedError

def get_delta(self, position: Tuple[int, int]) -> Tuple[int, int]:
"""Return the deltas between current position and the specified one
"""
Return the deltas between current position and the specified one

:param position: a position to calculate delta with
:type position: tuple
Expand Down
1 change: 1 addition & 0 deletions src/chess/symbols.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Chess pieces unicode symbols

"""

WHITE_KING = chr(0x2654)
Expand Down
18 changes: 10 additions & 8 deletions src/conv_store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


class Product:
"""Product model implementation
"""
Product model implementation

Instances of this class represent a product available for purchase.

Expand All @@ -25,7 +26,8 @@ def __init__(self,
price: int,
unit: Union[int, float]
) -> None:
"""Initialize instance
"""
Initialize instance

:param name: product name
:type name: str
Expand Down Expand Up @@ -158,16 +160,16 @@ def __iter__(self) -> Iterator[Tuple[Product, Union[int, float]]]:
def remove_product(self,
product: Product
) -> Tuple[Product, Union[int, float]]:
"""Remove product from a cart instance

"""
Remove product from a cart instance

:param product: a product instance to add to cart
:type product: :class: `Product`

:return: a shopping cart product/quantity entry
:rtype: tuple

:raise: ValueError
:raise ValueError: if the project isn't present in the shopping cart

"""

Expand Down Expand Up @@ -216,16 +218,16 @@ def sub_product(self,
product: Product,
quantity: Union[int, float]
) -> None:
"""Subtract product from the shopping cart
"""
Subtract product from the shopping cart

If quantity value is less or equal to 0 the product is to be
removed from the shopping cart


:param product: a product instance to add to cart
:type product: :class: `Product`
:param quantity: a quantity of a product to add
:type quantity: int or float
:type quantity: int | float

"""

Expand Down
Loading