Another of the classic "check if MATLAB has a function that does this already" problems...
What about a vector like [2 3 1]? The top solution will still return true because 1^2 is still one but it is not the square of one of the OTHER numbers.
is the Str2func construct really more effective???
Is it possible that this distorts the metric a bit?
It is currently impossible to submit a solution for this problem. The web page will never load.
The problem description states that one of the numbers has to be a square of one of the OTHER numbers. Still, the test suite tests if [1] gives true. I think [1] should give false, or that describtion be modified.
Agree with Mattias. the length of input vector should be 2 or larger.
The difference between test 4 and 5 which returns different answers is the number 36.. but 36 is not a square root of any number of the list in test 4!!
great solution
Agree with Mattias: the Test Suite is defective, and not consistent with the problem specification.
Having an input of [1] is OK, but result should be false, as there is no OTHER element to compare the 1 to!
can you share the shortest code?
easy
Fun question!
the word "other" should be ignored in the description...
fun
Good question!
Cool
Clever problem
good
The heading reads:
"...a square of one of the OTHER numbers...".
How can the function be expected to return [true] for a = [1]?
It makes no sense.
Good question
This problem is very good
いい問題。
sumかnnzか
intersect的返回值为一个向量，即使a和b的交集为空集，返回值仍为向量
x=[20:30]
例如：intersect(x.^2,x)
ans =
空的 1×0 double 行向量
所以isvector(intersect(x.^2,x))是错误的
而isempty是判断数组是不是空的，所以isempty可行
Tough one, but interesting
I solved it with 2 for-loops, 1 function, 1 if-condition.
It works, but there are more efficient ways apparently
Hey,the demand of this problem declare that "if one " and "the other",so if the intersect of x and x.^2 only have one value of 0 or 1,it may be return false because their squares are themselves, which is not satisfy the condition implied that c and c^2 is not one.
Nice Problem
The question asks if one number is the square of one of the OTHER numbers. Test 3 should return false.
Great problem
I don't understand why #4 if false. Can someone help me?
Test case #4 is true, not false, because the vector contains a perfect square and its square root.
Your function is not passing because it overwrites the check variable for each vector element, only actually checking the last value to return the true/false variable. It happened to pass the other four cases because their answers (either false or true) happen to correlate with your check for the last (or only) element in the vector.
a = [20:30];
assert(isequal(isItSquared(a),false))
Assertion failed.
It should be true because in the array there is 25.
a = [20:30]
a = 20 21 22 23 24 25 26 27 28 29 30
The question asks that whether a perfect square and it's root both are present in the array or not. You have to check for both and not one only.
Hi,
I verified this code with the given test suite. but the system is not accepting the code as it is.
thanks
TR
What's wrong with this code? Runs perfectly on my computer.
whoops, don't know how I managed to confuse .^ with .* for a bit there ---I was like, "What's WRONG?"
nice
You cheater!
This type of cheat/hack answers are no longer allowed.
Good question
0 and 1 elements in a will show the bug in your code
nice one-liner
a vector 'a' with elements 0 or 1 will show the bug in your code
cool!
Wow! This took me a couple of days to solve, but I did it and I’m very proud! :D
Hi. A vector with ones or zeroes as v = [3, 1] will show the bug in your code.
Lol
Test case : a = [20:30] contains a square. but its expected error is false. It should be fixed
hello,
where do you find a perfect square in [20:30]?
Hi Jann B,
25 is a perfect square, no?
How is the output true if the vector contains only one number? People posting such meaningless solutions: Please have some common sense!
easy easy
That's better...
a vector as v = [3,2,7,0,5] will show you your code's bug
a little smaller now
Nice ☺
Works
just kidding...
very easy
I want to make this code shorter!!
Case2,a = [20:30],fails.
Others pass.
Why???
in 2 test suite in a=[20:30] , contains 5^2 =25 so it has to be true not false
But there seems to be a better choice. Why forgetting ismember?
Test 2 in the test suite is wrong!
25 is clearly a perfect square.
the problem is to see if a number is the square of another number in the vector. Although 25 is a perfect square, 5 is not included in the vector
Though it passes the test suite, I feel like I could make this a better way but I am drawing a blank. Any feedback would be appreciated.
good
if a = [20:30] the output should be true as 25 is a perfect square, while in the test case output is given to be false.
25 is not a square of any other members of the group. If the group included 5 it would be true.
hur dur durrr durr. sqrt(25)==5, not included in givens
Yeah, I did it
My solution returns false for [20:30]; why not for [6 10 12 14 101]?
Instead of a(1):a(end) you should just write a.
Where did I do wrong？
The desired results are not the character strings 'true' and 'false' but the logical values true and false.
hint ; any and ismember.
submitted tested solution but still showing error......
L 17 (C 1-3): Parse error at END: usage might be invalid MATLAB syntax.
interesting solution... I like it :)
I disagree. Almost every problem has a solution wrapped in this function. It's cheating as it hides the complexity of the code from the scoring algorithm. That's why "eval" is not allowed ...
This is the correctly executing in matlab
I wonder...if you cheat, why even bother to actually solve the problem?
I have not heard of ismember before, thanks.
As primitive as my solutions are, I learn just as much from reading other peoples as doing the problem itself. Great setup mathworks :)
Code works on Matlab, but not on your test suite?
Srtr2func, classic way:)a new thing learned.
test case 3 should be 'false'
it states 'one of the other' numbers, so for a = [1] it should be false
Assertion 2. is not correct. vector 'a' return with 6th element as 25 & thts a perfect square.. So it must be true!!
Given an unsigned integer x, find the largest y by rearranging the bits in x
819 Solvers
Number of 1s in the Binary Representation of a Number
362 Solvers
342 Solvers
435 Solvers
336 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!