parallel lines
There are different ways to express the equation of a line. Here's one way:
2x + 4y = 1
x + 2y = 1
The x and the y are always the same, but we can sub out variables for the three numbers:
ax + by = c
Now, we can just make a tuple with a, b, and c:
[a b c]
The first two lines represented in this form:
[2 4 1]
[1 2 1]
Okay! Now that we have a way to represent lines, your task is to write a function that takes two lines and tells us if they're parallel. Here's an example:
(parallel? [2 4 1] [1 2 1]) ;=> true
(parallel? [2 4 1] [4 2 1]) ;=> false
(parallel? [0 1 5] [0 1 5]) ;=> false ;; lines are not parallel to themselves
I'm looking forward to seeing the variety of answers.
Thanks to this site for the challenge idea where it is considered Medium level in Python.
Email submissions to eric@purelyfunctional.tv before July 5, 2020. You can discuss the submissions in the comments below.
Wow...Thanks @ericnormand for your properties test - I will learn from them :)
Most solutions fail against some of the properties tests, mostly due to divide-by-zero errors I think. The only solutions which passed all properties tests (Eric's and mine), are @ninjure's and @ericnormand - congrats!
My own solutions also failed against Eric's horizontal/vertical checks, which happen to inject 0.0 to generate such a line: the innocuous looking statement
(= 0 a b)
fails when passed a float. The lesson here is to constantly be aware of potential mixed numeric types, or even better, to use logic which behaves uniformly across types. Also, I suspect most solutions (mine certainly does) fail against some mixed type inputs, e.g.so perhaps the input could be constrained to integers only.
I think it would be an interesting topic of discussion or blog post on the best way to cope uniformly with mixed types and under which conditions etc.
I patched this solution below to use
(== 0 ..)
to make it pass all tests: