See this? This is so cool.
What you're looking at is the new Scratch Pad area for Cody. It will make playing Cody more efficient, less messy, and more pleasant all the way around. If you've never played Cody before, you should try it just to see this new feature in action. Here's how it works.
There are more than 4,300 problems on Cody. So how do you decide where to start? I use Groups to focus my energy on one particular set of themed problems. I love the glow of satisfaction I get when I complete a group (and win the accompanying badge).
Lately I've been working on Indexing IV, the aptly-named fourth in a series of groups focused on indexing problems. Here's one I was just looking at: Problem 2366. Neither minima nor maxima. Given a vector, return all the elements that are neither a local minimum nor a local maximum.
So I go to the problem page and click on Solve.
As soon as I get there, I can see that something is different. Now, instead of just a space for me to write my function, there's also something called "Scratch Pad" and another area called "Output". I can write my function, as usual, in the Solution area. But now, instead of having to submit the code right away, I can try it out a few times with the Scratch Pad. So, instead of blindly writing some code and hoping for the best, I can set an input value for x and call the function and see if it's returning the correct answer before submitting it.
So let's see. Let's take a vector like [1 2 1 0 1 2 1]. The correct output (this is one of the examples in the problem statement) is [1 1 1], because the two 2s are local maxima, and the 0 is a local minimum. How would I write code to solve that? I think I'll use diff, since the element-by-element difference has to change signs at a local extreme (it appears we're only concerned with single point extrema in this problem).
My first effort looks like this:
d = sign(diff(x)); dd = [0 d;d 0]; y = x(sum(dd)~=0);
Using the Scratch Pad, I'm able to verify that I get the correct answer to all three of the examples shown in the problem statement. Feeling confident, I press the Submit button.
Arrgh! It turns out this fails on test 4, when the input vector x is flat. I should have thought of that as a test input.
I tighten up my code, and this time it works. Huzzah!
d = sign(diff(x)); dd = [0 d;d 0]; y = x(prod(dd)~=-1);
Now comes perhaps my favorite thing about Cody. Once I've solved the problem, I can go see how other people solved it. Some people used diff like me, but what's this? Some of the solutions use functions I've never seen before. Here is the current leader's code.
What?! If I had known about those, this problem would have been simple. Let's do some research...
Sure enough, there they are in the doc: islocalmin and islocalmax. When did they get added? I'll show you a nifty trick to find out. Scroll way down to the bottom of your function reference page, below the See Also line, and you can see the MATLAB version in which a given function first appears.
Isn't that cool? Now I know about two useful functions that have been shipping for four years! I'm a little disappointed that I didn't already know about these, but the important thing is that now I do. And the only reason I know is because of Cody. I learn so many tricks and special functions this way.
And so will you, if you join the fun. And the fun just got even easier with Cody's new Scratch Pad.
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。