The problem is from codeforces: http://www.codeforces.com/problemset/problem/227/A
The picture vividly illustrates the problem statement. Given 3 Points A, B and C, you are required to find out the direction. Read from keyboard the three coordinates as three complex numbers and compute the complex number . The method conjugate() applies to the complex number. It computes the complex number that has the identical length.
>>> a=3+4j >>> a.conjugate() (3-4j) >>> a.conjugate().conjugate() (3+4j) >>> a.conjugate().imag -4.0 >>> a.conjugate().real 3.0
The two-line Python solution is as follows.
a, b, c = [eval(raw_input().replace(' ', '+') + 'j') for i in xrange(3)] print ['TOWARDS', 'RIGHT', 'LEFT'][cmp(((b - a) * (c - b).conjugate()).imag, 0)]
The first line returns a, b and c as complex numbers e.g. 1+2j. The second line will print the corresponding direction, based on the cmp value which has three possible outcomes, -1, 0 and 1.
The pure math solution, not using complex numbers is given below.
#!/usr/bin/env python def read(): return map(int, raw_input().split(' ')) x1, y1 = read() x2, y2 = read() x3, y3 = read() k = (x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1) print ['TOWARDS','RIGHT','LEFT'][cmp(k, 0)]
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Codeforces: A. Is your horseshoe on the other hoof?
Next Post: Codeforces: B. T-primes