Here's a smart one liner for you!

preview_player
Показать описание
This line of code uses a pretty ingenious way to manipulate the range of a number. In this short, we take a quick look to understand how it works!

While this is not really good programming practice because it puts too many operations on one line (as opposed to using if-statements), it takes quite a smart approach at solving this problem!

-----

-----

Disclaimer: Please note that any information is provided on this channel in good faith, but I cannot guarantee 100% accuracy / correctness on all content. Contributors to this channel are not to be held responsible for any possible outcomes from your use of the information.
Рекомендации по теме
Комментарии
Автор

I’ve become so mindbroken by interview quizzes that nowadays when I face a problem that has a straightforward series of conditionals that would satisfy the problem I find myself sitting there obsessing over whether or not there isn’t some modulo, shift or other operation trick that would do it instead. Sure it’s less readable, but what if leetcode types flex on me oh

Eyetrauma
Автор

In my opinion I think if the one-liner was documented with:
// clamps value to domain [0, 10]

then the functional approach would be objectively more clear and concise. Of course the if statement approach is faster due to lack of the function call overhead.

kevintyrrell