6 kyu
Solving Quadratics
290 of 328wthit56
Loading description...
Algebra
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Something's wrong here. Just truncate the solutions to like 3 decimal places. That would turn this into great kata.
The problem has been pointed out but I could not see an issue raised so I do it: there are issues in Haskell tests:
I sacrificed my honor points to understand this problem - Here is what I found:
The only difference between an accepted solution and my code was the order of Type 'Two' roots; ( -b +sqrt) ( -b -sqrt ) . When I flipped those to ( -b -sqrt) ( -b + sqrt ) I immediately got the green light.
Thank you! Indeed that's the issue. Still needs to be fixed...
Thank you so much. This is silly, but it works.
If a = 0, then the equation is not a quadratic.
This is not adding a layer of complexity to this task, it just makes it unneccessarily long and boring.
Remove this fixed test.
Fixed
The error messages formatting is broken in JavaScript.
The
latex
text in the description is broken.This comment has been hidden.
No sample tests.
[JavaScript] The random test cases do not seem to allow for any floating point errors whatsoever - for a while, my (perfectly valid and efficient) solution was failing, like, 30 tests only because the values returned in my array differed from the expected values by around
0.00000000001
. Luckily for me, I was able to eventually guess the order in which the calculations should be performed which allowed my solution to return floating point values that matched the expected results exactly.Oh no! That's not good! I've added a fuzzy precision to the comparisons (to the JS translation, at least). This was written quite a while back, so I apologise for my naiveté ;P
Your dedication to passing the kata is pretty amazing, though! Well done!
I've just tested the Kata again and everything seems fine now, thanks for taking the time to fix this Issue :D
No worries. Thanks for pointing it out; it's always supremely frustrating with an author forgets that and makes the whole kata just out of reach for 90% of coders... : )
Falsifiable (after 2 tests and 1076 shrinks): expected: Single (-0.0) but got: Single (-0.0)
I see no difference at all between expected and actual. But the test seems to be thinking differently.
Which language are you using? Is there any other info about which test it might be specifically?
Hey @wthit56, I'm trying to get into algorithms to sharpen my skills and use those abilities to solve more complex katas here, but what's the best articles/books out there you wold recommend me to look into for starters? You seem very knowledgeable on how you write things down, before coding. I'm studying math btw, but I'm not in the Trig/Calc just yet. I'm more in the Algebra 2 level. Unfortunately, I was trying to find a way to contact you a days ago, but I coudn't reach you anywhere outside of codewars. Sorry about that :)
I'd recommend a site like HackerRank.com. It's kinda like Codewars, but a little more organised and curated. It's got a great Algorithms section with tasks ordered by difficult, in different sections you can tackle at your own pace.
As for Algebra, you can take a look at KhanAcademy.com. It has a ton of topics other than maths, but the maths section is particularly full. You can watch explanation videos and take mini tests as you go, to make sure you've understood what you just learned.
Hope those help. Any other non-code-specific questions, feel free to catch me on G+. You can make a post that's only visible to me, which is the easiest way. I would give you my email address, but only if you could see it quickly and I could take it down immedaitely afterwards. With a private post on G+ I wouldn't have to worry. ;D
Thanks for the advice! HackerRank looks very interesting. I really appreciate you taking the time to recommend me those sections. I followed you on your G+ account. I'll contact you from there :)
Thanks!
Can't access Math?
Interesting... Which language are you using?
JavaScript. Actually, I tried it again last night, and I was able to get it to work. Bizarre...
I don't think you can call Ax^2+Bx+C=0 a quadratic if A = 0...
Well... near enough. I'd call
0x^2+5x+10
a quadratic; it's just a matter of semantics. It adds an extra layer of complexity to the task, so I'll just keep it in...If 0x^2 is a quadratic then all equations are quadratics because you can always add 0x^2 to any equation and it will not make a difference.
I'm not trying to be pedantic btw, just found it annoying that my quadratic solution didn't work. Consider changing the title.
Anyway, appreciate the effort.
That is true. The way I see it, these are just edge cases to be handled by the function, though. That's why I added it to the description. It shouldn't be too tricky to solve those kinds of equations, anyway.
Thanks for your input, all the same!
Haskell: Experiencing the same issue as BalinKingOfMoria and jhfung.
Also:
Meanwhile:
ax^2 + bx = 0
→x(ax + b) = 0
→x = 0
orax + b = 0
→x = 0
orx = -b/a
Haskell: I think there are some floating point issues. I'm experiencing the same problem as BalinKingOfMoria.
Haskell: "Falsifiable (after 2 tests and 1076 shrinks): expected: Single (-0.0) but got: Single (-0.0)" Huh?
During which test does this happen? What parameters did QuickCheck generate? Also, could you (again) share your code? There might be some floating point problems which I didn't address when I originally translated the kata.
This comment has been hidden.
The description should probably contain a sentence on what
x
is. After all, you're looking for ax
withAx² + Bx + C = 0
.Haskell: How can you pass the test "returns a Single solution if a is zero and b is non zero" if "a" being 0 would cause division by 0? Should it be a Single NaN?
If
a
is zero, you have0x² + bx + c = 0
. You should be able to solve that one without the formula ;).DUH!
Having issues passing the minimum allowed number of Math.sqrt() calls. It's telling me I have 5, but it needs to be 4 or less. I'm at an impass on how to reduce the problem further. My code passes all of the tests, except for this one. I am checking the discriminants first and only computing sqrt() for the case of two real solutions. I thought perhaps it was the case of sqrt(1) that may be uncessary and easily bypassed, however, that doesn't seem to be the case. I'm now stuck trying to figure out how to satisfy this black box's issue with having an extra sqrt() call. Any suggestions?
Do you ever perform two
sqrt
s in a single quadratic call?I don't believe so, unless I'm misunderstanding how JS is behaving.
I intend to compute it once per function via: var sqrt_discrim = Math.sqrt(discriminant); I then use the value of'sqrt_discrim' to compute both solutions; -b + sqrt_discrim ...
Without seeing the Test Cases, are there 5 that require the calculation of two real solutions? Am I missing a trick with one that would require me to not have to perform the sqrt() call?
The answer is almost certainly yes, as my solution got away with the stated minimum number of sqrts. Could you paste in your code, and mark it as "spoiler"? Maybe I (or someone else) could figure it out.
I agree, this is a bit silly. I am only using ONE Math.sqrt() call. I calculate my determinant, then create
var root = Math.sqrt(determinant);
How is that FIVE calls? I am calling it once in the function.This comment has been hidden.
@hey - The count of
sqrt
calls is over every test, not just a single call. That is why that test is at the end.That makes sense to me. You'd completely cut out that sqrt call. I don't understand what's unclear and/or stupid about that.
I don't know what's going on with that perf test, but
Math.sqrt
is a notoriously expensive in all environments and languages. That is why this kata is aimed at reducing those calls as much as possible.Thanks to heyitsmattwade I realized the "trick" I missed; checking for c === 0.
For some reason Math.sqrt() is not working for me, I get
TypeError: Property 'sqrt' of object #<Object> is not a function
Ah. That's odd. Are you calling it from within your code? Could you paste your code in a reply and mark it as a "spoiler", please?
This comment has been hidden.
This comment has been hidden.
Was there any solution to this? I'm having the same issue.
I'm having the same issue, getting this when running the tests:
TypeError: Property 'sqrt' of object # is not a function at solveQuadratic
@xamvolagis - Your code produces a result of [0,0] on a certain test. There should never be 2 values the same; in such cases there is only 1 valid value for x. Therefore, only 1 value should be returned in the array.
@pez - Your issue is a little different. You are returning "0" as a possible value, when there are no possible values for x. If all values are 0, there is no valid value for x, so
undefined
should be returned.Just FYI, I am not getting the original problem of
TypeError: Property 'sqrt' of object #<Object> is not a function
. I am simply getting incorrectly generated results from the given code. Could one of the people that provided code double-check that the error is still coming up?This comment has been hidden.
I have the same problem, if I try to return in the first line Math.sqrt(9)
It raises that error......
@pez - Running your code, I am not getting the
TypeError: Property 'sqrt' of object #<Object> is not a function
error. It gives me a failed test. Could you paste in whatever red text is in the message that is stopping your solution running, please?@davidgg - not sure what you mean, there. Which error? And the code would be useufl? Maybe start a new thread--it's getting quite confusing with everyone cramming into the same thread with (potentially) different issues.
@wthit56 I only can see one error in the original message that I'm answering.............
TypeError: Property 'sqrt' of object # is not a function
I think that the tests that check the solution are doing dirty things, and obviously bad. The same code in JavaScript console works (different browsers and so)
This comment has been hidden.
My code passed the first 6 tests but oddly doesn't pass the 7th (which seems identical to the first...) My error message: For the quadratic equation: 0x2 + 2x + 3, expected: x = -1.5 ([-1.5]); got: found values: x = undefined (-1.5)
Not sure where I'm going wrong here... Thank you in advance for any help!
If there are possible values for x, an array should be returned containing those values. So you are returning a single number, but you should be returning that number within an array.
Thank you!
No probs ^^
Here's a type of quadratic equation I don't believe is explicitly tested, but could be to slightly increase difficulty: equations where
B = √(4AC)
andA
,B
, andC
are nonzero. For instance,y = x² + 2x + 1
(i.e.,A = 1
,B = 2
,C = 1
). In this case, the value inside the quadratic formula's radical is zero, so we shouldn't really have to callsqrt
.On the other hand, I guess this is sort of hard-coding a value in the solution (in that we know the square root of zero is itself), and opens the door to hard-coding other values in the solution, like substituting
1
for the radical whenB² - 4AC = 1
, or even substituting2
whenB² - 4AC = 4
.I'm pretty sure most of the tests are non-zero. And there are edge-cases where there are zeros involved explicitly added in. Unless I'm misunderstanding you?
This comment has been hidden.
Ah, I see what you mean. I think I've fixed that now?
This comment has been hidden.
Ahh... very clever. I'm now throwing an error if you try to use it in such a way ;P
This comment has been hidden.
The message should say something like "You passed the tests with x calls to Math.sqrt." So it's the total number of calls over all tests, not just from a single call.
Other than that, there may be further ways of reducing the need to call Math.sqrt within your code. It's been a while since I wrote this kata, so I can't remember all the algebra associated with it, but that's probably the problem, anyway. Perhaps someone else can give you further help?
Is this question resolved?
Minimizing calls to Math.sqrt is a bit overzealous for this problem imo.
The thing is, simply replicating the formula in a function is super-easy, so would be kinda pointless, I think.
For the quadratic equation: 0x2 + 0x + 0, expected: cannot find x; got: x = undefined
How is this different?
If the message says "x = undefined" it means you returned an array with no items, or
undefined
as the first item. If x cannot be found,undefined
should be returned, not an array. I'll try to make this clearer in the test messages.I've added some better messages for the tests now; is it clearer?
There's something strange going on in the test cases. I use
console.log
to log my result before I return it, and my result is the expected result. But the test fails with "got x = undefined" in several cases.Could you paste in the code here for me, and mark it as "spoiler"? I'll still be able to read it, and use the code to figure out what's going wrong. Also, if you could give me the test along with the result (the entire failed test message shown), that would be great.
Hey, here's a better image you could use...
Ooh-- thanks! Was that from a generator or something?
It's a website that generates SVG images from LaTeX...
Sounds cool... Where can I go to use it?
http://www.codecogs.com/latex/eqneditor.php
Awesome. Thanks.
I've added a credit next to the image, with links and such. Thanks for your help!
Hi,
I'm getting "For the quadratic equation: 0x2 + 2x + 3, expected: x = -1.5; got: cannot find x" I'm coding to when a === 0, return undefined, because you cannot divide by 0 (to found x: (stuff) / 2 * a).
The description confused me when said a, b, c could be 0.
Effectively,
0x^2 + 2x + 3
==2x + 3
. In cases where one or more term = 0, x's value should still be found and returned, though maybe you don't need the quadratic formula for that. /hint hint/Does that answer your question?
yes, thanks for the hint. i was thinking only in solutions that need the quadratic formula :)
Cool. Could you mark it as "resolved" for me then, please?
ok, thanks.
Cheers
I fell over the same thing. Given the formula, x should be undefined when a is zero.
I'm getting a different number of passes and failures everytime I submit. Also getting my errors cut off at the end with "expected x = undefined, instead got x = u"
expected: x = undefined
, that would be a bug in my testing code, though I can't figure out what could be causing it. If you do see that in the logs, please let me know what the quadratic equation was so I can try and fix the bug.This comment has been hidden.
Could you give me direct copy-paste examples of the errors referred to? I'm talking about ones which say something like
expected: x = undefined
.This comment has been hidden.
Thanks. I think I've fixed that bug now. It was just the message generating that wasn't right. Still unsure what causes messages to be clipped; maybe there's an upward limit for test failure messages...
Just want to point out:
[undefined]
or[undefined, undefined]
is not the same asundefined
. If there are no valid values,undefined
should be returned. If there is one valid value,[value]
should be returned. If there are two valid values,[value1, value2]
should be returned.Is this issue resolved?
I'll mark this as resolved.
I'm not sure if it's my code or something in the test cases, but I keep getting the following error:
A random number of tests are passed each time before that error arises, and I have not yet discovered an association between the input values and that error message.
Ah-- sorry. Seems an error made it through; normally it won't let you Publish a kata unless it all runs fine. Thanks for bringing it to my attention!
That error is gone now, but I think I've spotted a faulty test case, and I have a question:
a = 1
,b = 2
,c = 0
, I gotFor the quatratic equation: 1x2 + 2x + 0, expected: x = 0, x = undefined; got: x = 0, x = -2
a = 0
,b = 0
, andc = 0
. In that case, the equation we're solving would be0 = 0
, which is perfectly fine: all values ofx
are solutions. We need to either know that this case is excluded or what the return value should be for this case.0, 0, 0
is one of the tests. With quadratic equations, you are "solving" it by finding the values of x. When there is no "x" value in the equation, no vlaues for "x" can be found. Effectively, you cannot solve for "x". Soundefined
should be returned. I've tweaked the description a little; hopefully it's clearer?For the quatratic equation: 1x2 + 2x + 0, expected: x = 0; got: x = 0, x = -2
(and also just noticed the typo: 't' instead of 'd' in 'quatratic')