how does this work some clarification

9 次查看(过去 30 天)
can anyone explain how this solution work i tried to read documintations for this but did not really connect the dots
  1 个评论
dareen
dareen 2024-3-19
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

请先登录,再进行评论。

采纳的回答

Voss
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
n_str = '2024'
last_digit = n_str(end)
last_digit = '4'
location = regexp(last_digit,'[02468]') % location not empty
location = 1
empty = isempty(location)
empty = logical
0
tf = ~empty % number is divisible by 2
tf = logical
1
Case 2: for an odd number:
n_str = '2025' % odd number
n_str = '2025'
last_digit = n_str(end)
last_digit = '5'
location = regexp(last_digit,'[02468]') % location empty
location = []
empty = isempty(location)
empty = logical
1
tf = ~empty % number is not divisible by 2
tf = logical
0
  6 个评论
dareen
dareen 2024-3-19
oh i see what an annoying use is this , so it just give an illusion of an eaiser code and lowers the code readiablility so not the best thing to learn form thank you for sharing
Voss
Voss 2024-3-19
You're welcome!
"not the best thing to learn from"
Exactly!

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
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?
  1. An even integer is one that has a zero remainder when divided by 2.
  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.
  1 个评论
dareen
dareen 2024-3-19
thanks for the inshight i did not encounter regex before and i wanted to know if it is good practice to use or not ,so my goal first was to understand how it works and how its used and secondly to decide to adapt it or not , as for how the algorithm for checking if its is an even or odd number the basis i know how thats i understand , would you mind elaborating more on how regex leads to an obscene code in this case

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by