8 kyu
To square(root) or not to square(root)
9,109 of 34,612user5036852
Loading description...
Mathematics
Arrays
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Java:
assertArrayEquals
instead ofassertEquals
by wrapping the actual and expected as String for comparisonI liked this exercise, but i'd say it's 7kyu as it involves an understanding of multiple simple functions.
it's a little bad exersice couse u need to create a new array (not to change old), that make you use more RAM in perspective
Issue: I believe Go tests are broken for the case of generating random numbers. It is always expecting the exact same list of numbers unchanged.
no, u didn't check capacity mb
There is no issue with the tests.
This comment has been hidden.
This isn't an issue with the tests, it's an issue with your code. You're mutating the input.
Issue: C++ translation has a typo in the
ExtraMessage
of the first sample test-case - it lists the output instead of the input. Should beExtraMessage("Incorrect output for the input [ 4, 3, 9, 7, 2, 1 ]:")
instead ofExtraMessage("Incorrect output for the input [ 2, 9, 3, 49, 4, 1 ]:")
. That's the only typo.Issue fixed in this fork.
Merged.
data error [10, 10, 25, 25, 1, 1] should equal [10, 10201, 25, 25, 1, 1]
I learned to how to use the square root function from this kata.
C++ Translation
Approved
This comment has been hidden.
This comment has been hidden.
PHP assertions seem to be backwards. The parameter order for
assertSame
isassertSame($expected, $actual)
, but it's being fed the function output as the first argument, and the expected output second.Fixed.
D translation
Approved!
The tests are broken. Java ; 101 somehow convertet to 10 (under root) and from 54 expect the 7. but the root of 54 is 7,348469228349534
fix it pls;
Nothing to fix. Read the description again.
Rust translation (author gone).
Approved!
The instructions are unclear. It says that if it doesn't match the prior condition, to square it. The tests expect you to square ROOT it though. Squaring it is the inverse operation.
No, read it again:
Which language are you referring to? If it is javascript, the tests are fine:
Chrono, question: whenever it says, "take THIS", does that mean just keep that value and don't square it? (learning disability, sorry!)
No, it means take the square root.
The tests are broken!
This is not a proper way to raise an issue. Please read the documentation: https://docs.codewars.com/training/troubleshooting/
The tests aren't broken, your code is wrong. In PHP, the modulo operator converts both operands into integers before returning a result (which will also always be an integer), you'll have to find another way.
Actually, my solution in Swift is
"input.map { (Double(Int(sqrt(Double($0)))) == sqrt(Double($0))) ? Int(sqrt(Double($0))) : ($0 * $0) }"
...and it works in Xcode playground on my Mac, but local compilator can't build it ((
The kata works fine in Swift. Please read the documentation: https://docs.codewars.com/training/troubleshooting/
Thanks, I'll read
This comment has been hidden.
Don't post solutions in Discourse. Solutions need to be submitted in the trainer, then they appear on the Solutions page, that's enough. And please use a spoiler flag as soon as you post code somewhere, so that users who didn't solve the kata cannot see it (I put the flag for you this time). I advice you to have a look at the offical documentation (the link is available in the left panel): https://docs.codewars.com/
I think the test is broken for java. Or am I wrong? Below is the output from one of the test case Input: [2 9 3 49 4 1] expected:<[[2, 9, 3, 49, 4], 1]> but was:<[[4, 3, 9, 7, 2], 1]>
You're wrong, that's not the input, that's the expected output. It is the first sample test or the example in the kata's description.
This comment has been hidden.
Please use a spoiler flag when posting code (I have added the tag).
Ohh okay my bad. Im newbie here.
Read this. Also, your output is wrong, examine sample tests carefully
Below is the output from one of the test case , which is broken for python.
[1, 4, 9, 2, 25, 2] should equal [1, 4, 9, 2, 25, 36]
Please correct this.
The tests cases in Python are fine:
Please read this: https://docs.codewars.com/training/troubleshooting/
Not a kata issue.
Thanks for your response, there was an edge case where i wasn't evaluating composite number. My apologies for confusion.All good with Kata.
the same for golang
Are tests ok?
expected [ 2, 9, 3, 49, 4, 1, 10, 10201, 25, 25, 1, 1 ] to deeply equal [ 10, 10201, 25, 25, 1, 1 ]
It is working when I test it manually in RunJS i.e.
Tests are ok. And if they were not ok, you don't give the input so nobody could know.
Thanks akar-0 :)
Yeah, same.
test failed, expected output [88,48,60,82] output on my pc [88,48,60,82]
i guess the test is broken
What language do you attempt?
Do you find anything helpful in troubleshooting FAQ? https://docs.codewars.com/training/troubleshooting/#works-but-no
This comment has been hidden.
This comment has been hidden.
Please use a spoiler flag when you post code. I put the flag for you this time. Also, use properly markdown tags to format your code, see there: https://docs.codewars.com/training/troubleshooting/#print-input
Thanks i'm still getting used about how this platform works it
After you check if
result
is a whole number, you insert the original number into the array instead ofresult
(which is the square root of the original number)thanks!
the test is drunk
Error messages in C are backwards.
Fixed?
can't seem to get in the editor from my phone, but i can confirm the sample tests have now been corrected.
fixed (by someone)
Hi, I'm someone, nice to meet you :P
o rite, thanks!
LC translation
Approved
expected:<[2, 9, [3], 49, 4, 1]> but was:<[2, 9, [81], 49, 4, 1]> Why can't there be 3 out of 9? and why should it get 81?
The log is clear: you must return
3
, your code returns81
.This comment has been hidden.
So, same answer...
I think I've just understood your problem: you misread the log. The log tells you
Expected <Correct answer> but got <Your answer>
, it doesn't give the input (you would need to print it. We don't know which language you're using. It seems in C logs are incorrect and reverse expected and actual.COBOL translation (author gone).
Merge conflict :/
I expected that, since I first published it before approving Haskell's translation; updating the description in my fork to newest version didn't work. There is a fork (just removed a space, and it should be approvable...): https://www.codewars.com/kumite/62566ff5db61150063617fba?sel=6256c2b03ee51a0046469bf9
( that's why I mention "this translation changes the description" when a translation of mine does that )
I don't know COBOL. Can I approve in the blind?
I know, and you're right. I opened the translation page before yours was published and didn't check Discourse page when I published mine.
I'm confident with this translation, expected is generated along with the input, independently from reference solution. A blind approval should not harm ;)
I hope you're not generating squares to be squared then :P
This is a comment
Haskell translation
This translation removes "don't modify your input" from the description.
Approved.
This:
Should either be tested (in all languages) or removed from the description (and check if it was tested in some language, remove those tests). Given it's an 8 kyu kata, I would choose the later.
The proposed Haskell translation removes this text.
JavaScript ( and Haskell ) do not test and are not vulnerable to input modification; I can't read other languages unfortunately.
Removed by someone
This comment has been hidden.
Try again, select version 8. Please read this too: https://docs.codewars.com/training/troubleshooting#post-discourse
It sayz it expect 81 but gets 9. Nowhere in the expected result there is 81. Can anyone explain it to me? It is a simple kata and wrong test cases or vice versa? C language. Thanks.
hi, thanks for the comment. your code does not work, but there is a small problem with the kata (error message is backwards) so you are producing the value 81, which should be 9
This is too hard for 8kyu. It's more like 7kyu. just my 2cents...
Ranks cant be changed
Typescript 3.3 or above should be enabled
Fixed
For Go langauge please fix the test cases.
Expected <[]int | len:4, cap:4>: [7744, 2304, 3600, 6724] to equal <[]int | len:4, cap:4>: [88, 48, 60, 82]
thanks for raising this, I couldn't understand what am I missing!
this is still valid issue ... Please can anyone fix it?
This comment has been hidden.
yes, I am beginner and sorry for my mistake but ... when I print input data before I start doing anything
func SquareOrSquareRoot(arr []int) []int{ fmt.Println(arr) ...
then I get on terminal [88 48 60 82]
and in log: Expected <[]int | len:4, cap:4>: [7744, 2304, 3600, 6724] to equal <[]int | len:4, cap:4>: [88, 48, 60, 82]
so expected ouput is equal to input array?
ok ... so to test this case I changed my function to
func SquareOrSquareRoot(arr []int) []int{ fmt.Println("input data", arr) return arr }
and in log:
input data [88 48 60 82] Expected <[]int | len:4, cap:4>: [88, 48, 60, 82] to equal <[]int | len:4, cap:4>: [7744, 2304, 3600, 6724]
so 2 different expected outputs for same input?
EDIT: ok, my mistake ... I dont catch "The input array should not be modified!" that was my mistake ... thanks for patience
R translation added, please review :)
This comment has been hidden.
This comment has been hidden.
It depends on the programmer itself. Since the difference of such code neither impacts performance, speed nor efficiency (throughput) (in the current context), it is like asking
should I start off with Javascript or Python as my first programming language?
^^My code passes all three sample tests, and fails all random tests. The random tests seem to require that I return the input, rather than returning the array correctly produced according to the instructions. This is in python. What gives??? I've spent hours trying to figure this out already.
Read the instructions again, it seems you're mutating the input.
I have already read the instructions, and I have also already read "The input array should not be modified" in many of the comments. In my code, the input, arr, is never modified. I am creating another array x = arr and modifying elements of x. If that is against the rules, then the rules should be made more clear. Moreover, if it is against the rules, then my code should not pass the example tests.
That's not the right way to make a copy of a list in Python. You're only creating another var that points to the same list. The sample tests use a fixed expected result, the random tests use a random generated input and it uses that same input with a control function.
Why cant I use sqrt(), its a inbuilt php function.
You can now.
This comment has been hidden.
This comment has been hidden.
For c langauge please fix the test cases.
Unclear suggestion, closing!
In C: The compiler's output should at least swap the "expected" and "actual" them to be in the correct order.
This comment has been hidden.
Solved two katas and then creating an issue that should be a suggestion??
This comment has been hidden.
241 people solved this kata in Go, so I think there is no issue.
Please check your code. And next time, do not create an issue, if you are not sure for the reason for the failing test.
This comment has been hidden.
Language C. Why in this kata you can return an array from the function and this will be the correct solution ????? When returning from the function, the values should change!
You haven't understood this kata.
Very good kata, original, and the level of difficulty is perfect for a 8kyu
This comment has been hidden.
You should return your newArray.
Answered.
It seems like every random test for CoffeeScript begins with [undefined, ... ].
Oh, yes. You are right! Thanks! It should be fixed now!
@SteffenVogel_79, I forked and fixed the issue with Go translation.
Can you approve this?
(I forked this because I commented on another translation by the same user having the same issue a week ago and got no response.)
Thanks kazk! :thumbsup:
I took your changes to the kata. (I did it in the kata and not by approving because of differences in the descriptions, that lead to problems in approving.)
Okay, this is weird, I initially tried the kata by not modifying the input array and ran into some memory error; then i tried by modifying the array and passed .... Is this an issue in C?
83 people did the kata in C.
I would think that you have a problem in allociating enough memory for the new array.
Maybe post your code here for reviewing. (Mark as spoiler!)
Thanks for the quick feedback .... How do I make my comment formatted so that the code I paste here has an OK format. Kind of a silly question but .....
This comment has been hidden.
the size of an int-pointer is not the the size of the array.
You get the len of the array as an parameter. So use this for multiplicating it with the size of int! Then you get the correct size for malloc.
Thanks..... Silly me :) .... Awesome kata ...
Why are most of the solutions not fullfilling the following instruction?
"The input array should not be modified!"
because the kata doesnt check if the original array is still intact so it doesnt matter if you modify the original array or not, because either way you will pass
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
why did you use a nested for loop? By using the nested for loop, the value j would iterate the length of individual element i.e number is 200, j would be 1,2. I think what you are trying to do is check if each array element can be rooted. If so, one loop would be enough as the first loop would already be going through each element in the array. i.e. array is [1,2,3,4], arr index(i) of would be the elements in the array IF you are trying to find the square root or return the power of the element , you should use the built in functions available for you
My code passes the first and third test, but not for the second. It fails for the second element. Works for everything else, except that and I can't figure out why.
mind pasting your code here so i can help? Also when pasting your code, add "```" before and after your code to format it
I'm not sure if its just me, but my code is running fine however, when it runs the test algorithm to check whether my solution is correct, it always evaluates my code on a different input than the one I use resulting in my code always being wrong as it expects a different outcome. I am coding in Java. I tried changing the input to another one of the inputs used in the basic test algorithm however it then evaluates it with my previous input and gives me a fail. Can someone shed some light on this issue?
Can we see your code ? What you are saying is quite weird !
This comment has been hidden.
Your code is correct, except you obviously need to replace the "input" array with the actual input array named "array".
You got that, right ? :)
ps: oh, and you don't need your boolean :p
Swift translation please review :)
Approved. :-)
Crystal translation there for you :)
Thanks! Approved! :-)
This comment has been hidden.
The output of math.sqrt is always float.
Answered.
This comment has been hidden.
You are using Python 2.7.6 which makes use of integer divison where division results are floored (1/2 returns 0 instead of 0.5 because 0.5 floored is 0). You can fix this by stating it should be float divison (add a decimal point to one of the numbers - 1./2 or 1/2.) or you can change the Python version to Python3 which uses float divison by default and not integer division.
Thank you so much!! It worked!
This comment has been hidden.
Could you post your code here? (Mark as spoiler!)
The []-brackets are to show the difference. It is expected 10201 as second element. But your answer as second element was 10.
This comment has been hidden.
.
OP solved it, closing
TypeScript translation submitted, if you want to approve it :)
Approved! Thanks a lot!
This comment has been hidden.
No, it seems, that you change the given array!
In the description it is said: "The input array should not be modified!"
In doubt post your code here (mark as spoiler) and we will try to find the problem.
This comment has been hidden.
Great, that you solved it! :-)
Was a fun exercise, thanks.
I'm glad, you have had fun solving it. :-)
Python translation kumited.
Please review and approve :)
Thanks my friend. I approved it.
Danke schön :)
I wanted to approve your kata but gave me an error.
Thanks for trying my friend!
It's your last kata at the moment and it was your last day with less than 10k points... Seems too me last time we have the "congratulation days";-)?! So congrats to your 10k, good job (perhaps a little bit too quick):-)!
Thanks! ;-) I hope it is not the last time, both of us have congratulation days. ;-)
Surely not for you, next time is coming soon - if you have 10062 points:-)...