8 kyu
Invert values
57,393 of 178,246user7657844
Loading description...
Lists
Fundamentals
Arrays
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.
this site is not for beginners, how do I learn the methods if you don't even have a theory, I'm trying to replace "replace", I'm get an error a mistake + I don't understand what you said to change there? the whole point or something specific?
See if this paragraph explains anything.
You can also visit Codewars Discord for help.
This comment has been hidden.
This comment has been hidden.
This must be a wrong test case because 0 can't be -0
Test result: Invert array values Basic Tests expected [ +0 ] to deeply equal [ -0 ] Completed in 3ms
In computing, -0 is a valid value. See wikipedia: Signed Zero
Ah sure - Thanks for clarifing!
wow my frist try was easy i hope this code practice keeps being easy on my side
How has it been?
fyi random tests in Java are not checking anything. Please fix
Here is a fork addressing the issue.
Approved.
Well done. Thanks
Lua translation!
Approved
This comment has been hidden.
This comment has been hidden.
Your function gives too many outputs to the console. Try deleting the print statements and try again.
задача изи решил пока играл в бравлстарс соло шоудаун топ 2 занял и обосрался по этому 1 звезда
'You must not mutate the input list', but I didn't change the input list(
This comment has been hidden.
This comment has been hidden.
Pretty easy kata! Thanks!
As per the instructions the -ve values should be converted to +ve and +ve to -ve.But more than hundred test cases are written wrongly.
The tests are fine. Your code is wrong:
Not a kata issue. Next time, mention the language when reporting an issue.
This comment has been hidden.
This comment has been hidden.
Please:
See https://docs.codewars.com/training/troubleshooting/#post-discourse.
Nice kata!
fernanda es solo mia nice
nice fernanda bi
Hi, I am new to programing, and have problem to understand the concept of "mutate" the input array. I use new array for my code, and only "copy" content to new array, and work on new array. Still cant pass the tests. Can someone give me link to documentation/article about it?
As your code is right now you're creating a new reference to the same list, not creating a new list. You can see that printing
lst
at the beginning and before your return line.Thank you. Didn't know what I was doing - I thought that I was copying content of the list to new list. Now I created empty list and filled it with changed date from oryginal list.
thx for the help
This comment has been hidden.
we should be able to write code with our own data that works. I have big problems because I adapt to a given method as I write solutions in visual studio "in my own way" it works as it should. it's irritating
This comment has been hidden.
I agree that this solution should have passed the kata. I think it's silly that 0 and -0 are not considered the same (javascript).
reset the trainer and submit again ;)
how am i mutating the input list if im not changing anything in the input list?
You didn't make a copy of the original list, you've just created another reference to the same list, try printing
lst
before your return line and you'll see it.sincerely, thank you edit: just got it after reading your comment i didn't know that. i just made a new empty list and appended instead.
This comment has been hidden.
I fixed this in javascript, reset trainer and submit again.
If it was fixed, then I'd close the issue.
This comment has been hidden.
Random tests expected value is invalid.
Testing for [-60, 36, 85, -73, -19, 36, -79, -69, 88, 16, -17, 0, -88, -94, -18, 21, -89, 20, -10, 66, -45, -81, -32, -18, -28, 34, 41, 80, -95, 39, -39, -15, -53, 60, 70, 49, -19, -74, -44, 2, -16, -26, -63, -34, 97, -44, -56, -4, 81, 10, 0, 70, -91, -59, -84, 59, 53, -59, -5, 18, -64, 83, 37, 100, -82, 25, 3, 91] Expected: [ -60, 36, 85, -73, -19, 36, -79, -69, 88, 16, -17, 0, -88, -94, -18, 21, -89, 20, -10, 66, -45, -81, -32, -18, ... ] Actual: [ 60, -36, -85, 73, 19, -36, 79, 69, -88, -16, 17, 0, 88, 94, 18, -21, 89, -20, 10, -66, 45, 81, 32, 18, ... ] Which: at location [0] is <60> instead of <-60>
who can fix it?
In what language?
in dart language
This comment has been hidden.
Your solution fails because it modifies the input array. Tests for Dart have to be fixed to give better feedback in such situations, but you also need to fix your solution to not modify the original array.
I solve it! thank you.
I don't understand the "-" symbol to invert the number, I cannot find any information about it online other than the method reverse() in javascript. can someone tell me how it is called so i can look it up please?
It's basically just minus(or negative), you can use it to invert numbers, for example: -(5) is the invert of 5, which is -5. -(-9) is the invert of -9, which is 9. Hope you got it!
I think the description should tell you it requires you to use a new array instead of modifying the input array
Otherwise we have to guess at hidden requirments (not that I haven't had to do that in real life but it seems mean for a beginner level kata)
From description:
I can see that in teh JavaScript version - but not in the Go version I was workng on
You're right about that, I'll check it later, thanks.
P.S.: added the same message to Go description.
It's missing from Java too...
This comment has been hidden.
It's a generic failure message. It means that your solution did not return the expected value.
Using your example:
-6 should equal to 2
. Meaning that the solution you've written returns -6 as the final output, whereas the actual (correct) output is 2.funny
Could someone explain what does it mean the error "You must not mutate the input list"? I am a beginner in Python and, although my code inverts correctly the input list, my solution doesn't pass the random tests, probably because of this error that I have no idea how to solve.
Read the posts below, like this one: https://www.codewars.com/kata/5899dc03bc95b1bf1b0000ad/discuss#637b0f6d8ee59335c37afbe2
This comment has been hidden.
C++ code works fine, not an issue! Please post your code in spoiler using
question
tagThis comment has been hidden.
I did not know that negative zero existed (0-0)
All random tests passed, but i have a "You must not mutate the input list" error. what is the problem? solved with a loop
While your code loops through the input list
lst
it changes its values, which is not permitted. You need to create a new list and return that.anyway when im create a new list i get an error
This comment has been hidden.
Scala translation
Do not mutate the input array/list.
This condition is not checked in tests.
Fixed for Python
Invert 0 to a -0? Wtf? Man, that makes no sense. You should remove this. That only makes this kata more complicated for 8 kyu.
More complicated how? How would your current solution be simpler without that test?
This comment has been hidden.
Fixed for javascript.
This comment has been hidden.
This comment has been hidden.
thnx
Great one! Python work just fine
RISC-V translation Kumited - please accept :-D
Approved!
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting/#post-discourse
You're returning a different data type.
toIntArray(), i see. thanks
Go translation
Approved!
There is no test case for NonType error
Why should there be?
D translation
Approved!
I didn't know that 0 has an inverse, which is -0. What a stupid kata.
This sounds like you are using Javascript, in which case your anger should be towards the language, not the kata. Feel free to pick a better language to use ;)
Also, complaints are not issues.
I wouldn't say it was a language issue. An incorrect verification method was used. 0 and -0 should be deemed the same, regardless of language pecularities.
welcome to javascript
This comment has been hidden.
Java Translation (Author inactive)
Approved some time ago
This comment has been hidden.
This comment has been hidden.
There were test errors when trying to invert the value of the original array. Solved by creating a new array and entering the inverted values.
Expected: equal to [ -147, -104, -898, 438, -21, -302, 138, -880, 7, -530, 76, 929, -564, 506, 683, -427, -506, 693, -286, 178, 978, -643, 980, 635, 583, -456, 46, -239, 338, -176, 948, -32, 469, 904, -320, 851, 347, -668, -508, 202, -722, -24, 451, -233, -305, -753, 24, 935, 998, -445, -99, 629, -630, -236, 305, -384, -198, 455, -578, 246, -480, 316, -865, -196, -633, -268, 333, -660, 803, 265, 821, 966, 289, 590, -475, -82, -269, 445, -946, 236, -738, 930, -36, -182, 731, 423, 976, 417, 785, 20, -685, -528, 591, 191, -598, -753, -532, -49, -977, 767, 401, -492, 99, -420, 865, 256, -763, 908, -960, 483, -855, -322, 899, -486, -29, -852, -829, -409, 81, 589, -559, -268, 275, 296, -605, -874, -525, -16, -478, -363, -928, 116, 767, 331, 170, -907, 390, 865, -274, -64, 13, -640, -39, 7, -1000, -160, 411, 688, -962, -909, 232, 697, 607, -941, -702, -669, -13, -859, -334, -41, -315, 819, -555, 305, 610, -854, 611, 296, -417, -445, 98, -19, -242, 67, 464, 993, 587, -693, -536, -175, 118, 192, 465, 85, 139, -64, -974, 879, 275, 647, -978, -31, 441, -707, 361, 23, -901, 540, 628, 734, -405, -779, 159, -157, 278, -156, 512, -926, -802, -847, -531, -107, 404, 420, -614, 717, -837, 0, 385, 684, -375, -483, -154, 824, 372, 435, 448, -728, -704, -784, -109, -525, 9, 216, 199, 356, 333, 582, 890, -698, -84, -157, 807, 280, 133, 304, 513, 871, -335, -105, 105, -730, 119, -834, -892, 27, -338, 32, 492, -473, -742, -600, -948, 462, -667, 441, -1, 64, 796, -871, -775, 288, 950, 730, 811, 272, -542, 809, -120, -667, 26, -542, 230, 54, 148, -407, 192, -996, -794, 850, -42, 454, 205, -225, -959, 125, 30, -611, 942, -621, 783, -517, -163, 161, -457, -482, -913, -283, -477, 293, -384, 900, 908, -859, 450, -342, 340, 155, 328, 903, -544, -421, 116, -838, 692, -478, -893, 542, -820, 822, -57, 329, -73, 332, -227, 163, -167, 743, -597, -54, -603, -492, 616, 614, 446, 1000, 960, 621, 502, 625, 615, 266, -59, -530, -140, -933, 280, 477, -936, -663, -631, 617, 874, -221, -397, 542, 714, -961, -794, 277, -560, -545, -32, -779, 638, -854, -638, 341, -951, -782, 7, -182, -144, 876, -614, 428, -821, -583, -582, 355, 913, 426, -17, 274, 600, -695, -351, 573, 54, 575, -535, 802, 30, -431, -661, -246, 506, 557, -445, -958, 737, 928, 136, 569, 261, 829, -546, -597, 207, 982, -532, -713, -222, 624, -99, -694, -118, -584, -616, -501, 161, -510, -818, -419, 736, 922, 819, 307, 614, -547, -383, 885, -144, 964, -93, 679, -666, -954, 309, -919, -36, 693, 470, 755, -863, -324, 3, 942, 360, -924, 578, 54, -101, -796, -537, 923, -2, -349, -811, -504, -207, 277, 150, 940, -406, -624, -636, 744, -206, -962, 244, -248, 665, -84, 292, 422, 573, 466, -649, -414, 840, 222, -437, 902, -106, 203, -883, -877, -165, 158, -351, 296, 544, -41 ] Actual: [ -147, -104, -898, 438, -21, -302, 138, -880, 7, -530, 76, 929, -564, 506, 683, -427, -506, 693, -286, 178, 978, -643, 980, 635, 583, -456, 46, -239, 338, -176, 948, -32, 469, 904, -320, 851, 347, -668, -508, 202, -722, -24, 451, -233, -305, -753, 24, 935, 998, -445, -99, 629, -630, -236, 305, -384, -198, 455, -578, 246, -480, 316, -865, -196, -633, -268, 333, -660, 803, 265, 821, 966, 289, 590, -475, -82, -269, 445, -946, 236, -738, 930, -36, -182, 731, 423, 976, 417, 785, 20, -685, -528, 591, 191, -598, -753, -532, -49, -977, 767, 401, -492, 99, -420, 865, 256, -763, 908, -960, 483, -855, -322, 899, -486, -29, -852, -829, -409, 81, 589, -559, -268, 275, 296, -605, -874, -525, -16, -478, -363, -928, 116, 767, 331, 170, -907, 390, 865, -274, -64, 13, -640, -39, 7, -1000, -160, 411, 688, -962, -909, 232, 697, 607, -941, -702, -669, -13, -859, -334, -41, -315, 819, -555, 305, 610, -854, 611, 296, -417, -445, 98, -19, -242, 67, 464, 993, 587, -693, -536, -175, 118, 192, 465, 85, 139, -64, -974, 879, 275, 647, -978, -31, 441, -707, 361, 23, -901, 540, 628, 734, -405, -779, 159, -157, 278, -156, 512, -926, -802, -847, -531, -107, 404, 420, -614, 717, -837, 385, 684, -375, -483, -154, 824, 372, 435, 448, -728, -704, -784, -109, -525, 9, 216, 199, 356, 333, 582, 890, -698, -84, -157, 807, 280, 133, 304, 513, 871, -335, -105, 105, -730, 119, -834, -892, 27, -338, 32, 492, -473, -742, -600, -948, 462, -667, 441, -1, 64, 796, -871, -775, 288, 950, 730, 811, 272, -542, 809, -120, -667, 26, -542, 230, 54, 148, -407, 192, -996, -794, 850, -42, 454, 205, -225, -959, 125, 30, -611, 942, -621, 783, -517, -163, 161, -457, -482, -913, -283, -477, 293, -384, 900, 908, -859, 450, -342, 340, 155, 328, 903, -544, -421, 116, -838, 692, -478, -893, 542, -820, 822, -57, 329, -73, 332, -227, 163, -167, 743, -597, -54, -603, -492, 616, 614, 446, 1000, 960, 621, 502, 625, 615, 266, -59, -530, -140, -933, 280, 477, -936, -663, -631, 617, 874, -221, -397, 542, 714, -961, -794, 277, -560, -545, -32, -779, 638, -854, -638, 341, -951, -782, 7, -182, -144, 876, -614, 428, -821, -583, -582, 355, 913, 426, -17, 274, 600, -695, -351, 573, 54, 575, -535, 802, 30, -431, -661, -246, 506, 557, -445, -958, 737, 928, 136, 569, 261, 829, -546, -597, 207, 982, -532, -713, -222, 624, -99, -694, -118, -584, -616, -501, 161, -510, -818, -419, 736, 922, 819, 307, 614, -547, -383, 885, -144, 964, -93, 679, -666, -954, 309, -919, -36, 693, 470, 755, -863, -324, 3, 942, 360, -924, 578, 54, -101, -796, -537, 923, -2, -349, -811, -504, -207, 277, 150, 940, -406, -624, -636, 744, -206, -962, 244, -248, 665, -84, 292, 422, 573, 466, -649, -414, 840, 222, -437, 902, -106, 203, -883, -877, -165, 158, -351, 296, 544, -41 ]
can anyone help me? thanks
You are missing a zero in the actual result, check if you if-else branch left that condition unhandled.
I dont see it. Which index? Thank u.
It's index 217 in the original list. Look at the lengths of the two lists, the expected is 504, the actual is 503. The actual list is missing the zero.
Something in your code isn't handling zeroes correctly. It will help to run through the code by hand and think through what would happen if the input is zero.
missing empty array test in NASM
This comment has been hidden.
Numpy arrays apply '==' elementwise, so you get an array of truth values. This cannot be converted to a bool so cannot be directly used in an 'if' statement, which at some level the test will be doing.
Python lists compare equal if they have the same values for every element.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I see now. THANK YOU for taking the time and clairifying that for me Jim.
Forked typescript (fixed merge conflict and initial solution setup)
R translation (author long gone)
Thanks :)
This comment has been hidden.
Because the array itself can't be multiplied or added. You want to use the values inside the array to build another array to return.
Julia translation
[-1, -2, -3, -4, -5] should equal [-1, 2, -3, 4, -5]
ummmm? Should it, tho? Doesn't fit the brief of inverting the integers.
The first one is what your function returned, not the input value. Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Python new test framework should be added
I've forked the Rust translation in order to update it to Rust 1.49 and updated
rand::Rng::gen_range
to its new signature that takes astd::ops::Range
.As per the instructions the -ve values should be converted to +ve and +ve to -ve.But more than hundred test cases are written wrongly.Hence wrong Kata.
No, the tests are ok. And you didn't even mention the language you're trying so I guessed Python.
This comment has been hidden.
The random test set for dart is wrong..
According to the instruction, the result should be inverted... Such as from -51 to 51.
But in the test set, all solutions is expected to be equal, such as given array of [1,2,3] and expected outcome is also [1,2,3] which is wrong for all the 100 random test.
That sounds like you're mutating the input. Added the note for other languages to Dart too.
See previous comments.. Most of the people reported the same case, the basic test set result is okay, while the random test set is broken
And that's because they were mutating the input too. Fixed tests use a hardcoded result, random tests use vars that get modified if you indeed mutate them affecting the expected values.
are you seriouse? In which case should the result of an inverted ([1,2,3]) be also [1,2,3] as in the random test? Then what purpose is the function then? Should the expected result be negative (as in the basic test and in the isntruction) like [-1, -2, -3]
Don't mutate the input and your code will work with fixed and random values.
i see i got it now. So there is another test that check the input array to be the same.
Ok i got it then, and manage to complete it. But the test output should be clearer, to avoid any confusion.
No, there is not another test, see the tests for yourself clicking in
Show Kata Test Cases
Mutating the input leads to weird problems like this, and should be avoided when possible. I've created a kata about this, but it hasn't a Dart translation.
It says in description that input SHOULD be modified, so that's a faulty translation then. Most translations don't abide by this spec.
I don't see where it says the input SHOULD be modified, where do you see it?
Crkng M1nd!s Awesome motivational!
Random tests dont work!!! ((
This comment has been hidden.
You're testing with the index, not the array item ;-)
Also, next time please use Markdown formatting to post your code.
Also, you have a redundant if statement, but not the problem.
Dart and Factor translations
Racket translation
I would suggest giving an appropriate class name for the Java version, for example - class InvertValues. If you have a local collection of your own kata solutions (for example all solved kyu8), it comes into conflict with other code challenges where an author has declared the same name.
resolving (seehere)
This comment has been hidden.
Read this I see where your confusion comes from, in sample tests
assertDeepEquals
is used and in random testsassertSimilar
is used instead. Changed it so all tests useassertDeepEquals
.Testing with Javascript. Didnt alter the given array in any form, but random tests fail without apparent reason.
AFAIK Javascript tests are ok and aren't vulnerable to input mutation, so the problem must be in your code. Post it and mark your code as having spoiler content.
Just in case,
0
is neither positive nor negative.0 * (-1) = -0? Srsl? Tests wrong.
Not a kata issue, javascript has signed 0 and when comparing with assertSimilar you're comparing strings. Anyway, now it's been changed to assertDeepEquals and 0 and -0 are equal.
This comment has been hidden.
Kotlin Translation
Please, review and approve/comment
This comment has been hidden.
Because of that.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Ah! Duh! I see it now. Oh my gosh--thank you SO much. You rock.
This comment has been hidden.
Javascript has negative zero Not a kata issue.
I must disagree with you here Chrono: the kata states
Each positive becomes negatives, and the negatives become positives.
So-0
is a perfectly valid invert to0
(and vice versa) by the kata's description (since there is a positive zero and a negative zero).From the link I pasted there:
Zero is neither positive or negative (by that definition), so it should remain the same. Also from Wikipedia:
As I mentioned javascript has indeed negative zero (that's why he got that -0), but it shouldn't be used here.
This comment has been hidden.
Not an issue
random tests fails the exercise in python without an apparent reason, nowhere apart in comments its written that you need to return another array-list ... Which seems rendunant
Random tests are not working correctly. I have tested this in repl and it works just fine. When I submit, the basic tests all pass but every single random test fails. I'm certain it's not an issue with my code.
Read my comment below.
Issue with random test. Unable to validate this Kata !
Language? Read my comment below. Also when reporting an issue be more specific on what happens.
JS Node v6.11.0 and Python (both versions) All tests failed when attempt, while all basic tests are OK
It say for exemple : Expected: '[-92, 92, -76, ......instead got: '[92, -92, 76, ...
99.9% sure that's your case. If it is, mark the issue as resolved.
Ok thanks, initial array was mutated
This comment has been hidden.
Poor, poor
0
. ;)Im new at coding so I started with python and I just started using this site but I don't get how Im supposed to learn things on here cause it just gives me code to do but I don't know how to do it.
Welcome, not a kata issue, use the forum or gitter room to ask this kind of questions. Most katas won't teach you how to do things, they're more on the testing side.
Please do not forgot the key word "return" at the end of your func when using C language~
It's not necessary if you have a void function. Though, I'm having issues in this Kata.
Before saying the tests are bugged or broken, check you're not mutating the original array. If it still doesn't work, please tell which language you're using. Also read the comments already here, maybe you have the same mistake/problem.
You should have added this in the instructions ;-)
It's not my kata.
Given the author is gone, I've added a note in the instructions.
there is a problem with the Test. i try my code on the console and works fine.
The "random problems" are bugged for JS on Windows10 w/ Chrome. It looks like the random test solutions are the inverse of the inversed output, meaning...they're the same as the input!!
No, you're mutating the input, that's a side effect of your code.
Random tests still not working, though basic tests are ok.
This comment has been hidden.
The Elixir version passes the
numtest
variable into the function, when it should not.Fixed! Thank you! ;)
Even though I can now finally feel like a developer by proclaiming "It works on my machine!", it seems that there is an issue with the random tests of Python for this kata.
Don't modify the original array.
Thanks, that worked! Do I need to do this in any kata, or was it just a glitch within this one? I'm eager to know why this happened.
Mutating an array in a function is usually a bad practise because it modifies it permanently and you cannot revert the process. Mutating an array is good as long as it is a method of a class, for example: python has .sort() which is a method of the list class.
The Kata author has done:
What happens is that your solution ("invert" function) will modify the original array, and this modified array will be the parameter for the user's solution ("answer" function); therefore changing out the real expected solution.
I'm not sure if the Kata author did this accidently or purposely, but this "issue" is fine to be left because it stops people modifing the original array (deterring them to stop modifying the parameter). When making a Kata, I would usually do the same, however, I would inform the user not to mutate it in any way.
It can be "fixed" by:
Wow, thanks for this very clear explanation!
No problem :)
This comment has been hidden.
It appears that there is a consistent issue with this challenge.
All the basic tests are working, but the submission attempts have an error.
probably relates to specific language(s). js works, tried just now
Not an issue, already answered multiple times.
Python, error with random test
Don't modify the original array.
Java basic test broken
I don't know why the arrays have to be converted to string but the basic tests work fine.
In the Elixir version, it asks for Inveter.invert/1, the sample test calls Inverter.invert/2 and the final tests require Inverter.invert/1 :)
Fixed! Thank you! ;)
For python you need to save the result in a new list and return it to pass the random test.
Random test is broken, not your code.
+1 random test broken.
Broken for me, too
[C++ version] Description says "The input should be modified, not returned" yet the test cases checks the returned values, so... I basically have to return them anyway. Maybe I can't understand something or it just makes sense in some other language.
I fixed it. This is only true for the C version.
Elixir translation added for approval.
Inverter.invert(numtest, list)
has an extraneous argument in the sample tests.Thanks. I forked it and made the update there, since I couldn't just update it and save.
+1, error with random test
Random tests are broken.
Broken how? Which language? It's probably because you're modifying the original array instead of creating a new array and returning that instead. If so, don't modify the input array.
That worked. Thank you!
This comment has been hidden.
You're not meant to modify the original array. It's bad practise to modify it because it will change it permanently and you can't reverse the process. Here is an example (written in Python):
The Kata author has designed it in such a way that the array will be the parameter for the function of the expected results. This means that the modified array that you are returning will be the parameter instead of the original array. To curcumvate this problem you need to create another array and push it this one instead or you can duplicate the array inside the function e.g.
lst = l[:]
Also, next time can you post this as a question rather than an issue. Thanks!
Thanks for your help. :)
This comment has been hidden.
tmp = lst
is passing the same array to the variabletmp
, so in the end this mutates the original array (which messes up with the random tests).This comment has been hidden.
I couldn't get my Python work on the random test. Next comment is the part of the code, anybody can help? thanks.
IMHO too easy on Haskell, even for 8 kyu.
This comment has been hidden.
The answer to your question is yes...but seriously not really, just a few mistakes.
1.) You need to put the return statement outside the 'for' loop. Otherwise it will only loop once then return (only doing the first item in list). 2.) You need to invert the value by multiplying by -1, not 1. Multiplying by -1 gives the inverse, multiplying by 1 gives the number. 3.) You need to check that store[i] is not zero, if it is zero, you must leave it as it is, else you need to invert it (conditional to check if zero).
My code js is correct and i got error: TypeError: Cannot assign to read only property '0' of object '[object Array]'
May you post your code as a reply to this comment, so that I can help you? Put your code between two sets of three backticks to format it properly in Markdown, see here: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code.
This comment has been hidden.
1.) You need to take into account zeros. In JavaScript you can have -0 (for some reason), even though zero is neither positive or negative and therefore shouldn't have a sign. Your code ignores zeros so that it will invert a zero from "0" to "-0". You must make it so that it doesn't invert the zeroes (no negative sign).
2.) You're not meant to modify the original array. It's bad practise to modify it because it will change it permenantly and you can't reverse the process. You can either duplicate the array, see here: http://stackoverflow.com/questions/3978492/javascript-fastest-way-to-duplicate-an-array-slice-vs-for-loop or you can create a new array and append to that. Here is an example why you shouldn't modify the array:
Apologies for the delay, I didn't see your message.
Same here, for js random tests doesn't work. I'm 100% sure my solution is correct.
You're not meant to modify the original array. It's bad practise to modify it because it will change it permanently and you can't reverse the process. Here is an example (written in Python):
The Kata author has designed it in such a way that the array will be the parameter for the function of the expected results. This means that the modified array that you are returning will be the parameter instead of the original array. To curcumvate this problem you need to create another array and push it this one instead.
Thanks for the tip!
This comment has been hidden.
You're not meant to modify the original array. It's bad practise to modify it because it will change it permanently and you can't reverse the process. Here is an example (written in Python):
The Kata author has designed it in such a way that the array will be the parameter for the function of the expected results. This means that the modified array that you are returning will be the parameter instead of the original array. To curcumvate this problem you need to create another array and push it this one instead.
I think the problem is not that I'm changing the original array,the problem is that there is no mention of multidimensional arrays being passed as argument
Multidimensional arrays aren't passed as the parameter, the reason you're failing the tests is because you're modifying the original array.
This comment has been hidden.
Creating another variable doesn't duplicate/clone the array, it just changes the pointer names but still points at the array mentioned. The memory location stays the same, but is referenced by another name. I cannot prove this in JavaScript (since no memory locations), but this is proven by Python:
To curcumvate this problem, you have to create a new array (empty) and append (push) to this array or you can clone the array using .slice() - see here: http://stackoverflow.com/questions/3978492/javascript-fastest-way-to-duplicate-an-array-slice-vs-for-loop.
It worked. Thank you.
This comment has been hidden.
I changed the JavaScript compiler from Node v6.6.0 to Node v6.6.0/Babel. Now all tests pass!
or you could also have changed
n**2
ton*n
which will be right and one char shorter :stuck_out_tongue:Why does ** not work?
Neither Python nor js random tests seem to work for me when the algorithm seems good :(
This comment has been hidden.
You're not meant to modify the original array. It's bad practise to modify it because it will change it permenantly and you can't reverse the process. Here is an example:
Thanks, that helped! I think that the fact that you musn't modify given array should be mentioned in the description though, since technically my previous version did work. I'm inexperienced, like probably many people here, so if we're to learn, some obvious things should be said explicitly :)
In python, I'm passing all the basic tests, but the random test gives me an error. Even though my code works on all numbers I've tried.
I couldn't get it to work in either Python or JavaScript, screens in next comment because of spoiler policy
This comment has been hidden.
how does one change their profile picture?
Just link your GitHub account
thanks
This comment has been hidden.
You're not meant to modify the original array. It's bad practise to modify it because it will change it permenantly and you can't reverse the process. I do agree this is an issue because the Kata author has named the random test array the same as the input parameter name (which you are not meant to do). Here is an example:
This comment has been hidden.
You're not meant to modify the original array. It's bad practise to modify it because it will change it permenantly and you can't reverse the process.
C and C++ translations, enjoy :)
seems to be an issue with the tests, works on all test examples when run in seperate program.
In the java version of this kata the main function has int as an input instead of int[]. I would suggest that it gets changed as it is annoyingly easy to miss.
0 times -1 is still 0 not -0. Also all the cases work just fine when I rewrite into another program and test it. Seems bugged to me.
Welcome to standarization world where Math.random can return a negative number (-0) in some programming languages. This is because an int is represented using 32 bits.
CoffeeScript Translation kumited! Please Accept :D
This comment has been hidden.
On java it should be public static int[] invert(int[] array)
but it is public static int[] invert(int array)
Solved! Thank you! :)
This comment has been hidden.
input().split(",")
tolst
since that is the parameter of the function. There is no user input in testcases only variables in the form of function parameters.This comment has been hidden.
Currently Codewars platform is experiencing issues. Please wait warmly :(
https://github.com/Codewars/codewars.com/issues/893
Help please, the Basic test is executed correctly, and the random test is considered to be failed, although the arrays are inverted correctly in all cases.
You shouldn't mutate the original array because it's used for both your solution and the internal solution.
This comment has been hidden.
Just like Voile said, codewars have problems with itself :c.
Yeah, so please wait warmly while we're waiting for a fix D:
FYI: https://github.com/Codewars/codewars.com/issues/893
Thanks everybody!
I passed everything and it won't let me get credit for completing?
Currently Codewars platform is experiencing issues.
This comment has been hidden.
Don't mutate your original array, that's most often a bad practice.
This comment has been hidden.
I think there is an error in your code. Could you post your solution?
You mutated your original array instead of returning a new one.
Ruby and Crystal translations, enjoy :)
C# translation <a href=https://www.codewars.com/kumite/58e494adcc26fcd67f00003d?sel=58e494adcc26fcd67f00003d>here, check it please)
WTF? if I multiply 0 by negative the result ever goint to be 0, no -0
All numbers are floating-point in Javascript, zero must be signed and your code should handle this.
This comment has been hidden.
This comment has been hidden.
to create a new array, don't modify arr
thank you!!!! how did i not see that!!!! done it now!
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
PHP Translation Kumited - please accept :D
When I changed the list that was given as an input it failed the random tests but when I created a new list from the input list it worked. Maybe that deserves a mention?
If there is no mention that an input should be actively modified in the Description then it is naturally assumed that your function should be pure (i.e. should not modify the input passed in) as that is considered best practice.
A good lesson learned. Thanks!
I believe this is broken in JS. I can pass all the Randoms with values = 0 but fail the basic values. Or I can pass the 4 basic and fail all the Random.
I think your code does not work. The array length can be up to 100 in the random tests and if the length is greater than 5, your code calculates x-x (== 0).
I seperated the code to show how the first 4 "basic" pass with a certian math problem and the rest pass with a different math problem.
The fact that the "expected"(on the left side) all change to 0's and pass seems very wrong and confusing.
Sounds like I'm having issues because I am editing the array that was passed and not creating a new one. Maybe I'll find out why that is some day.
I took a screenshot. http://prntscr.com/e6e6yb but apparently lightshot isnt working right now. If you try my code as it is you can see how the "random" section passes with all 0's with the occasional test failing. Initally the first 4 would pass and all the random would be "inverted" still with the same code so i made 2 seperate operations with one only affecting the "basic" ones that only had a few numbers. I only had x-x ==0 to show that that is passing. I understand that that would not invert anything. To invert a number you must simply subtract it from its self 2 times, positive or negative numbers work this way.
It does not work if the random array length is 5. ;)
Do not modify the input array (see comments above).
You can break it by setting the input array to have length of 0, and this will pass all the random tests.
The random tests are broken.
Example is in my solution.
Kata currently expects 0 to become -0, but 0 cannot be negative. I think it requires a very subtle edit.
Thank you, it should be fixed now. ;)
Thanks, will approve
I have added a JS translation.