In this class 2 find the torsional angle problem You are given four points A, B, C, and D in a 3-dimensional Cartesian coordinate system. You are required to print the angle between the plane made by the points A, B, C and B, C, D in degrees. 

HackerRank Class 2 Find the Torsional Angle solution in python


Problem solution in Python 2 programming.

import math

class Point:
  """docstring for Point"""
  def __init__(self, x, y, z):
    # super(Point, self).__init__()
    self.x = x
    self.y = y
    self.z = z

  def minus(self, other):
    return Point(self.x - other.x, self.y - other.y, self.z - other.z)

  def cross(self, other):
    return Point( self.y * other.z - self.z * other.y
                 , self.z * other.x - self.x * other.z
                 , self.x * other.y - self.y * other.x)

  def dot(self, other):
    return self.x * other.x + self.y * other.y + self.z * other.z

  def abs(self):
    return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)

  def str(self):
    return "(" + str(self.x) + ", " + str(self.y) + ", " + str(self.z) + ")"

def get_Point():
  lst = map(float, raw_input().strip().split())
  return Point(lst[0], lst[1], lst[2])

a = get_Point()
b = get_Point()
c = get_Point()
d = get_Point()

ab = a.minus(b)
bc = b.minus(c)
cd = c.minus(d)

x = ab.cross(bc)
y = bc.cross(cd)

v = x.dot(y) / (x.abs() * y.abs())


print "%.2f" % (math.acos(v) / math.acos(0) * 90,)


Problem solution in Python 3 programming.

class Points(object):
    def __init__(self, x, y, z):
        self.x=x
        self.y=y
        self.z=z

    def __sub__(self, no):
        return  Points((self.x-no.x),(self.y-no.y),(self.z-no.z))

    def dot(self, no):
        return (self.x*no.x)+(self.y*no.y)+(self.z*no.z)

    def cross(self, no):
        return Points((self.y*no.z-self.z*no.y),(self.z*no.x-self.x*no.z),(self.x*no.y-self.y*no.x))
        
    def absolute(self):
        return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)




Problem solution in pypy programming.

class Points(object):
    def __init__(self, x, y, z):
        self.x=x
        self.y=y
        self.z=z
       
    def __sub__(self, other):
        return Points(self.x-other.x, self.y-other.y, self.z-other.z)   
         
    def dot(self, other):
        return (self.x*other.x)+ (self.y*other.y )+ (self.z*other.z)
    def cross(self, other):
        return Points( self.y*other.z - self.z*other.y,
                       self.z*other.x - self.x*other.z,
                       self.x*other.y - self.y*other.x
                     )
    def absolute(self):
        return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)


Problem solution in pypy3 programming.

from math import acos, sqrt, degrees

class Vector:
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z
    
    def __sub__(self, other):
        return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
    
    def __mul__(self, other):
        return self.x * other.x + self.y * other.y + self.z * other.z
        
    def __and__(self, other):
        return Vector(self.y * other.z - other.y * self.z,
                      other.x * self.z - self.x * other.z,
                      self.x * other.y - other.x * self.y)
    
    def __abs__(self):
        return sqrt(self * self)
    
def read_vector():
    return Vector(*[float(x) for x in input().split()])

A = read_vector()
B = read_vector()
C = read_vector()
D = read_vector()

X = (B - A) & (C - B)
Y = (C - B) & (D - C)
phi = degrees(acos((X * Y)/(abs(X) * abs(Y))))
print('{:0.2f}'.format(phi))