From now on, anytime I can chain together a composition of functions, I will never exceed a Cody score of 12! Thank you!
I also would encourage people not to submit this sort of dynamic expression regexp trick solutions indiscriminately. Having said that, and just for reference, you can push this down to a Cody score of 10... (hint1: use varargin; hint2: use 'command syntax' instead of 'function syntax').
To keep Cody from degenerating into a bunch of unreadable party tricks, we've blocked usage of eval, evalc, and a few others. But regexp is too important for us to block, so this trick will remain available into the foreseeable future. Which means, to back up Alfonso's comment, please don't abuse it. It's funny once, and then it just gets annoying.
function y = bit_reverse(x,n)
x = dec2bin(x);
if n>length(x)
for i=1:n-length(x)
a(i)='0';
end
for i=1:length(x)
a(n-length(x)+i)=x(i);
end
for i=1:n-1
y(i) = a(n-i);
end
else
for i=1:length(x)
y(i) = x(length(x)-i+1);
end
end
y=bin2dec(y);
end
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
assert(bit_reverse(1,1) == 1)
|
2 | Pass |
%%
assert(bit_reverse(4,3) == 1)
|
3 | Pass |
%%
assert(bit_reverse(2,3) == 2)
|
4 | Pass |
%%
assert(bit_reverse(6,3) == 3)
|
5 | Pass |
%%
assert(bit_reverse(5,3) == 5)
|
6 | Pass |
%%
assert(bit_reverse(7,3) == 7)
|
Remove all the words that end with "ain"
1292 Solvers
Remove the two elements next to NaN value
411 Solvers
Set the array elements whose value is 13 to 0
935 Solvers
444 Solvers
Sum of odd numbers in a matrix
311 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!