Skip to content

Commit f3df0b7

Browse files
committed
Classes 1-2
1 parent b607fe2 commit f3df0b7

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

Classes/Class 2 Find the Torsional Angle.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Class 2 - Find the Torsional Angle
2+
# https://www.hackerrank.com/challenges/class-2-find-the-torsional-angle/problem
3+
4+
import math
5+
6+
class vector:
7+
def __init__(self, x=0, y=0, z=0):
8+
self.x = x
9+
self.y = y
10+
self.z = z
11+
12+
def __mul__(self, k):
13+
return vector(self.x * k, self.y * k, self.z * k)
14+
15+
def __add__(self, other):
16+
s, o = self, other
17+
return vector(s.x + o.x, s.y + o.y, s.z + o.z)
18+
19+
def __sub__(self, other):
20+
return self + other * -1
21+
22+
# dot product
23+
def __mod__(self, other):
24+
s, o = self, other
25+
return (s.x * o.x) + (s.y * o.y) + (s.z * o.z)
26+
27+
# modulus
28+
def __abs__(self):
29+
return math.sqrt(self % self)
30+
31+
# cross product
32+
def __xor__(self, other):
33+
s, o = self, other
34+
x = (s.y * o.z) - (s.z * o.y)
35+
y = (s.z * o.x) - (s.x * o.z)
36+
z = (s.x * o.y) - (s.y * o.x)
37+
return vector(x, y, z)
38+
39+
def read_vector():
40+
return vector(*map(float, input().split()))
41+
42+
A = read_vector()
43+
B = read_vector()
44+
C = read_vector()
45+
D = read_vector()
46+
47+
X = (B - A) ^ (C - B)
48+
Y = (C - B) ^ (D - C)
49+
50+
phi = math.degrees(math.acos(X % Y / math.sqrt((X % X) * (Y % Y))))
51+
print('%.2f' % phi)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Classes: Dealing with Complex Numbers
2+
# https://www.hackerrank.com/challenges/class-1-dealing-with-complex-numbers/problem
3+
4+
from math import sqrt
5+
6+
class ComplexNumber(object):
7+
def __init__(self, x=0, y=0):
8+
self.real_p = x
9+
self.imag_p = y
10+
11+
def __add__(self, other):
12+
s = ComplexNumber()
13+
s.real_p = self.real_p + other.real_p
14+
s.imag_p = self.imag_p + other.imag_p
15+
return s
16+
17+
def __sub__(self, other):
18+
s = ComplexNumber()
19+
s.real_p = self.real_p - other.real_p
20+
s.imag_p = self.imag_p - other.imag_p
21+
return s
22+
23+
def __mul__(self, other):
24+
s = ComplexNumber()
25+
s.real_p = self.real_p*other.real_p - self.imag_p*other.imag_p
26+
s.imag_p = self.real_p*other.imag_p + self.imag_p*other.real_p
27+
return s
28+
29+
def __truediv__(self, other):
30+
s = ComplexNumber()
31+
s.real_p = (self.real_p*other.real_p + self.imag_p*other.imag_p) / (other.real_p**2 + other.imag_p**2)
32+
s.imag_p = (self.imag_p*other.real_p - self.real_p*other.imag_p) / (other.real_p**2 + other.imag_p**2)
33+
return s
34+
35+
def __abs__(self):
36+
s = ComplexNumber()
37+
s.real_p = sqrt(self.real_p**2 + self.imag_p**2)
38+
return s
39+
40+
def __str__(self):
41+
if self.imag_p >= 0:
42+
s = "{0:.2f}+{1:.2f}i".format(self.real_p, self.imag_p)
43+
else:
44+
s = "{0:.2f}{1:.2f}i".format(self.real_p, self.imag_p)
45+
return s
46+
47+
x1, y1 = map(float, input().split())
48+
x2, y2 = map(float, input().split())
49+
z1 = ComplexNumber(x1, y1)
50+
z2 = ComplexNumber(x2, y2)
51+
print(z1+z2)
52+
print(z1-z2)
53+
print(z1*z2)
54+
print(z1/z2)
55+
print(abs(z1))
56+
print(abs(z2))

0 commit comments

Comments
 (0)