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
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'
tf = 'false'
function tf = is_odd(n)
if rem(n,2)== 0
tf = 'false'
tf = 'true'
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
Easy enough that I can do it!
how to reduce size
i have the size of 28
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!
Nice and simple. Thanks for the points :)
what is wrong here?
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
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)
tf = 'false';
tf = 'ture';
why assertion failed. it works in matlab.
my solution was right and i tested it i m not sure what is going on
function [tf] = is_it_odd(n)
my answer correct
How can be a smaller size? tf = rem(n,2)
Had cofused mod with % in python
What is the problem here?
this work on matlab but not here, why?
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'
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;
tf = true;
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.
This solution works offline?
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.
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.
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.
% i mean that: test = is_it_odd; :D
Why is function "odd" not working?
Is this code wrong?
How could i get this any shorter?
Why is this solution not working?? works on my matlab 2014 version!!
I think the ' ' characters might be the problem.
it should use mod.
realized my true/false assignments were off. They should be reverse...if mod(n,2) ~= 0; tf=true; else tf = false; end
can anyone tell me better solution than this?
Well done brother :)
function tf = is_it_odd(n)
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;
I think the assertion is flawed, because
isequal(is_it_odd(2),true) == true == 1
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).
Why doesn't " if mod(n,2)==0, tf = 'false', else tf = 'true', end " work?
it should be tf = false
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 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??
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'.
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)).
Prime factor digits
The Answer to Life, the Universe, and Everything
07 - Common functions and indexing 6
Find the nearest prime number
Remove the vowels
Determine whether a vector is monotonically increasing
Find state names that end with the letter A
Find the longest sequence of 1's in a binary sequence.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office