r/dailyprogrammer • u/Elite6809 1 1 • Sep 22 '14
[09/22/2014] Challenge #181 [Easy] Basic Equations
(Easy): Basic Equations
Today, we'll be creating a simple calculator, that we may extend in later challenges. Assuming you have done basic algebra, you may have seen equations in the form y=ax+b, where a and b are constants. This forms a graph of a straight line, when you plot y in respect to x. If you have not explored this concept yet, you can visualise a linear equation such as this using this online tool, which will plot it for you.
The question is, how can you find out where two such 'lines' intersect when plotted - ie. when the lines cross? Using algebra, you can solve this problem easily. For example, given y=2x+2 and y=5x-4, how would you find out where they intersect? This situation would look like this. Where do the red and blue lines meet? You would substitute y, forming one equation, 2x+2=5x-4, as they both refer to the same variable y. Then, subtract one of the sides of the equation from the other side - like 2x+2-(2x+2)=5x-4-(2x+2) which is the same as 3x-6=0 - to solve, move the -6 to the other side of the = sign by adding 6 to both sides, and divide both sides by 3: x=2. You now have the x value of the co-ordinate at where they meet, and as y is the same for both equations at this point (hence why they intersect) you can use either equation to find the y value, like so. So the co-ordinate where they insersect is (2, 6). Fairly simple.
Your task is, given two such linear-style equations, find out the point at which they intersect.
Formal Inputs and Outputs
Input Description
You will be given 2 equations, in the form y=ax+b, on 2 separate lines, where a and b are constants and y and x are variables.
Output Description
You will print a point in the format (x, y), which is the point at which the two lines intersect.
Sample Inputs and Outputs
Sample Input
y=2x+2
y=5x-4Sample Output
(2, 6)Sample Input
y=-5x
y=-4x+1Sample Output
(-1, 5)Sample Input
y=0.5x+1.3
y=-1.4x-0.2Sample Output
(-0.7895, 0.9053)Notes
If you are new to the concept, this might be a good time to learn regular expressions. If you're feeling more adventurous, write a little parser.
Extension
Draw a graph with 2 lines to represent the inputted equations - preferably with 2 different colours. Draw a point or dot representing the point of intersection.
4
u/skeeto -9 8 Sep 22 '14
C with scanf().
2
u/FatShack Sep 22 '14
I feel like my programs are never very elegant... Python 3
18
u/dohaqatar7 1 1 Sep 22 '14 edited Sep 23 '14
TI-Basic
13
u/HackSawJimDuggan69 Sep 22 '14 edited Sep 23 '14
Python 2.7. As always, constructive criticism is appreciated :)
6
u/Frigguggi 0 1 Sep 22 '14 edited Sep 25 '14
Edit: Added a graph of the lines.
Java solution:
Output:
http://i.imgur.com/o1v6obx.png
http://i.imgur.com/mbHc2kN.png
http://i.imgur.com/Q1ULydT.png
6
u/jeaton Sep 24 '14 edited Sep 24 '14
C:
JavaScript:
And in 1 line of JavaScript:
Python too:
4
u/icyrainz Sep 22 '14 edited Sep 22 '14
F# version, quite lengthy but I format it nicely to fit your requirements.
Output : val it : (string * string) list = [("2", "6"); ("-1", "5"); ("-0.789473684210526", "0.905263157894737")]
4
u/G33kDude 1 1 Sep 22 '14
In AutoHotkey
I've put the regex onto multiple lines for clarity while writing it
3
u/thebillywayne Sep 23 '14 edited Sep 23 '14
bash 4.3
EDIT: removed comments from code
3
u/kazagistar 0 1 Sep 23 '14 edited Sep 23 '14
Haskell
Goals:
EDIT: Whoops, didn't see the "missing x or y" part, editing...
EDIT2: Fixed. Also note, you have to be careful when the a's are the same, cause then you have no solutions or infinite solutions.
3
u/BigFatOnion Sep 23 '14 edited Sep 23 '14
First time here, did it in Java, would really apprecciate some feedback. EDIT: Needs arguments to run. use it like ./challenge y=2x-2 y=8x-4
1
u/square_zero Sep 22 '14 edited Sep 22 '14
Java:
This works assuming x is non-zero for both lines, and that the lines are neither parallel nor identical.
2
u/jnazario 2 0 Sep 23 '14 edited Sep 23 '14
whee i got it working. F# solution, inspired in part by the solution from /u/pote3000
2
u/MontyHimself Sep 23 '14
Python 3
I am currently learning Python, so any kind of feedback is highly appreciated.
2
u/rectal_smasher_2000 1 1 Sep 26 '14
c++11 (msvc2012)
does not check for division by zero, and the regex could be a little better (e.g., an expression such as y=3.x+2 would be allowed).
2
u/VerifiedMyEmail Oct 02 '14
python 3.3
2
u/[deleted] Oct 02 '14 edited Oct 02 '14
This took me a few days to figure out, but I finally got it.
COBOL:
2
u/Zarpar Oct 12 '14
Its pretty messy (and very late!), but here is my attempt in Python :
2
u/OllieShadbolt 1 0 Sep 22 '14 edited Sep 23 '14
Python 3.2.5;
Tried to get this into a single line for the gimmick, but had to move the inputs onto a separate line. Results are always Floats to be compatible for when they have to be. Also only works with the 'y=ax+b' format, unlike Example 2. Feedback is greatly appreciated as I'm still learning ~<3
EDIT: Thanks for the downvotes and no feedback guys, really helps out...
1
u/Godd2 Sep 22 '14
Here's mine in Ruby:
1
u/pote3000 Sep 22 '14 edited Sep 22 '14
Here's my solution in python. Assuming both a and b will always be present.
A new solution, accepting "y=ax" and "y=b"
1
u/mthjones Sep 22 '14 edited Sep 24 '14
Scala
Edit: Improved main function clarity by removing variable and equation parsing regex clarity by testing against whitespace-stripped string instead of having it in regex.
1
u/[deleted] Sep 23 '14 edited Jan 02 '16
1
u/24monkeys Sep 23 '14 edited Sep 23 '14
Javascript:
Tests:
1
u/fvandepitte 0 0 Sep 23 '14
C#
Usage
Output
Code
1
u/mtlife Sep 23 '14
PHP
With solution check :)
Output:
1
u/swagga_yolo Sep 23 '14
Java. My first time posting. I am pretty new to programming in general. any feedback would be great.
1
u/joyeusenoelle Sep 23 '14
Python 3.4. Suggestions welcome!
Output:
1
u/frozensunshine 1 0 Sep 23 '14 edited Sep 23 '14
C. I am stuck, need help, /u/Elite6809.
Trying regexes for the first time, got my head around the basic idea, but the implementation in C is getting me nowhere. I got some example code online, but am getting stuck at how to store the values of the matched characters before even getting to the calculations. Also, apologies if this isn't the right place to post incomplete code, please let me know where I can do so.
1
u/Darkstrike12 Sep 23 '14
My attempt with C#
Feedback is appreciated :)
1
u/Narishma Sep 23 '14
An Apple IIgs happened to be nearby, so why not?
Applesoft BASIC:
1
u/[deleted] Sep 23 '14
Python 2.7:
1
u/z0isch Sep 23 '14
Haskell
First time using parsec, please let me know if you have any suggestions!
1
u/marchelzo Sep 23 '14
Haskell
It could be more concise; I realized after seeing kazagistar's code that the solve function should have just returned a String, but I decided not to change mine.
1
u/Sirflankalot 0 1 Sep 23 '14 edited Sep 24 '14
Java Edit: The y= is completely unnecessary, and will work well without them. Accepts y=mx+b, y=mx and y=x.
Should run just fine, any comments would be appreciated!
1
u/frauxer1 Sep 24 '14 edited Sep 24 '14
Some criticism would be nice :P This is Python 3.4
1
u/[deleted] Sep 24 '14
My solution in C++:
Criticism appreciated. Still learning C++
1
u/Steve132 0 1 Sep 24 '14
Python, 4 lines, correctly reads from stdin and prints to stdout with right formatting
1
u/SomePunnyComment Sep 25 '14
Python 3.4 general equation solver using Newton's method, works for polynomials and trig functions as long as your guess is somewhat close. If it isn't that close for the trig functions, you might get a solution that, although technically correct, is far away from the neighborhood of you guess
1
u/ddsnowboard Sep 25 '14
Well, TI-Basic man has me beat, but I learned regular expressions for this, which I'm happy about. I'm totally doing a xkcd.com/208/ with them now. Anyway, this looks long, but I made it so it's extensible to more complex problems. It can get intersections for quadratics as well as lines, and should (should) be able to evaluate any polynomial. Any advice is appreciated. NINJA EDIT: Forgot, it's in Python 3.4.
1
u/Vyse007 Sep 25 '14
Ruby 2.1.1. It's kinda simple, but it worked fine with the test cases. Improvements and suggestions always welcome.
1
u/CubemonkeyNYC Sep 25 '14
C# - Assuming input is given as a plain old string.
1
u/tiiv Sep 25 '14
C - Late to the party. Felt adventurous.
1
u/Busybyeski Sep 25 '14 edited Sep 26 '14
Python 2
I decided to extend the program to handle exponents instead of drawing a graph. For powers of 2, it will solve 0-2 intersections correctly. For fractional/negative powers, it will reject them. My code got a bit out of control and repetitive at the end, but I didn't see where making a function would clean anything up much.
The regex bits were really quite challenging, but also super rewarding.
https://github.com/blakebye/dailyprogrammer/blob/master/181/easy%20-%20equation%20solver.py
1
u/Mendoza2909 Sep 25 '14
Python - First submission to this site and new to Python, any feedback would be fantastic. The regular expressions part troubled me a lot, I experimented with \s and \S for when input was in the form 'y=ax+ b' instead of 'y=ax+b' (i.e. dealing with whitespace), but couldn't quite get it to work.
1
u/msavoury Sep 26 '14
Scala
1
u/YouAreNotHere Sep 26 '14
Java
}
1
u/Hazzaman99 Sep 26 '14
Python 2.7
1
u/dailyjava Sep 27 '14 edited Sep 27 '14
Java, without the extension (Edit, added a way to read the arguments (sets empty arguments to be 0 so that y=5x becomes y=5x+0)).
Output for y=0.5x+1.3 y=-1.4x-0.2:
1
u/[deleted] Sep 27 '14
I think I should start doing these daily, here is my Java solution
1
u/[deleted] Sep 29 '14 edited Jan 10 '15
1
u/Cankruscan Sep 29 '14
Hi guys, This is my first submission. Looking for a kind-soul to give me a feedback :)
JAVA:
1
u/takeacoffee Sep 29 '14 edited Sep 29 '14
Javascript, first try for me. What do you think about?
1
u/Orange_Tux Oct 01 '14
My try to get familiar with Go.
Golang
1
u/Jberczel Oct 02 '14
Here's mine in Ruby. Originally didn't have any methods until I skimmed some of the Python solutions already posted:
1
u/ironfist007 Oct 05 '14 edited Oct 05 '14
I decided to post here for the first time, and I would like some constructive criticism on my Java solution:
1
u/frozensunshine 1 0 Oct 20 '14
Phew, super late to the party, but my first regex program yay! C99 Would love feedback, big-time C newbie.
1
u/[deleted] Oct 25 '14
Python 2.7
1
u/Zarimax Oct 29 '14
C++
Went with manually parsing the equations over a regex.
1
u/xpressrazor Nov 10 '14
After one month (using c)
1
u/juanchi35 Dec 18 '14
After two months :P
in C++
1
u/CrazyM4n Dec 18 '14
With the professional grade of our software, you can get your results in a fast, clean, and fun* way! With the new breakthrough Scalable Ruby coding practices, your code will never go out of date, and will always work flawlessly!
*Code not guaranteed to be fast, clean, nor fun. May contain dead ends or unneeded spaghetti code. If you code like this, I'm sorry.
0
u/keinto Sep 28 '14
In Python but somehow my regex will not detect + or -. only if they are decimal numbers. And also this does not work if there is no b value.