Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
69
Task/Vector/Python/vector-1.py
Normal file
69
Task/Vector/Python/vector-1.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
class Vector:
|
||||
def __init__(self,m,value):
|
||||
self.m = m
|
||||
self.value = value
|
||||
self.angle = math.degrees(math.atan(self.m))
|
||||
self.x = self.value * math.sin(math.radians(self.angle))
|
||||
self.y = self.value * math.cos(math.radians(self.angle))
|
||||
|
||||
def __add__(self,vector):
|
||||
"""
|
||||
>>> Vector(1,10) + Vector(1,2)
|
||||
Vector:
|
||||
- Angular coefficient: 1.0
|
||||
- Angle: 45.0 degrees
|
||||
- Value: 12.0
|
||||
- X component: 8.49
|
||||
- Y component: 8.49
|
||||
"""
|
||||
final_x = self.x + vector.x
|
||||
final_y = self.y + vector.y
|
||||
final_value = pytagoras(final_x,final_y)
|
||||
final_m = final_y / final_x
|
||||
return Vector(final_m,final_value)
|
||||
|
||||
def __neg__(self):
|
||||
return Vector(self.m,-self.value)
|
||||
|
||||
def __sub__(self,vector):
|
||||
return self + (- vector)
|
||||
|
||||
def __mul__(self,scalar):
|
||||
"""
|
||||
>>> Vector(4,5) * 2
|
||||
Vector:
|
||||
- Angular coefficient: 4
|
||||
- Angle: 75.96 degrees
|
||||
- Value: 10
|
||||
- X component: 9.7
|
||||
- Y component: 2.43
|
||||
|
||||
"""
|
||||
return Vector(self.m,self.value*scalar)
|
||||
|
||||
def __div__(self,scalar):
|
||||
return self * (1 / scalar)
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
Returns a nicely formatted list of the properties of the Vector.
|
||||
|
||||
>>> Vector(1,10)
|
||||
Vector:
|
||||
- Angular coefficient: 1
|
||||
- Angle: 45.0 degrees
|
||||
- Value: 10
|
||||
- X component: 7.07
|
||||
- Y component: 7.07
|
||||
|
||||
"""
|
||||
return """Vector:
|
||||
- Angular coefficient: {}
|
||||
- Angle: {} degrees
|
||||
- Value: {}
|
||||
- X component: {}
|
||||
- Y component: {}""".format(self.m.__round__(2),
|
||||
self.angle.__round__(2),
|
||||
self.value.__round__(2),
|
||||
self.x.__round__(2),
|
||||
self.y.__round__(2))
|
||||
76
Task/Vector/Python/vector-2.py
Normal file
76
Task/Vector/Python/vector-2.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
from __future__ import annotations
|
||||
import math
|
||||
from functools import lru_cache
|
||||
from typing import NamedTuple
|
||||
|
||||
CACHE_SIZE = None
|
||||
|
||||
|
||||
def hypotenuse(leg: float,
|
||||
other_leg: float) -> float:
|
||||
"""Returns hypotenuse for given legs"""
|
||||
return math.sqrt(leg ** 2 + other_leg ** 2)
|
||||
|
||||
|
||||
class Vector(NamedTuple):
|
||||
slope: float
|
||||
length: float
|
||||
|
||||
@property
|
||||
@lru_cache(CACHE_SIZE)
|
||||
def angle(self) -> float:
|
||||
return math.atan(self.slope)
|
||||
|
||||
@property
|
||||
@lru_cache(CACHE_SIZE)
|
||||
def x(self) -> float:
|
||||
return self.length * math.sin(self.angle)
|
||||
|
||||
@property
|
||||
@lru_cache(CACHE_SIZE)
|
||||
def y(self) -> float:
|
||||
return self.length * math.cos(self.angle)
|
||||
|
||||
def __add__(self, other: Vector) -> Vector:
|
||||
"""Returns self + other"""
|
||||
new_x = self.x + other.x
|
||||
new_y = self.y + other.y
|
||||
new_length = hypotenuse(new_x, new_y)
|
||||
new_slope = new_y / new_x
|
||||
return Vector(new_slope, new_length)
|
||||
|
||||
def __neg__(self) -> Vector:
|
||||
"""Returns -self"""
|
||||
return Vector(self.slope, -self.length)
|
||||
|
||||
def __sub__(self, other: Vector) -> Vector:
|
||||
"""Returns self - other"""
|
||||
return self + (-other)
|
||||
|
||||
def __mul__(self, scalar: float) -> Vector:
|
||||
"""Returns self * scalar"""
|
||||
return Vector(self.slope, self.length * scalar)
|
||||
|
||||
def __truediv__(self, scalar: float) -> Vector:
|
||||
"""Returns self / scalar"""
|
||||
return self * (1 / scalar)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
v1 = Vector(1, 1)
|
||||
|
||||
print("Pretty print:")
|
||||
print(v1, end='\n' * 2)
|
||||
|
||||
print("Addition:")
|
||||
v2 = v1 + v1
|
||||
print(v1 + v1, end='\n' * 2)
|
||||
|
||||
print("Subtraction:")
|
||||
print(v2 - v1, end='\n' * 2)
|
||||
|
||||
print("Multiplication:")
|
||||
print(v1 * 2, end='\n' * 2)
|
||||
|
||||
print("Division:")
|
||||
print(v2 / 2)
|
||||
Loading…
Add table
Add a link
Reference in a new issue