Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
You can use memoization to improve it!
extreme shorthand for if-else, basically
smart
?: is the ternary operator.
https://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do
Wait what does the ? even mean
I really need to get used to this form of If(?)...
bad algorithm
it has exponential complexity: Theta(2^n)
it´s a good solution, simple and good.
great explanation
Wow, that's a really nice answer! Thanks bro!
That really clears my mind!
A good heuristic for deciding between the ternary operator and if/else is whether you are choosing between side effects or between calculations. For example:
Here it should be clear that the choice is between two actions. Contrast with:
In this case we're choosing one of two calculations. You could rewrite the first example to use the ternary operator, but then it's easier to miss the side effects.
There is a strong tendency for beginning programmers to want to use language constructs to make code terser and denser than it should be, without considering the factor of readability. It comes down to the question of whether you have the experience and/or discipline to use the ternary operator when it's appropriate and not just because you want to see if you can fit something on one line. If you're not disciplined enough to consider the pros and cons each time you use it, then it's best to err on the side of verbosity. The same argument holds for the
++
and--
operators and other shortcuts.That's not to say that you personally aren't good enough or experienced enough, but if you think about a policy that has to cover a whole company or a whole open source project, you need rules that will prevent the worst programmer in the group from writing bad code, and so you end up with more always / never prescriptions.
One last thing: Different languages vary widely on how verbose coders tend to be. Things like the ternary operator and using short-circuiting boolean operators for side effects is much more tolerated in JavaScript, for example. The best advice is to try to match what most people are doing for your given language and platform, since it will make it easier to adapt to any given code base in that language, and easier for others to give feedback on or contribute to your code.
This comment is hidden because it contains spoiler information about the solution
For some reason, one of the (somewhat) recent updates to the Codewars platform stopped using the "provided" code during Example Test Cases. Since this is a problem with Codewars, the kata will remain as is.
As a workaround, you can add your own Triangle class to your test case code.
The provided test cases don't work, complaining that they can't find the
Triangle
class, but the final submission does.Sure. Thanks,
Loading more items...