This problem nicely illustrates a very frustrating aspect of Cody: if you have no idea how to solve a problem that involves some sort of 'trick', you are completely stuck with no chance to learn or improve upon your solution. I've spent an inordinate amount of time today trying a variety of solutions to this, and I suspect the correct solution involves something like arrayfun or cellfun or eval. But I can't get any solution to work, no matter how 'ugly' or 'brute force' it is. I'm really curious as to how to do this, since function handles clearly are an area I need to improve my skills in. At least with other problems you can usually come up with some sort of solution, go solve another problem, and then come back to look at all the 'elegant' solutions and hopefully learn from them. With problems like this that type of feedback cycle is impossible.
I would agree. I had an eval solution that worked on my older Matlab version, but using eval is prohibited by the test suite. Test suite also checks for more than 32 levels of parenthesis,ruling out a str2func solution :(
This is the kind of problem I love and hate at the same time, was worse than nightmare when I was a student. I was able to solve with great relief after searching by the keyword 'functions' in Matlab Help and looking at the examples presented there.
Right now, I'm in the same boat as Alan was when he wrote his comment.
Is there any chance of getting a "You win, Cody. Just tell me how to solve it, pretty please with sugar on top." button for some of these problems?
A little hint: funception
For anyone who gets stuck on this problem, or who wants more practice with function handles, I've created a few more similar problems: http://www.mathworks.com/matlabcentral/cody/problems/581-function-composition, http://www.mathworks.com/matlabcentral/cody/problems/582-function-composition-harder, http://www.mathworks.com/matlabcentral/cody/problems/583-implement-a-counter
@freddy: what is funception?
I really got stuck with this problem although I managed to solve those from David Hruska.
@CODY TEAM: I suggest that when a player reaches 1000 points , he receives a joker that he can use to get the solution for 1 problem ;)
@Aurelien Queffurust: My intention with funception was to combine func(tion) and (inc)eption - based on the film. Function Iterator is quite similar to this "new" word creation, but in the first case, one could use e.g. str2func solutions. My intention was point to think of a nested concept.
Ouf! I finally managed to solve it! FYI, I have created a trend for this issue with trendy : http://www.mathworks.com/matlabcentral/trendy/plots/862
to monitor how correct solutions are computed for this problem ;)
Now this happens to be the last problem in the Cody Challenge that I still have absolutley no idea how to solve. I skipped it it a while ago so I could work on the other problems. I am absolutely lost and feel very similar as Alan. Unfortunately, reading through the links you suggested also did not help. Would it be possible to at least provide a small hint on how to structure the code? I think I get what the code is supposed to do but I am completely in the dark about how to build it up. Any help will be greatly appreciated!
Don't give up Pascal. This problem has been THE nightmare for everybody. You can find some hints in the Loren's Blog (http://blogs.mathworks.com/loren/).
Still not making an progress. I know it's a little obtrusive, especially considering that this is just a game but would you mind if I wrote you an email explaining my thought processes so far?
I finally got it! Thanks again for your help :)
Hey..can sb help me? I'm stuck on Test1 and Test5. Why in Test1 the answer is pi? I thought it should be 50, as we pass 50 to the function and no matter how many iternations are it should return 50. And Test5 - why 1.6180? We input 30 and iterate once so sqrt(31) is 5.5678. The rest of test seems to be ok for me.
this sort of problems i like to see more on Cody
Took me way longer than i thought it would.
phew! that was so hard for someone has little understanding of functions let alone sub functions, function handles and nested functions.Those are my tips by the way :) They're saying nested function is way to go but the hardest thing to nail for me was function handle actually!
Also first examples can be deceptive, at first I thought n was not the iteration time and found golden ratio to be sqrt(31) but actually they want you to iterate 1 with the golden ratio function, 30 times.
I don´t believe... I didn´t... Very interesting probleam really, easier to make in paper
Wow! This was a wild one to throw in this spot, after only 23 sort of basic problems for the ones that do the challenge in this order.
Shout out to those fellow members who pointed out to the nested functions documentation.
Probably it would help a lot if the explanation was a bit more clear. For example, that thing about "returns a scalar output and an integer n >= 1" is very confusing. The only thing that's returned is the scalar output. Probably you meant that the function takes a scalar input and an integer n>=1—which is actually true—but it's not clear.
Another thing, the examples can really be misleading, especially that addTen handle, which doesn't really mean "add ten" but "add one ten times."
Anyway, great problem. I see it served as an introduction function handles and nested functions for many, including myself.
That was tough !
New to sub-functions and getting the syntax correct (understanding it ) was tricky. Arguments in - out shake them all about.
new concept for me after 3 years. now gotta use
I solved the problem based off the following example:
http://matlab.izmiran.ru/help/techdoc/matlab_prog/ch_fun12.html
useful challenge!!!!
I don't know if anyone else has mentioned it but, is there a list of forbidden functions for each problem?
I had the whole thing worked out on my own Matlab install only to fail all the tests because I used func2str or str2func.
There have been other problems where a one-line solution was perfectly fine. It just doesn't seem to be following any kind of consistent rule.
It's hard to me for I have no idea about function handle in MATLAB before I meet this problem.
I have a question, this code ran well on my Matlab. But it reports "Undefined function 'syms' for input arguments of type 'char'." all test.So I have no idea what's wrong with it. Can somebody help me?
Wow a useful challenge!
I agree, was tough one for me at my current knowledge level.
This took me 3 days but I am old and slow.
Work through, modify, play with and understand the examples given in the Matlab documentation about Nested Functions.
My solution is adapted from their second example: makefcn.m.
Will also work for n = 0.
I thought I'd need a class to save 'fh' and 'n', but it seems that nested functions are magical.
Cheater. :-) (And not even *that* short.)
Good to see someone else try the recursive approach. It may be larger than the iterative alternative, but in my opinion it is more elegant.
That was amazingly hard! Never used function handles. And I think i never will...
Interesting to see you used the same variable name for both input and output of your nested function.
How do I understand this? Never seen such a kind of code before.
Nice use of an anonymous function in your recursive approach. (I had used a nested function in an 'iterative' approach.)
Hmm, my version of Matlab doesn't have that function :)
WTF?
Nobody's version of Matlab has that 'function'. Have a look at https://au.mathworks.com/matlabcentral/cody/problems/1774-free-passes-for-everyone
"iterative iterator"
Hey, I finished the cody challenge a few months ago but when I just checked back my old solution to this problem is no longer valid! So my solution was retroactively rejected! What gives Cody team?
Yes... sorry about that. It turns out the STR2FUNC function was being badly abused in the name of writing short answers. Since we saw almost no places where it was being used responsibly, we decided to invalidate all answers with STR2FUNC. It's a compromise, but we think on balance that it's the right thing to do.
I hit like because this is the first "leading solution" that is not a cheat.
I hit like because this is the first "leading solution" that I have seen that is not a cheat.
To be fair, you should point out in the preamble that you cannot use the 'eval' command in your code
Have tried some solutions, but couldn't figure out a working one...
I managed to solve it thanks to matlab help. Tip: "Nested Functions"
Solved it after seeing your message about nested functions. Much appreciated.
292 Solvers
533 Solvers
372 Solvers
637 Solvers
4268 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!