Something weird is happening
When I submit the size of my code is -1 and it fails the test suite when it seems to work fine in MATLAB desktop.
would you please let me know why
sum(x) creates error?
easy
function y = one(x)
z = double(num2str(x))-double('0');
y = sum(z);
end
Interesting problem
ASCII
Can someone check this code?
function y = one(x)
split1= num2cell(x)
sum=0;
for i=1:length(split1)
if split1(i)=='1'
sum=sum+1
else
sum=0
i=i+1
y = sum;
end
What's wrong with this problem!!
used y=y+str2double(x(i)) instead of y=y+str2double(x(i)) and it worked just fine
function y = one(x)
y1=[];
for a=x(1:end)
y1 = [y1,str2num(a)];
end
y=sum(y1);
end
y=0;
for index=1:length(x)
y = y + str2num(x(index));
end
y = sum(x=='1');
Well, well! Here he/she is as well, great job continuing your copy and paste of the cheap and meaningless trick you prick!
In case you didn't know how to do it and wanted to know how -which somehow I doubt it for some reason!, but anyhow, here is one of MANY ways to do it:
function x=one(x),x=count(x,'1');end
y = length(find(x=='1'));
Why doesn't this solution work? Works just fine in my desktop program...
Because you compare a char Array to a double. Try (x=='1').
Project Euler: Problem 3, Largest prime factor
385 Solvers
middleAsColumn: Return all but first and last element as a column vector
390 Solvers
Find out missing number from a vector of 9 elements
245 Solvers
Find the Oldest Person in a Room
5244 Solvers
1863 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!