Coding Challenge #2 – Polynomial
Welcome to the second Coding Challenge.
Here I will post a coding challenge every few days that you can solve. There will be easy ones and hard ones, pretty mixed.
How does it work?
- Try to solve the challenge in whatever language you find fitting
- You can use a new language you always wanted to learn or show off your golfing skills
- Post a comment with a link to your code (use https://repl.it or similar sites that allow execution of your code) and some info on how the code works or what is remarkable
- Optional: Create a post with the tag #coding-solution
- In that post walk other users through the process of solving your challenge. That way they can learn and try it out on the next one
- Optional: Read and review other peoples code, try to find bugs, give feedback and learn
Why even bother?
- Training is everything, the more you train your mind to think in code, the better you are prepared
- You may learn a few new tricks from reading other solutions
- I will send tips for really good solutions (and use the liquid rewards of this post for it too)
- You may get recruited if someone likes your code (f.e. I am looking for talents)
Challenge #2 – Polynomial
This challenge will be an interesting one, easy to do but hard to get right.
Wikipedia context: https://en.wikipedia.org/wiki/Polynomial
Implement a Polynomial
class taking an a string formula
that abides to these specifications:
- parse the polynomial given as a string, f.e.
+1x^0 -5x^1 +3x^2
- implement a few methods,
add, substract, multiply, divide
- implement the standart to-string method of you language to give back a polynomial in above format
Remarks:
- The format is pretty easy to parse. If you want a challenge, try accepting more loose polynomials like
1+2x
or3x^{12}-6 x^2
. - Try adding more methods,
derive
andintegrate
will be fun. - Bonus points for:
- handling edge cases
- good coding practices and modularity
- useful and meaningful tests
- beautiful (readable, self-documenting) code
Please keep the #coding-challenge tag clean of solutions.
Here you go:
https://steemit.com/coding-challenge/@philosophist/coding-challenge-2-polynomial
Hope that fits a good bit of what you were looking for @reggaemuffin
Definitely :)
Haha, you said the next challenge would be a bit harder than FizzBuzz, but this is quite the brain teaser. I'm not even sure if I'm going to fully implement it, but I'm working on a solution that's definitely going to take me a while.
Expect a post about it though, it's definitely good practice and I'm having fun solving this one.
My main problem right now is parsing the input and output in more loose styles, omitting things like
x^0
.A few tips: Only the first + can be omitted, so after that a + or - is a delimiter. if no x is present, use x^0 and if x is present, use x^1.
so I would first split by +I- and then work on each number. If you want to allow x^{-1} then ignore +I- inside {}. Each number can be split at the x and the left part is the number, the right part (if exist) the x^y.
I'll be keeping this challenge on for a bit so you have time to complete it :)
Thanks for the hints! I actually got the input parsing solved already, clean output is almost done but I'm moving on to the problem of adding now. Making progress :)
Thanks....... upvote and follow
I've only implemented the parsing. The code is accessible at https://repl.it/KQWN. Since I used a parsec library, you cannot run it there. :/
I don't parse the variable names, so it's possible to use multiple variables mistakenly.
This is pretty cool! Can you elaborate a bit on what language you used, how you build up the grammar etc.?
There are three parsers related to polynomials:
poly
term
andnumber
.Line 15: All parsers remove the trailing spaces, so the main parser
poly
should remove the leading whitespace. It basically states: skip the whitespace,poly
is made of manyterm
s and you arrive to the end of the input.Line 18:
term
is a tuple of anumber
and anothernumber
. The second number is located after anidentifier
and the symbol^
.Line 21-25:
number
is either negative or positive.<|>
stands for either. For the second case the number may not start with the sign+
,option
takes care of that.f
computes the value of the string of numbers.Line 28: We run the parser
poly
on the sample input. It prints out the result as a tuple ofterm
s as indicated in the parser descriptions.Thank you for the explanation! Parsec is really strong, looking forward to leaning more about it :)
It is and I'm using it in very basic form. There're very good documents at https://github.com/haskell/parsec
I implemented
showPoly
,add
,subtractPoly
, andmultiply
functions.Oh snap, the simplify looks crazy! It would be so cool if you could make a post explaining how each little piece works in many details, once you are finished.
There are basically three transformations.
sortOn snd poly
poly is a list of tuples of two numbers for each polynomial term's coefficient and power. First step sorts the list of tuple by its second element which is power.
groupBy ...
We group all terms by their power.
groupBy
returns a list of lists. Each list represent a power value.map f
For each list of lists we
foldl
the list which is basically reducing the list to a single value. For instance if we have5x^2-3x^2+x^2
it evaluates to3x^2
.Once you get used to functional programming, most things will look very clear. ;)
Take a look: http://learnyouahaskell.com
I would agree.
meep
beep loves coding ? :D
meep
I think this is Functional Programming.....
looks like Haskell or F#... not sure.
Checking the author is is most likely Haskell, that was the language he submitted the last challenge in.
It's Haskell. The links provide the information but it wasn't clear at first sight.
Whew, all done. Here's my post about it: https://steemit.com/coding-solution/@pilcrow/javascript-solution-coding-challenge-2-polynomial
It became pretty long, but as it's the first complete answer maybe it could help other developers out?
I am looking forward to see some Solutions. Unfortunately this is above my head.
@raggaemuffin I'm not sure what you are asking us to code here. We take a string that is a polynomial, change it to a math code, and then return the same string we started with? Are we factoring, simplifying, solving, or plugging in a solution value for x?
I understand it like this:
Am I correct on this one?
So, does it come with a second input to plug in for x, then?
@bandli is correct. You get a string representation of a polynomial and should implement a class that parses that and allows to fo math with it:
If you want to have an
evaluate
method that could be fun tooIt might help a lot if you have one or two examples like this in your post, so we can include them in our test cases. It makes understanding the problem at hand a bit easier. In the real world I would say "specifications unclear" and send it back to the Product Owner ;)
You are right! I wanted to post it and probably should have refined the specification a bit before that. Next challenge I'll see to make up to that
Oh! You want us to create a polynomial class and return an object of that given polynomial! Is that it?
I want you to create a polynomial class that has a constructor accepting a string :)
Wonderful topic I hope to follow up https://steemit.com/@safamostaafa
wow....very interesting,i just started learning coding a few weeks back.still learning html and css .i hope to participate in subsequent one.let see how this one goes.
42