i must say thank you all,i really like this community and how encouraging it is, it makes one want to ask more questions and learn more with how diverse , respectful and well written the answers are
how does this work some clarification
2 次查看(过去 30 天)
显示 更早的评论
can anyone explain how this solution work i tried to read documintations for this but did not really connect the dots
采纳的回答
Voss
2024-3-19
This is the part of the solution that's relevant to solving the problem:
tf = ~isempty(regexp(n_str(end),'[02468]'));
The way it works is by taking the last digit of the number-as-character-vector n_str, which is n_str(end), and using regexp to find the location within that single digit where it is any of the characters '0', '2', '4', '6', or '8'.
The last digit is only one character long, so the location will be 1 if the digit is '0', '2', '4', '6', or '8', and the location will be empty otherwise.
Then isempty is used to test whether the location found by regexp was empty or not: if the location was empty isempty returns true; if the location is not empty isempty returns false.
Finally, the result tf is the logical negation (~) of what was returned by isempty, so that if the location was empty then tf is false (i.e., the number was found not to be divisible by 2), and if the location was not empty, the result tf is true (i.e., the number was found to be divisible by 2).
You can break it down into steps and inspect the result after each step.
Case 1: for an even number:
n_str = '2024' % even number
last_digit = n_str(end)
location = regexp(last_digit,'[02468]') % location not empty
empty = isempty(location)
tf = ~empty % number is divisible by 2
Case 2: for an odd number:
n_str = '2025' % odd number
last_digit = n_str(end)
location = regexp(last_digit,'[02468]') % location empty
empty = isempty(location)
tf = ~empty % number is not divisible by 2
6 个评论
更多回答(1 个)
John D'Errico
2024-3-19
Honestly, this Cody answer is one of the reasons why I think Cody is seriously flawed. It encourages people to write obscene code, all for the purpose of gaming the scoring aogorithm. And then others see the code, and decide it is actually good code, because it has a low Cody score. (Puke, retch...)
How does it work? That part is trivial.
Can you tell me several ways to decide if a number is even or odd? What distinguishing characteristic does an even integer have, versus an odd one?
- An even integer is one that has a zero remainder when divided by 2.
- An even integer ends with a units digit in the set {0,2,4,6,8}.
Surely, I can think of some other rules that would work too. But the two rules above are the obvious ones.
Option 1 is easily tested. Sort of. Just test if rem(x,2) == 0. The problem is, you are passed not a number, but a string representation of that number. And test 1 in the test suite aims to prevent you from doing anything of the sort.
The solution the problem author probably wants you to find is one where the units digit of the number is extracted, then see if it is one of the valid even digits. That is all that was done here. Even better though, is to game the scoring by putting complex code into a string, then effectively using regex to do the dirty work.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!