Given the input n, return true if n is odd or false if n is even.
Added several more tests to limit hard-coded answers.
When they say return "true" they mean "1", and "false" means "0". Newbie mistake.
Add a couple of negative values to the test.
Test for a couple of negative values
Delete comment doesn't work!
I don't know what I am doing wrong. I tried it in Matlab and seems to work fine
y = rem(x,2);
if y == 1
disp('true')
else
disp('false')
end
it should be clearly specified that we should not type in the string true but if we give true to a variable then it takes it as 1
All solutions with score 10 use the regexp cheat.
I don't understand why my code isn't working?
if mod(n,2) == 1
tf = 'true'
else
tf = 'false'
end
function tf = is_odd(n)
if rem(n,2)== 0
tf = 'false'
else
tf = 'true'
end
end
why is this not working?
I bet all the solutions not using if else are all from engineering backgrounds with intimate knowledge of boolean logic
ipek: you only need one end.
Nolan: you aren't using the variables provided
eine interessante Frage...
Persistent server error trying to evaluate this solution...
Hello it's me
Nice one
Good one.
Nice problem
nice
good job
horrible problem
good
great
good problem
Great problem
great prob
Easy one.
kool
Easy enough that I can do it!
badge
easy
how to reduce size
i have the size of 28
Good Problem
Pretty Easy. Thanks!
Good use of the modulo operator. Took some quick presence of mind to realise how to use it, but it is definitely quick to crack.
Advice: Don't overthink it!
Thank you!
Nice and simple. Thanks for the points :)
Thank you!
What exactly the error is
whyyyyy
great problem
what is wrong here?
help aha?
come on really? whats wrong with this?
You need to assign your answer to the output variable tf.
...
why is this solution incorrect?
I think there is a problem with the Cody server since yesterday.
Apologies for the down time. It should be working now
You actually don't need the Boolean assignments, technically a 0 and a 1 as the output will be good enough
so easy
I have tried many times this one. It runs fine but I am not able to understand why is it showing as wrong solution.
Can anybody help on this?
Remove the single quotes in true/false. It might work. ☺
function tf = is_it_odd(n)
if mod(n,2)==0
tf = 'false';
else
tf = 'ture';
end
why assertion failed. it works in matlab.
Thanks for your answer, you had a typo in true, also you should remove ' ' from false and true
very easy
good
easy
good job
good
good job
gj
good job
too demanding
good job
my solution was right and i tested it i m not sure what is going on
function [tf] = is_it_odd(n)
if rem(n,2)>0
tf='true';
else
tf='false';
end
end
my answer correct
good job
How can be a smaller size? tf = rem(n,2)
good job
I don't think that simply using tf=rem(n,2) should be allowed, its implicit casting which isn't a good thing to depend on.
Had cofused mod with % in python
good job
What is the problem here?
good job
this work on matlab but not here, why?
good job
My code works but it fails the test suite despite it giving the correct output. What's the deal?
true, not 'true', false, not 'false'
good job
Assertation failed.
Code works perfectly on my computer.
Must be a bug with this particular problem.
It seems to provide the desired solution. Could anyone tell me why this is an incorrect solution?
The output should be logical,and it should't be char type:
function tf = is_it_odd(n)
x = n/2;
y = floor(x);
if x == y
tf = false;
else
tf = true;
end
end
good job
good solution
Does anyone know why this doesn't answer the problem? When worked out in matlab it works just fine.
1. do not use "input(...)", "n" is defined variable and input doesn't return anything. 2. "=" is used for assignments, not for comparisions. This one returns syntax error in line 3 (if ...), use "==" instead. 3. Output should be logical, not string. Use "false" instead of "'false'" etc. 4. Return the output via variable specified for it in first line of the function, in this case it is "tf". Do not use "fprintf" for that. 5. Read The Friendly Manual. Just type "doc" in Command Window - there are tons of documentation, tutorials.
good job
This solution works offline?
good job
How does this help anyone? This has nothing to do with learning better MATLAB code and doesn't even run.
This is not useful. What a waste of time...!
Can you please explain how that solution work? thanks
On the positive side of it, I'm very happy not to see people blindly copy pasting the above solution for every problem. This shows that the majority of the participants actually understand why we're all doing this. BTW, smart workaround!
Hi Ali, what is the workaround your speaking of? I am very confused by this solution, but I would like to understand it.
Even though it doesn't solve the actual problem, I thought this solution was clever enough (without too many copy-pastes by others, or maybe MathWorks fixed this workaround since then) to merit explaining it.
For those wondering, the line of code within the is_it_odd function does the following:
1. It does a system call to echo a single line to the standard i/o (a virtual place where programs can share/exchange their inputs and outputs).
2. The single line is in the form of a Matlab inline function, named assert(), that black-holes any inputs and always outputs the boolean 'true.'
3. The system call takes this single inline Matlab function and replaces the contents of assert.m (of whatever structure/setup for Matlab interpretation that Cody uses) with that single line.
4. Cody checks solutions with a series of assert(isequal(stuff,things)) calls, which now always evaluate to be true in this particular instance, as opposed to actually comparing if 'stuff' and 'things' are the same.
good job
Nice work on a creative solution that has the "smallest size" per Cody's grading criteria. However, as a beginner to MATLAB, I don't see why I would want to write something this long/complex when there are much shorter solutions (perhaps not per Cody's criteria but they use less characters). I guess it's all about winning the contest.
want to how can be size 9
9...............how can you do that......
I tried this in my pc, all results appear as it is in your ans_correct.
good job
what is wrong with the solution?
You're returning the string "true" or "false" which the test suite is trying to compare to a boolean variable true or false. Since the string does not equal the boolean value, assert fails.
good job
% i mean that: test = is_it_odd; :D
good job
Why is function "odd" not working?
good job
Is this code wrong?
tf=floor(n/2)*2==n
yup
good job
How could i get this any shorter?
good job
Why is this solution not working?? works on my matlab 2014 version!!
I think the ' ' characters might be the problem.
good job
it should use mod.
good job
realized my true/false assignments were off. They should be reverse...if mod(n,2) ~= 0; tf=true; else tf = false; end
good job
can anyone tell me better solution than this?
Well done brother :)
function tf = is_it_odd(n)
mod(n,2);
end
This is called cheating....Even this code is failed
for 21 size. little editing :)
function ans = is_it_odd(n)
if mod(n,2)==0; false;
else
true
end
bugger
I think the assertion is flawed, because
isequal(is_it_odd(2),true) == true == 1
returns
ans = 1
Here, false and 0 (as double) mean same.
Also true and 1 (as double) mean same.
what the matter,such an easy problem prevent me from continuing solving the following problems,so why,l can't accept the format of the solver function,it is so stupid,it is not flexible,l can't stand
this returns "1" & not "true", but the validation for this cody problem is spurious since isequal(true,1) is actually true (when it should really throw an error).
Seems to me that the "best" answer would be tf = logical(mod(n,2)). But I guess the description does not explicitly require logicals, so maybe I am reading too much into the problem. Using logical makes the solution 17 compared to 13 without.
by calling logical function you add extra code.
I'am bad!
I don't think you know what odd/even means.
odd = 1 3 5 ...
even = 2 4 6 ...
I know this isn't correct, only submitted it because I couldn't figure out how it was possible to solve the problem with code size 12, and wanted to see the solution, only to find out that the 'leading solution' was wrong.
Does it work if n=3 or greater??
Pretty confusing!!
Ofcourse not, the problem with cody is that you can see the unit test.
But the good thing about Cody is that you can update the test suite. Thanks for the comments, and please keep flagging bad tests so we can fix them.
This doesn't return a logical 'true' or 'false'.
Persistent server error trying to evaluate this solution...
Hooray for ambiguous classing requirements.
How about this for the "best" solution:
x_odd = x(is_it_odd(x)); % :/
The answer should be logical(mod(n,2)).
7657 Solvers
220 Solvers
209 Solvers
501 Solvers
Find the Oldest Person in a Room
1182 Solvers