8
\$\begingroup\$

Given equation of two lines in the format Ax+By=C, determine their intersection point. If they are parallel print 'parallel' without quotes. For input/output format refer sample input/output.

Input
1x+1y=1
1x-1y=1

Output
(1.00,0.00)

Input
0x+1y=0
1x+0y=0

Output
(0.00,0.00)
  1. -1000000 <= A,B,C <= 1000000.
  2. Note their intersection point may not be integer. Print upto two decimal places.
  3. Shortest solution wins.
\$\endgroup\$
6
  • \$\begingroup\$ Is a function call valid? example : f[1x+1y=1, 1x-1y=1] \$\endgroup\$ Apr 17, 2011 at 20:58
  • \$\begingroup\$ @belisarius Yes, but the solution should be entire program. \$\endgroup\$
    – fR0DDY
    Apr 18, 2011 at 5:59
  • \$\begingroup\$ Is the format always Ax+By=C or can it also be Ax-By=C or even -Ax+By=C? Are negative numbers for A, B or C even allowed? »Print up to two decimal places« implies that less can be printed, your examples do not suggest that. If the output is locale-dependent (which can happen with plenty languages in formatted output), can we assume en-US or C as locale? (I.e. anything that uses a decimal point and not a comma). \$\endgroup\$
    – Joey
    Apr 18, 2011 at 10:43
  • \$\begingroup\$ @Joey Yes A,B and C can be negative. Ax-By=C is already given in one of the examples. The points should have 2 decimal places. \$\endgroup\$
    – fR0DDY
    Apr 18, 2011 at 11:45
  • \$\begingroup\$ @Joey Edited the question to show that. Thanks for pointing that out. \$\endgroup\$
    – fR0DDY
    Apr 18, 2011 at 13:49

8 Answers 8

3
\$\begingroup\$

Ruby - 119 chars

a,b,c,d,e,f=(gets+gets).scan(/-?\d+/).map &:to_f
puts (t=a*e-b*d)==0?'parallel':"(%.2f,%.2f)"%[(e*c-b*f)/t,(a*f-c*d)/t]
\$\endgroup\$
2
  • 1
    \$\begingroup\$ It's odd but my ruby 1.9.x shows -0.00, :-\ and you can use gets(p) instead of 2 gets, it will read the entire contents (assuming it was piped) \$\endgroup\$
    – st0le
    Apr 16, 2011 at 15:55
  • 1
    \$\begingroup\$ gets(2) would get two bytes of input, as opposed to two lines on input. \$\endgroup\$
    – britishtea
    Sep 29, 2014 at 12:23
3
\$\begingroup\$

Python, 148 146 chars

import re
I=raw_input
a,b,p,c,d,q=eval(re.sub('x|y=','.,',I()+','+I()))
D=a*d-b*c
print'(%.2f,%.2f)'%((p*d-q*b)/D,(a*p-c*q)/D)if D else'parallel'
\$\endgroup\$
2
\$\begingroup\$

sage - 162 chars

x,y=var('x,y')
e=lambda:eval(raw_input().replace('x','*x').replace('y','*y='))
try:r=solve([e(),e()],[x,y])[0];print`r[0].rhs(),r[1].rhs()`
except:print'parallel'
\$\endgroup\$
2
\$\begingroup\$

Python+sympy - 161 chars

from sympy import*
x,y=symbols('xy')
e=lambda:eval(raw_input().replace('x','*x').replace('y=','*y-'))
r=solve([e(),e()],[x,y])
print r and`r[x],r[y]`or'parallel'

Python+sympy - 143 chars (different output format)

from sympy import*
x,y=symbols('xy')
e=lambda:eval(raw_input().replace('x','*x').replace('y=','*y-'))
print solve([e(),e()],[x,y])or'parallel'

output format for 143 char version is slightly different from the spec

In:
1x+1y=1
1x-1y=1

Out:
{x: 1, y: 0}

In:
0x+1y=0
1x+0y=0

Out:
{x: 0, y: 0}
\$\endgroup\$
2
\$\begingroup\$

J, 146 132 134 124

echo'parallel'"_`(1|.')(',[:(,',',])&(0j2&":)/[:,,.@{:%.}:)@.([:*@|[:-/ .*}:)|:".>{.`(<@}:@;@(1 2&{))`{:(`:0)"1;:;._2(1!:1)3

Parsing this is awful.

Edit: Realized that my output is basically broken, although it works for the examples given and parallels...

Edit: Posting the shorter version I already had.

Edit: Fixed without too much pain...

Edit: Fixed a weird issue where the program was spread across multiple lines.

Somewhat ungolfed version:

words =: ;:;._2(1!:1)3
NB. Leverage J's parser itself to split each line into e.g. ax + by = c
lines =: ".>{.`(<@}:@;@(1 2&{))`{:(`:0)"1 words
NB. Use a 3-part gerund (`) to get boxed strings 'ax';'b';'c', unbox and parse
calc_and_format =: 1|.')(',[:(,',',])&(0j2&":)/[:,,.@{:%.}:
NB. %. (matrix multiply) first two rows (a d/b e) by third row turned (c/f)
NB. And then format laboriously. 0j2&": formats numbers with 2 decimals.
det_nonzero =: [:*@|[:-/ .*}:
NB. 1 if determinant (-/ .*) of (a b/d e) is nonzero (*@|)
echo'parallel'"_`calc_and_format@.det_nonzero|: lines
NB. transpose parsed input to get (a d/b e/c f). 
NB. If det_nonzero, call calc_and_format, otherwise constant 'parallel'
NB. Print
\$\endgroup\$
2
\$\begingroup\$

OCaml + Batteries, 163 characters

As straightforward as it gets:

Scanf.scanf"%fx%fy=%f\n%fx%fy=%f\n"Float.(fun a b c d e
f->Printf.(fun u->if u=0.then printf"parallel"else
printf"(%.2f,%.2f)"((b*f-c*e)/u)((d*c-a*f)/u))(b*d-a*e))

Edit:

  • Initial version, 169
  • Use Batteries for delimited overloading of operators, 164
  • Lambda binding of u, 163
\$\endgroup\$
2
\$\begingroup\$

C-149 bytes

Not much of golfing just the basics.

float a,b,c,d,m,n,t;main(){scanf("%fx%fy=%f%fx%fy=%f",&a,&b,&m,&c,&d,&n);t=b*c-a*d;t?printf("(%.2f,%.2f)",(b*n-d*m)/t,(c*m-a*n)/t):puts("parallel");}

Here is the ideone link for testing.

Instead of printing 'parallel' an alternative could be to print the orthogonal distance between the lines when they are parallel.

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 10 bytes

∆Q:[vt|`ċ₃

Try it Online!

8 if the format can be ⟨ ⟨ `x` | xval ⟩ | ⟨ `y` | yval ⟩ ⟩ . Returns fraction representation of all numbers if not integer.

Explained

∆Q:[vt|`ċ₃
∆Q          # Solve the two input equations simultaneously
  :[        # If there's a solution
    vt      #   get the last values of each two item tuple
      |`ċ₃  # else, push the string "parallel"
\$\endgroup\$
2
  • \$\begingroup\$ In Vyxal, can we not close If Statement? \$\endgroup\$
    – lesobrod
    Jan 11 at 16:12
  • \$\begingroup\$ @lesobrod that's right. You don't need to if it's at the end of a program \$\endgroup\$
    – lyxal
    Jan 11 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.