MATLAB Answers

Problem 10 of MATLAB cody challenge

26 ビュー (過去 30 日間)
NAVNEET NAYAN
NAVNEET NAYAN 2018 年 7 月 28 日
編集済み: Guillaume 2020 年 2 月 2 日
I was trying to solve this question in cody challenge: Problem 10. Determine whether a vector is monotonically increasing. I tried following code:
i=1;
while i<length(x)
if x(i)<=x(i+1)
tf='true';
else
tf='false';
break;
end
%
i=i+1;
end
When I am running this piece of code on MATLAB editor everything is Ok. But when I am submitting this, incorrect answer results. Format to make a function for this problem is given as:
function tf = mono_increase(x)
tf = false;
end
Can anyone sort it out?

  0 件のコメント

サインインしてコメントする。

採用された回答

Dennis
Dennis 2018 年 7 月 30 日
There are a few problems with your solution:
  1. You need to return true/false - not as string
  2. You compare values to be bigger or equal -> [1 1 1] is not increasing, but your solution return true
  3. Your solution wont work with single values, because x(i+1) does not exist
A working solution based on your approach might look like this:
if length(x)==1
tf=true;
else
i=1;
while i<length(x)
if x(i)<x(i+1)
tf=true;
else
tf=false;
break;
end
%
i=i+1;
end
end

  3 件のコメント

NAVNEET NAYAN
NAVNEET NAYAN 2018 年 7 月 30 日
Thanks...I was making the mistake what you have pointed out in above 3 points.
Guillaume
Guillaume 2018 年 7 月 30 日
A proper implementation in matlab would do this without a loop (e.g. see Paolo's answer). If you were to implement this with a loop, the following would be cleaner:
function tf = mono_increase(x)
tf = true;
for idx = 2:numel(x)
if x(idx) <= x(idx-1)
tf = false;
break; %for a better cody score, omit this line. for a better implementation, keep it!
end
end
end
Any loop implementation will score very poorly on cody.
NAVNEET NAYAN
NAVNEET NAYAN 2018 年 7 月 31 日
thanks Guillaume for the worthy suggestions.

サインインしてコメントする。

その他の回答 (2 件)

Paolo
Paolo 2018 年 7 月 28 日
You can use:
all(diff(x)>0)

  4 件のコメント

表示 1 件の古いコメント
Paolo
Paolo 2018 年 7 月 30 日
Ah, that's interesting :)
function tf = mono_increase(x)
tf = all(diff(x)>0)
end
gives size 16. Whereas
function tf = mono_increase(x)
tf = issorted(x, 'strictascend')
end
gives size 13.
How exactly is the size computed? And how can the leading solution be size 9?
Guillaume
Guillaume 2018 年 7 月 30 日
Most of the very low scoring solutions no longer work. The two 9 overwrite assert.m with a system command. The 10s use regexp with a dynamic regular expression to execute code and bypass assignment to the function with the so-called ans trick.
For better or for worse, the score of a cody solution only depends on the number of nodes in the parse tree of the code. You can see the parse tree of code with the undocumented mtree class.
t = mtree('tf = all(diff(x)>0)');
t.rawdump
t = mtree('tf = issorted(x, ''strictascend'')');
t.rawdump
%also
%t = mtree('-filename', 'nameoffile.m')
There are plenty of tricks to get low score. Command form, if possible, cost less than function form. A char array regardless of its length only cost one so stuffing code in text helps. Compare:
str2num 1+2+3+4 %command form cheaper than function for
mtree('str2num 1+2+3+4')
1+2+3+4
mtree('1+2+3+4')
Omitting assignment using ans also helps:
%score 11:
function ans = mono_increase(x)
issorted(x, 'strictascend'); %no assignment so result is assigned to ans
end
As demonstrated above, low scoring solutions are rarely good matlab code and are often inefficient. They are optimised for the scoring system, not for the speed of execution or readability.
Paolo
Paolo 2018 年 8 月 1 日
Thanks for the detailed answer Guillaume.
Very interesting indeed, those are some cool tricks. Wouldn't have thought people had put all this effort into hacking cody!

サインインしてコメントする。


Sriram Nayak
Sriram Nayak 2020 年 2 月 2 日
i=1;
while i<length(x)
if x(i)<=x(i+1)
tf='true';
else
tf='false';
break;
end
%
i=i+1;
end

  1 件のコメント

Guillaume
Guillaume 2020 年 2 月 2 日
I'm afraid this is is not going to work. The char array `true` and the logical value true are not the same at all.
In term of cody score
i = 1;
while i < endbound
%do something
i = i + 1;
end
is going to score you very badly against the equivalent and much simpler for loop:
for i = 1:endbound
%dosomething
end

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by