Skip to content

Commit

Permalink
add self with casting
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarend authored Apr 1, 2024
1 parent 673e909 commit bb5810c
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions problems-3/n-valikov/problem3.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import unittest
from numbers import Number
from typing import Tuple, Any, TypeAlias
from typing import Tuple, Any, Self


class Vector:
Vector: TypeAlias = Vector

_content: Tuple
_iter_counter: int

Expand All @@ -16,20 +14,20 @@ def __init__(self, content: Tuple):
self._content = content
self._iter_counter = 0

def __add__(self, other: Vector) -> Vector:
def __add__(self, other: Self) -> Self:
if len(self) != len(other):
raise ValueError('Vectors dimensions must be equal')

return Vector(tuple(first + second for first, second in zip(self._content, other._content)))
return type(self)(tuple(first + second for first, second in zip(self._content, other._content)))

def __sub__(self, other: Vector) -> Vector:
def __sub__(self, other: Self) -> Self:
return self + (-other)

def __neg__(self) -> Vector:
return Vector(tuple(-value for value in self._content))
def __neg__(self) -> Self:
return type(self)(tuple(-value for value in self._content))

def __mul__(self, constant: int) -> Vector:
return Vector(tuple(content * constant for content in self._content))
def __mul__(self, constant: int) -> Self:
return type(self)(tuple(content * constant for content in self._content))

def __next__(self):
length = len(self)
Expand Down Expand Up @@ -57,11 +55,11 @@ def __str__(self):
return (f"Number of dimensions: {len(self._content)}\n"
f"{'\n'.join(map(lambda value: str(value), self._content))}\n")

def scalar_product(self, other: Vector) -> Vector:
def scalar_product(self, other: Self) -> Self:
if len(self) != len(other):
raise ValueError('Vectors dimensions must be equal')

return Vector(
return type(self)(
tuple(this_content * other_content for this_content, other_content in zip(self._content, other._content)))


Expand Down

0 comments on commit bb5810c

Please sign in to comment.