Given a number n, create an n-by-n matrix in which the integers from 1 to n^2 wind back and forth along the rows as shown in the examples below.
Examples:
Input n = 3 Output a = [ 1 2 3 6 5 4 7 8 9 ]
Input n = 4 Output a = [ 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 ]
nice one
good one
i got same solution on matlab, but i don't know why i didn't here
I am no expert, but does anyone else think that the first test case is flawed? "%%n = 4;" instead of " %% (new line) n = 4;" So, "n" is never defined for the first case. Am I right? If not, I apologize!
that seems legit
nice one
Really enjoyed this one.
Can someone help me? My solution works on my matlab but gives me an error during the test! What am I supposed to do ? :(
I think the answer to test suite 5 should be c = 2 since the first row is 0.21 and the second is 0.26. Please correct me if I'm wrong.
Awesome!
Awesome
Great
Check this out.
function b = back_and_forth(n)
b=1:n^2;
b=reshape(b,n,n)';
for i=2:2:n
b(i,:)=fliplr(b(i,:));
end
end
It appears a '\r' is needed after the %% in the first test of the solution to make it process properly.
The problem with the first test case has been fixed here.
Check this out.
function b = back_and_forth(n)
b=1:n^2;
b=reshape(b,n,n)';
for i=2:2:n
b(i,:)=fliplr(b(i,:));
end
end
my code works on my PC but not on this..
a = [1:(n^2)];
a = reshape(a,[],n)'
How can u do it in 10
I also want to know.
I can't write shorter program.
(^.^;)
very easy
gj
Why is this not working please?
a = ones(n,n);
for i = 1:n^2;
a(i) = i;
end
a = a'
I've tried this in school and it worked fine... :O Could you please help and explain? :)
Look closer at the requirements. Even rows should be flipped, such that their values are in descending order when reading from left-to-right.
Why is this problem showing errors in line 6 when I have a code of 4 line?
It works on matlab
i got same solution on matlab, but i don't know why i didn't here
good Question
really clever :)
Sorry for the nasty solution. I'm hoping the use of str2num and anonymous functions will eventually be limited, since most simple problems can be solved with this cheat.
Hi,
Could you comment how using str2num and anonymous functions decreases the score from 46 to 13? Is it a glinch in the notation system?
Regards
Hi, Florent.
The scoring in Cody is based on the number of nodes in the parse tree, and strings only require one node.
You can have a look for yourself by typing:
'>> mtree('back_and_forth.m','-file').show'.
Try to do it for the same code, without str2num, and you'll see that even small things like parenthesis or plus signs require their own node in the tree.
In terms of performance, the two versions are not so different. The problem here is that every solution, that use str2num in this way, will have the same Cody score, regardless of how elegant or efficient the anonymous function is.
I really like how you've split the matrix into two. That's a clever insight! And I am really interested in knowing WHY str2num requires only one node.
brilliant!
clever!
abs(sort(-cumprod(-ones(n)).*reshape(1:n^2,n,n)',2));
This one is more clear.what do you think?
well done,why can't come up with this method
Sorry, I just applied a slightly involved ans trick to the previous best solution. I hurts me though that the best solution involves a for loop.
This should work
This is faster and nicer than calling an external function to flip the direction.
Determine whether a vector is monotonically increasing
8789 Solvers
1893 Solvers
Given a window, how many subsets of a vector sum positive
648 Solvers
The Answer to Life, the Universe, and Everything
305 Solvers
180 Solvers