Skip to content
Closed

Dz1 #32

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
34 changes: 9 additions & 25 deletions .github/workflows/code_style.yml → .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is a basic workflow to help you get started with Actions

name: Check code style
name: Check lint

# Controls when the workflow will run
on:
Expand All @@ -27,31 +27,15 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Set up Git repository
uses: actions/checkout@v2

# Setup Python with version from matrix
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

# Install requirements
- name: Install requirements

# Runs command-line programs using the operating system's shell
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
python -m pip install -r requirements.txt
python -m pip list

# Install pre-commit from .pre-commit-config.yaml
- name: Install pre-commit
run: |
pre-commit install

# Run pre-commit on all the files in the repo
- name: Run pre-commit
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Pre commit
run: |
pre-commit run --all-files --color always --verbose --show-diff-on-failure
41 changes: 41 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This is a basic workflow to help you get started with Actions

name: Check mypy

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events
[ push, pull_request ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

# This workflow contains a single job called "style"
style:

# The type of runner that the job will run on
runs-on: ubuntu-latest

# A strategy creates a build matrix for your jobs
strategy:

# You can define a matrix of different job configurations
matrix:

# Each option you define in the matrix has a key and value
python-version: [ 3.8 ]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run mypy
run: |
mypy project/ tests/
41 changes: 41 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This is a basic workflow to help you get started with Actions

name: Check pytest

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events
[ push, pull_request ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:

# This workflow contains a single job called "style"
style:

# The type of runner that the job will run on
runs-on: ubuntu-latest

# A strategy creates a build matrix for your jobs
strategy:

# You can define a matrix of different job configurations
matrix:

# Each option you define in the matrix has a key and value
python-version: [ 3.8 ]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run pytest
run: |
python ./scripts/run_tests.py
1 change: 0 additions & 1 deletion project/__main__.py

This file was deleted.

89 changes: 89 additions & 0 deletions project/operation_on_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Operations on matrices


def matrix_addition(matrix_1: list[list], matrix_2: list[list]):
"""
Adds two matrices together.

Args:
-----
matrix_1 (list of lists): The first matrix.
matrix_2 (list of lists): The second matrix.

Returns:
--------
list of lists: The sum of the two matrices, or None if the matrices have
different dimensions.
Raises:
TypeError: If either input is not a list of lists.
"""
if len(matrix_1) != len(matrix_2) or len(matrix_1[0]) != len(matrix_2[0]):
return None
else:
matrix_sum = [[0] * len(matrix_1[0]) for _ in range(len(matrix_1))]
for i in range(len(matrix_1)):
for j in range(len(matrix_1[0])):
matrix_sum[i][j] = matrix_1[i][j] + matrix_2[i][j]
return matrix_sum


def matrix_multiplication(matrix_1: list[list], matrix_2: list[list]):
"""
The matrix_multiplication function multiplies two matrices represented as nested lists.

Parameters:
-----------
matrix_1 (list of lists): The first matrix.
matrix_2 (list of lists): The second matrix.

Return value:
-------------
list of lists: The product of two matrices if the number of columns in the first matrix is equal to the number of rows in the second matrix.
None: If the number of columns in the first matrix is not equal to the number of rows in the second matrix.

Exceptions:
-----------
TypeError: Occurs if one or both of the input arguments are not lists or nested lists.

"""
if len(matrix_1[0]) != len(matrix_2):
return None
else:
matrix_mult = [[0] * len(matrix_2[0]) for _ in range(len(matrix_1))]
for i in range(len(matrix_1)):
for j in range(len(matrix_2[0])):
for r in range(len(matrix_2)):
matrix_mult[i][j] += int(matrix_1[i][r]) * int(matrix_2[r][j])
return matrix_mult


def matrix_transposition(matrix: list[list]):
"""
The matrix_transposition function transposes a matrix represented as a nested list.
Transposing a matrix means exchanging rows and columns.
The function passes through each element of the original matrix matrix using nested for loops.
For each matrix[i][j] element (row i, column j), the function assigns the value of the matrix_trans[j][i] element
(row j, column i). Thus, the rows and columns of the original matrix are swapped.

Parameters:
-----------
matrix (list of lists): The matrix to be transposed.

Return value:
-------------
list of lists: A transposed matrix represented as a nested list.

Exceptions:
-----------
TypeError: Occurs if the input argument is not a list or a nested list.

"""

if not matrix:
return []
else:
matrix_trans = [[0] * len(matrix) for _ in range(len(matrix[0]))]
for i in range(len(matrix)):
for j in range(len(matrix[0])):
matrix_trans[j][i] = matrix[i][j]
return matrix_trans
69 changes: 69 additions & 0 deletions project/operation_on_vectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Operation on vectors

from math import pi, acos


def the_scalar_product_of_vectors(vector_1: list[int], vector_2: list[int]):
"""
Function the_scalar_product_of_vectors checks whether the vectors have the same length. If not, it returns None.
Otherwise, the function calculates the scalar pro1duct by multiplying the corresponding elements of the vectors
and summing the results.

Parameters:
-----------
vector_1 (list): The first vector, represented as a list of numbers.
vector_2 (list): The second vector, represented as a list of numbers.

Return value:
-------------
int: The scalar product of vectors vector_1 and vector_2, if they have the same length.
None: If the vectors have different lengths.

"""
if len(vector_1) != len(vector_2):
return None
else:
p = 0
for i in range(len(vector_1)):
p += vector_1[i] * vector_2[i]
return p


def vector_length(vector: list[int]):
"""
The vector_length function calculates the length of a vector represented as a list of numbers.

Parameters:
-----------
vector (list): A vector represented as a list of numbers.

Return value:
-------------
int: The length of the vector, rounded to an integer

"""
s = 0
for i in vector:
s += i**2
return round(pow(s, 0.5), 2)


def the_angle_between_the_vectors(vector_1: list[int], vector_2: list[int]):
"""
The_angle_between_the_vectors function calculates the angle between two vectors
given as lists of numbers using the scalar product and the lengths of the vectors.

Parameters:
-----------
vector_1 (list): The first vector, represented as a list of numbers.
vector_2 (list): The second vector, represented as a list of numbers.

Return value:
-------------
float: The angle between the vectors in radians, with an accuracy of two decimal places.

"""
s = the_scalar_product_of_vectors(vector_1, vector_2)
angle = s / (vector_length(vector_1) * vector_length(vector_2))
angle = acos(angle) * 180 / pi
return round(angle)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
black
mypy
pre-commit
pytest
3 changes: 2 additions & 1 deletion tasks/task1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Задача 1. Инициализация рабочего окружения

* **Дедлайн**: 27.09.2024, 23:59
* **Дедлайн**: 03.10.2024, 23:59
* Полный балл: 5

## Задача
Expand All @@ -13,3 +13,4 @@
- [ ] Операции над матрицами: сложение, умножение, транспонирование.
- [ ] Добавить тесты покрывающие реализованную функциональность.
- [ ] Добавить запуск тестов с помощью `pytest` в `.github/workflows/`.
- [ ] Добавить статическую проверку типов с помощью `mypy` в `.github/workflows/`.
18 changes: 0 additions & 18 deletions tests/test_basic.py

This file was deleted.

Loading