forked from Krekep/spbu-python-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Тетюхин. Task 1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaximTetuchin
wants to merge
6
commits into
main
Choose a base branch
from
task1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2acccb9
added 1 task
MaximTetuchin 05b9120
fixed code with pre-commit
MaximTetuchin 1b1e2be
fixed tests dir
MaximTetuchin c79444f
changed reqs.txt
MaximTetuchin f185e09
fixed tests
MaximTetuchin 0ac672e
fixed tests
MaximTetuchin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Types check with myPy | ||
|
|
||
| on: [push, pull_request] | ||
|
|
||
| jobs: | ||
| mypy: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| python-version: [3.9] | ||
|
|
||
| steps: | ||
| - name: Set up Git repository | ||
| uses: actions/checkout@v3 | ||
|
|
||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip wheel setuptools | ||
| python -m pip install -r requirements.txt | ||
| python -m pip install mypy | ||
|
|
||
| - name: Run MyPy | ||
| run: | | ||
| mypy project/ |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: Run Tests | ||
| on: [push, pull_request] | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.10' | ||
| - name: Install pytest | ||
| run: pip install pytest | ||
| - name: Run tests using project script | ||
| run: | | ||
| python ./scripts/run_tests.py |
Empty file.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| def matrix_add(matrix1, matrix2): | ||
| """ | ||
| Add two matrices element-wise. | ||
|
|
||
| Args: | ||
| matrix1 (list): First matrix as list of lists. | ||
| matrix2 (list): Second matrix as list of lists. | ||
|
|
||
| Returns: | ||
| list: Resulting matrix after addition. | ||
|
|
||
| Raises: | ||
| ValueError: If matrices have different dimensions. | ||
| """ | ||
| if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]): | ||
| raise ValueError("Matrices must have the same dimensions") | ||
|
|
||
| return [ | ||
| [matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] | ||
| for i in range(len(matrix1)) | ||
| ] | ||
|
|
||
|
|
||
| def matrix_multiply(matrix1, matrix2): | ||
| """ | ||
| Multiply two matrices. | ||
|
|
||
| Args: | ||
| matrix1 (list): First matrix. | ||
| matrix2 (list): Second matrix. | ||
|
|
||
| Returns: | ||
| list: Resulting matrix after multiplication. | ||
|
|
||
| Raises: | ||
| ValueError: If number of columns in first matrix doesn't match | ||
| number of rows in second matrix. | ||
| """ | ||
| if len(matrix1[0]) != len(matrix2): | ||
| raise ValueError( | ||
| "Number of columns in first matrix must equal " | ||
| "number of rows in second matrix" | ||
| ) | ||
|
|
||
| result = [[0] * len(matrix2[0]) for _ in range(len(matrix1))] | ||
| for i in range(len(matrix1)): | ||
| for k in range(len(matrix2)): | ||
| for j in range(len(matrix2[0])): | ||
| result[i][j] += matrix1[i][k] * matrix2[k][j] | ||
| return result | ||
|
|
||
|
|
||
| def matrix_transpose(matrix): | ||
| """ | ||
| Transpose a matrix (swap rows and columns). | ||
|
|
||
| Args: | ||
| matrix (list): Input matrix. | ||
|
|
||
| Returns: | ||
| list: Transposed matrix. | ||
| """ | ||
| return [list(row) for row in zip(*matrix)] | ||
|
|
||
|
|
||
| class Matrix: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем Вам реализация через класс и через функции? |
||
| """ | ||
| A class representing a mathematical matrix. | ||
|
|
||
| Attributes: | ||
| data (list): Matrix data as list of lists. | ||
| """ | ||
|
|
||
| def __init__(self, data): | ||
| """ | ||
| Initialize a matrix with given data. | ||
|
|
||
| Args: | ||
| data (list): Matrix data as list of lists. | ||
|
|
||
| Raises: | ||
| ValueError: If rows have inconsistent lengths. | ||
| """ | ||
| if not all(len(row) == len(data[0]) for row in data): | ||
| raise ValueError("All rows must have the same length") | ||
| self.data = data | ||
| self.rows = len(data) | ||
| self.cols = len(data[0]) | ||
|
|
||
| def add(self, other): | ||
| """ | ||
| Add another matrix to this matrix. | ||
|
|
||
| Args: | ||
| other (Matrix): Another matrix. | ||
|
|
||
| Returns: | ||
| Matrix: Result of addition. | ||
| """ | ||
| return Matrix(matrix_add(self.data, other.data)) | ||
|
|
||
| def multiply(self, other): | ||
| """ | ||
| Multiply with another matrix. | ||
|
|
||
| Args: | ||
| other (Matrix): Another matrix. | ||
|
|
||
| Returns: | ||
| Matrix: Result of multiplication. | ||
| """ | ||
| return Matrix(matrix_multiply(self.data, other.data)) | ||
|
|
||
| def transpose(self): | ||
| """ | ||
| Transpose the matrix. | ||
|
|
||
| Returns: | ||
| Matrix: Transposed matrix. | ||
| """ | ||
| return Matrix(matrix_transpose(self.data)) | ||
|
|
||
| def __repr__(self): | ||
| return f"Matrix({self.data})" | ||
|
|
||
| def __eq__(self, other): | ||
| if not isinstance(other, Matrix): | ||
| return False | ||
| return self.data == other.data | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import math | ||
|
|
||
|
|
||
| def dot_product(vector1, vector2): | ||
| """ | ||
| Calculate the dot product of two vectors. | ||
|
|
||
| Args: | ||
| vector1 (list): First vector as a list of numbers. | ||
| vector2 (list): Second vector as a list of numbers. | ||
|
|
||
| Returns: | ||
| float: Dot product of the two vectors. | ||
|
|
||
| Raises: | ||
| ValueError: If vectors have different lengths. | ||
| """ | ||
| if len(vector1) != len(vector2): | ||
| raise ValueError("Vectors must have the same length") | ||
| return sum(v1 * v2 for v1, v2 in zip(vector1, vector2)) | ||
|
|
||
|
|
||
| def vector_length(vector): | ||
| """ | ||
| Calculate the Euclidean length (magnitude) of a vector. | ||
|
|
||
| Args: | ||
| vector (list): Vector as a list of numbers. | ||
|
|
||
| Returns: | ||
| float: Length of the vector. | ||
| """ | ||
| return math.sqrt(sum(x * x for x in vector)) | ||
|
|
||
|
|
||
| def angle_between(vector1, vector2): | ||
| """ | ||
| Calculate the angle between two vectors in radians. | ||
|
|
||
| Args: | ||
| vector1 (list): First vector. | ||
| vector2 (list): Second vector. | ||
|
|
||
| Returns: | ||
| float: Angle between vectors in radians. | ||
|
|
||
| Raises: | ||
| ValueError: If either vector is a zero vector. | ||
| """ | ||
| dot = dot_product(vector1, vector2) | ||
| len1 = vector_length(vector1) | ||
| len2 = vector_length(vector2) | ||
|
|
||
| if len1 == 0 or len2 == 0: | ||
| raise ValueError("Vectors cannot be zero vectors") | ||
|
|
||
| # Ensure cosine value is within valid range to avoid floating point errors | ||
| cos_angle = dot / (len1 * len2) | ||
| cos_angle = max(min(cos_angle, 1.0), -1.0) | ||
|
|
||
| return math.acos(cos_angle) | ||
|
|
||
|
|
||
| class Vector: | ||
| """ | ||
| A class representing a mathematical vector. | ||
|
|
||
| Attributes: | ||
| components (list): The components of the vector. | ||
| """ | ||
|
|
||
| def __init__(self, components): | ||
| """ | ||
| Initialize a vector with given components. | ||
|
|
||
| Args: | ||
| components (list): List of numerical components. | ||
| """ | ||
| self.components = list(components) | ||
|
|
||
| def dot(self, other): | ||
| """ | ||
| Calculate dot product with another vector. | ||
|
|
||
| Args: | ||
| other (Vector): Another vector. | ||
|
|
||
| Returns: | ||
| float: Dot product. | ||
| """ | ||
| return dot_product(self.components, other.components) | ||
|
|
||
| def length(self): | ||
| """ | ||
| Calculate the length of the vector. | ||
|
|
||
| Returns: | ||
| float: Vector length. | ||
| """ | ||
| return vector_length(self.components) | ||
|
|
||
| def angle_with(self, other): | ||
| """ | ||
| Calculate angle with another vector. | ||
|
|
||
| Args: | ||
| other (Vector): Another vector. | ||
|
|
||
| Returns: | ||
| float: Angle in radians. | ||
| """ | ||
| return angle_between(self.components, other.components) | ||
|
|
||
| def __repr__(self): | ||
| return f"Vector({self.components})" | ||
|
|
||
| def __eq__(self, other): | ||
| if not isinstance(other, Vector): | ||
| return False | ||
| return self.components == other.components |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| black | ||
| mypy | ||
| pre-commit | ||
| pytest |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Отсутствуют typehints