6 kyu
Array.diff
125,642 of 301,494marcinbunsch
Loading description...
Arrays
Fundamentals
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.
The description of the task doesn't mention that the lists are only comprised of numbers but the tests only check for numbers. Would be clearer if that was pointed out in the task.
Loved this Kata, great for teaching to use a stream
Popular kata just got even more popular Translation Dart
This comment has been hidden.
if you use a iteration to comprobe if b include element from a is better to do the iteration in descending order to dont have error
kata hint != kata suggestion
this kata seemed much easier than some of the 7kyu katas
ye definately it is not 6.. its too easy.
The bear effect
I am suprised nobody used a set to solve the problem. All the top one liner solutions have a complexity of O(m * n), which is inefficent for large inputs
Not a kata suggestion. A lot of katas have very biased upvotes, since the top-voted solutions are always at the top, they get upvoted constantly, but that does not mean they are "best practices".
This comment has been hidden.
Thank you! i used a map and the time complexity is O(N).
This comment has been hidden.
Not a kata suggestion. See https://docs.codewars.com/training/troubleshooting#post-discourse.
This comment has been hidden.
Not a kata issue, the code you think is the right one does a different thing to what the kata asks:
If
a
is empty, your code returnsb
.This comment has been hidden.
i am close to giving up at this point, filtered the 2 arrays to get the unique values for both and the returned the concatenated unique values as an array, i even passed the sample arrays and got the exact same answers, but it's still not accepting my answer
you are asked for this: https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement
you are doing this: https://en.wikipedia.org/wiki/Symmetric_difference
This comment has been hidden.
why i can't use a.ToList()?
Missing
using System.Linq
perhaps...im beginner, im not understand this tester want me return. is it need to return a string or array ?. the logic is okay but then still says not correct.
Just array. You are overthinking it.
This seems 8th kyu easy. Am.... I.... EVOLVING?!?!?
I do also believe that this is too easy for a 6 kyu, being more like a 7 kyu.
I made it hard for myself, i filter a[] and b[] for any unique numbers via two sets. (Maybe a new kata?) Who can read is clearly in the advantage :D TLDR: Read instructions properly and someone please make the test message less ambigous :)
The assertion messages display the inputs clearly, actual and expected results clearly, closing.
This comment has been hidden.
Not a kata suggestion. OP solved it, closing...
Javascript: Tests assert for
array_diff
, boilerplate names function asarrayDiff
, should bearray_diff
Both name (
arrayDiff
/array_diff
) is fine for the tests. Not a kata issue.Some extended test are wrong, please review them
Testing for arrayDiff([0,-12,20,1,20,-15,15,-8,-19,-19,1,2,3,17,-3,-5,19,-16,-18],[0,-12,20,1,20,-15,15,-8,-19,-19,1,2,3,17,-3]) Should work for random arrays too: expected [ +0, -5, 19, -16, -18 ] to deeply equal [ -5, 19, -16, -18 ]
0 it is in both arrays this shouldn't be in the array result, why are you expected 0?
Please use code formatting when posting code. See https://docs.codewars.com/training/troubleshooting#post-discourse.
Expected
x
to equaly
indicates that your function returnedx
when it should have returnedy
, not the other way around. Not a kata issue.Uhh... Is this really a yellow kata?
The solution is literally a one-liner.
learning this stuffs anyone who like to join me
This comment has been hidden.
JavaScript:
Not a kata issue: the
arrayDiff
function takes strictly 2 arrays as argumentsCorrect:
Incorrect:
This comment has been hidden.
Olá Luca, o seu código apresenta um problema ao assumir que a variável 'b' possui apenas um elemento. Na verdade, 'b' pode conter múltiplos valores. Consoante você tem no seu código, você acessa apenas o primeiro índice da variável 'b'.
This comment has been hidden.
It is forbidden to post solutions for others to see / use. Fortunately someone has already added the spoiler flag to the comment so it is not visible to those that have not solved the kata.
This comment has been hidden.
Not sure what your question is, but seems like you've solved it, so closing...
Found the instructions for this needlessly overcomplicated, I somehow bumbled my way to a solution in the end.
This comment has been hidden.
i solved it
test case a = [3, 4, 5] b= [1, 2, 3, 4]
In operators: if (a[i] != b[j]) { difference.add(a[i]); You output element a[i] as many times as it does not match the elements b. And it must be equal to 1 time, and then only if it does not coincide with any of the elements b.
This comment has been hidden.
This comment has been hidden.
Why remove the zeroes?
This comment has been hidden.
way too simple, but i guess the point was to get people to explore their 'clever' side
How can I resize 'a' array?
Hi! I can`t pass random test. I have got error: array lengths differed, expected.length=6 actual.length=81; arrays first differed at element [0]; expected:<18> but was:<0> In IDE everethings work good. How can i fix this?
OP solved it, closing
How is this kata a 6kuy? This is too easy.
use the array filter
This comment has been hidden.
This comment has been hidden.
Who knows? We're not mindreaders.
Not a valid kata issue. See https://docs.codewars.com/training/troubleshooting for tips.
This comment has been hidden.
You need to return your array
c
. The text "a was ..., b was ..." is just an assertion message for the test.This comment has been hidden.
Yes. If a test looks like this:
assert.deepEqual(arrayDiff([1,2,2], [2]), [1], "a was [1,2,2], b was [2]");
, your returned value should be[1]
.If your result is wrong, you'll get this message:
expected ABC to deeply equal XYZ
.Here, "ABC" is your result and "XYZ" is the correct solution.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I can't import "fmt" or "slices", error follows:
package slices is not in GOROOT (/usr/local/go/src/slices)
what do I do?
Documentation suggests that package
slices
is only available since version 1.21, but only 1.20 is available at the moment. So, don't import unsupported packages.This comment has been hidden.
This comment has been hidden.
I just needed to clear the list because... values are sent in a row...
This comment has been hidden.
You have to return the solution, not log it. Replace your "console.log(temp)" statements with "return temp".
Not a kata issue
my code pass on the test cases on my system but not here. what am i missing
maybe you're not running the tests that fail
You should consider All Possible States and your code should run without Error!
for JS test auto test is wrong - it curring randomly elements frrom result
your code is wrong.
This comment has been hidden.
"Good" is pretty subjective. It can be good or good enough in some cases, but might not be in others. It certainly isn't optimized and is not efficient when you feed it large arrays.
Since it iterates over the array once for every element, it needs to access the array n*n times (n being the length of the array). An efficient solution only iterates the entire array twice, so it would need to access the array 2*n times.
In Big O notation, the first solution is O(n^2) (quadratic), whereas a optimal solution runs in O(n) (linear).
This comment has been hidden.
A problem with your code is not a kata issue, use
Question
label next time and/or use Discord channel to ask for help. I don't know what you tried to do there, but it's very far from what the kata asks.This comment has been hidden.
Please don't:
Not a kata suggestion. See https://docs.codewars.com/training/troubleshooting#post-discourse.
Test passed but ... RandomTest Test Failed Expected is <System.Int32[2221]>, actual is <System.Int32[2218]> Values differ at index [206] Expected: 0 But was: 309 what is mean?
The error message shows at index 206, the expected value should be 2221 whilst your code returns 2218. Though, OP solved it, closing
This comment has been hidden.
[ 17, 6, 6, 7, 10 ]
is what you returned,[ 17, 6, 6, 7, 10, -7 ]
is what was expected: your code is not correct. please ask aquestion
to ask for help in the future; anissue
is a provable flaw/bug within the testsThis is not a 6kyu in ruby. maybe in other languages but not ruby.
Ranks are shared across languages.
This comment has been hidden.
This comment has been hidden.
Your logic is simply incorrect. Reread the instructions and debug your solution. See https://docs.codewars.com/training/troubleshooting/.
I know that something is wrong but I debug code on my computer and I get correct answer... Only in codewars test I have errors :/
your code works for me with this test: int aux1[] = {6, 9, 7, 0, 11}; int aux2[] = {-2, 7, 18, -1, 16, -19, 3, -18, -5};
which test from codewars is getting an error?
hmm that is the point, could I see which test I pass or not?
This comment has been hidden.
You can - simply print any value you're interested in to
stdout
. This is information that was given to you in previous messages. See https://docs.codewars.com/training/troubleshooting/ and https://docs.codewars.com/training/training-example#debugging-a-kata.I think I have problem with first test:
Assert.AreEqual(new int[] {2}, Kata.ArrayDiff(new int[] {1, 2}, new int[] {1}));
Not sure why this kata is at lvl 6, it's easy.
It's 10 years old :) Difficulty standards have increased since then.
Hello.
My program has done tests which was wrong
For example wrong test: Exptected: {6, 9, 11} Equal: {0, 6, 9, 11}
My test has done: Expect(ArrayDiff([]int{6, 9, 7, 0, 11}, []int{-2, 7, 18, -1, 16, -19, 3, -18, -5})).To(Equal([]int{6, 9, 11})), passed
Expect(ArrayDiff([]int{-18, 9, 13, 1, 6, 13, -5, 17, -4, -10, -10, -1, 3, 12, -4, 10, 0, -15}, {3, 19})).To(Equal([]int{6, 9, 11})), passed
Why?
thank you a lot. I ve done this kata
This comment has been hidden.
Don't waste your time thinking you should only use unmutable arrays in this task.
This comment has been hidden.
Why are you posting solution code in the discourse randomly?
This comment has been hidden.
No, that's not the reason your code fails, it fails with an infinite loop for this test:
Not a kata issue, it's a problem in your code. Please use
Question
label next time.thanks for the advice, im new to codewars
Man, here I am with my cute little for loop only to notice in #bestpractices that there's a method specifically for doing this that w3schools didn't tell me about...
By the by, it looks extremely specific, is there a way I will ever be able to recall this?
Default code configured improperly https://imgur.com/a/en1uwL2
Python. Hint: you can use a list to store the needed data.
Hello. While solving this kata, I ran into a problem, my code passed all the tests, but during random tests, the program gave an error. Here are the introductory lists: [-8, 5, -16, -9, -20, 14, 16, -4, 1, -20, 4, -7, 4, -14, 0, -15, -7, - 11], [-9, -2, 5, -11, -15, 2, 5, -3, 4, 5].
My answer is [-8, -16, -20, 14, -20, 16, -4, 1, -7, -14, 0, -7].
Correct answer: [-8, -16, -20, 14, 16, -4, 1, -20, -7, -14, 0, -7].
As you can see, in my answer, that in the correct answer the same set of numbers, but violated their order.
The question is, who can give a hint how to preserve the order of the numbers?
Can anyone help me with this kata?
`
The correct order is the same order that the elements appear in
array1
. So you should returnarray1
exactly as it is given to you, just with the elements that are also inarray2
removed.This comment has been hidden.
This comment has been hidden.
Please use the spoiler flag and appropriate formatting when posting code. See https://docs.codewars.com/training/troubleshooting/#post-discourse
I count at minimum four problems with your code. If you can't see a single one, that might be a sign that this kata is too hard for you. You should try easier katas (e.g. 8kyu) and comeback later to this one (users should be able to complete katas mostly by themselves).
The difficulty of this kata should be 8 kyu.
onGod
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/201.
Please join the discussion to help us identify duplicate kata and retire them.
Is returning new array correct? According to instructions we should remove values from a, not filter it and return new array.
On Codewars, if description does not mention anything related to array mutation (and only asks to return the result), such criteria are not tested except in specific language setups (ex: C)
This comment has been hidden.
This comment has been hidden.
really freaking sus for an account with 0 solved kata and no other posted comments to post hints out of nowhere
kata hint != kata suggestion
Sorry, my bad. I can't delete it now, marked it like spoiler.
So, I kinda solved it after many trials. Here are my mistakes: I was thinking of a situation where the length of b is greater than a. how do i return the difference bearing in mind that some elements are in a but not in b and vice versa. how do I merge the array and still maintain the order in this context the index.
but thats not the case in the problem. you just have to assume that a is greater in length and that all element in b will be found in a and then removed.
Nice!
Namespace 'List<>' could not be found. Array.Exists / Array.Except could not be found... The test is not working correctly (C#)
I have completed Array.diff task and i'm getting the desired output for 6 basic tests in the compiler and checked all the other relavent test in that task, but it still showing failed results. What should i do??? Help me
You can print the result to see which inputs cause the failure.
BTW, seems like you solved it, closing...
in many languages input arguments are normal and understandable. But for Typescript test and inputs are different and strange. why?
It's been raised an issue already. However, you can still refer to sample tests and adopt your code in Javascript to complete it ^^
This comment has been hidden.
This comment has been hidden.
I Should have just messed with the traits and mutability, nice though!
This comment has been hidden.
This comment has been hidden.
ah sorry, first timer. noted.
can anyone tell me?? how to do arrayDiff
This kata definition should add an additional restriction for not using built-in language functions ? for example for php there is a array_diff function. Idk if this restriction is more oriented to the spirit of the kata.
This comment has been hidden.
I think that all the language tools should be allowed
This suggestion cannot be implemented under the constraints of different programming language's paradigm. Also, the only thing you can restrict when it comes to lifting up the challenge to the next level is probably code length, which fits well in a code-golf task, but not here, so closing.
This comment has been hidden.
Do some free courses elsewhere until you're confident enough to solve some problem without having to look up how to write what you need, start by doing 8 and 7 kyu (the lower the number, the harder the kata) katas and only then go up in the ladder.
Make sure you read the CW Docs: https://docs.codewars.com/
I tried 8 if i'm right, lowest difficulty
I'm a bit lost on the website. I bought a book on python, I read the first half so far but I can't access my book until tomorrow so I guess I'm stuck for now... The objectives looked simple, like sort letters by alphabetic order or return the middle of a word but I can't do that (I hope I can add "yet"). Thank you, that does help me a lot
Also, read this to understand how solutions and tests are established in Codewars . Happy coding ^^
This comment has been hidden.
You're misreading the error messages. It says
expected <actual> to deeply equal <expected>
, where<actual>
is the value returned from your function and<expected>
is the value it should have returned instead.Your solution is incorrect - not a kata issue.
This comment has been hidden.
0
and-19
are present inb
, so they aren't included in the output.This comment has been hidden.
Hi, I don't know what's wrong with my code. I've passed the SampleTest but I can't pass the RandomTest, I don't know what's wrong. Anyway, should I drop my code in here?
Check out the official documentation on troubleshooting: https://docs.codewars.com/training/troubleshooting It has everything you need to know to make you almost working solutions actually work.
And no, you should not drop solution code in the discourse.
OP solved it, closing!
This comment has been hidden.
This comment has been hidden.
Basic tests passed! I found issue when check with all tests, but im my pc works correctly.
No, it doesn't, try in your pc with this test:
Please how do i solve the arrayDiff challenge
This comment has been hidden.
Never mind. got it
6 kyu seriously?
This comment has been hidden.
Hi everyone, I am new here. I'm facing some issues with tests. The code I wrote works in my pc for both basic and the random tests but when I try it here the result from my code is not the same as what I got in my pc. It there anything I should do to fix this?
Your code is not ok, why are you removing duplicated elements of the arrays? There are also another problems with your code, but start from there. Read this too: https://docs.codewars.com/training/troubleshooting
import
This comment has been hidden.
It's honestly baffling that your solution works at all. It's solely because the test cases are poorly written and your solution can oftentimes solve that specific case that's being generated over and over.
This comment has been hidden.
Interesting puzzle
In Javascript I copy solution that gives the expected result, it does not pass the test. Please check it if possible.
Your code is wrong, you're pushing an array several times into your result array.
This comment has been hidden.
OP solved it, closing
This comment has been hidden.
Hi! Possible some issue with your code. Attempt contains much more tests and they are different from the test.
This comment has been hidden.
This comment has been hidden.
If they don't want us using lists, IEnumberables or Linq just say so. Even adding the namespaces isn't working.
its perfectly fine to use lists, IEnumerable, and Linq in this kata.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
That's a problem with your code, not a kata issue, please read this: https://docs.codewars.com/training/troubleshooting
I'm sorry i put the wrong tag, this supposed to be a question
This comment has been hidden.
The prompt needs more examples.
This comment has been hidden.
That ^ Not a kata issue. Read this: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-7.0#examples
Oh damn, I feel so stupid haha. I'm using VisualStudio and I didn't need a using directive.
Thanks a bunch!
This comment has been hidden.
I wonder if the question prompt should specify that the lists are going to be sorted in non-descending order? I think this will make a difference to how one approaches the solutuion.
The order is the original one, you shouldn't reorder anything.
Right, but what is the form of the input? Is it always sorted or could it be [5,1,3], [6,3,2] or something?
The order of the input is random, and you should keep it as it is minus the elements that are present in both lists:
This comment has been hidden.
OP solved it, closing
OK this Kata was really great, for my current level of experience this pushed me to my limit. I spent all day working on it and nearly gave up but came through at the end. Although it completed around the 5 second mark I am still very happy!
Thanks!
This comment has been hidden.
My code is not passing the test. However, in the browser it passes. I can't see where the mistake is. Could you please help? Thank you!
hi,
i'm not very well-versed in js so i'll try to help with the logic of your code. it seems that your program is not handling the cases when:
a
e.g., whena=[1,2,2], b=[1]
your program only considers the first2
and not the secondOP solved it, closing
This comment has been hidden.
Your code is wrong, not a kata issue.
You shouldn't add values to a, just remove the ones that are in b too, and of course, you shouldn't sort anything if you should keep their order.
Thx! I had missunderstood the exercise.
This comment has been hidden.
This comment has been hidden.
thats odd, I'm not sure what happened, but I would suggest trying it using array methods instead of the loop...
hi i try to solve these with javascript but it gives me error: a was [1,2,2], b was [2]: expected [ 1, 2 ] to deeply equal [ 1 ] but when i check values console.log(a,b) it gives [1,2] [1]
Maybe you're confusing the logs. The message seems to match the input.
Yes, it's a problem with your code, it skips values so, it's not removing all 2s there.
This comment has been hidden.
list.remove only removes the first occurrence. Also, mutating
a
while iterating through it is a bad idea.BTW, don't forget the spoiler tag if you want to post code snippet.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
because if i try to set the length of the return string to z it gives me an memory error but if i try to use it as the size for my return string it does not work either
This comment has been hidden.
This comment has been hidden.
Hello, I'm using golang and having a problem with the random test.
Expected <[]int | len:8, cap:8>: [-20, -20, 2, 13, -6, -2, 7, 15] to equal <[]int | len:6, cap:8>: [-20, -20, 13, -6, 7, 15]
From that output, we can assume that a is equal to [-20, -20, 2, 13, -6, -2, 7, 15] and b is equal to [2, -2], right? But when I try it in my IDE with the same input, it generates output [-20, -20, 13, -6, 7, 15] which is the right output. Can you please help me to find my mistake?
The error contradicts the task. a was [1,2,3], b was [1,2]: expected [ 2, 3, 3 ] to deeply equal [ 3 ]
Where is the contradiction? The expected result is [3], your function returned [2,3,3]
You probably meant actual [2,3,3] and expected [3]
Are we supposed to code the most efficient solution, processing-time wise, or the most elegant from a syntax point of view?
Both.
This comment has been hidden.
Mark your post as having spoiler content next time, read the error message:
My JavaScript solution is returning the correct array when I test it in Chrome but is often returning undefined when testing in the kata. So I pass all of the initial tests in my browser but fail one of them in the kata. Same thing when I complete the full attempt. I get the correct result array in my browser, even for very long a and b inputs but for whatever reason, the kata thinks I'm returning undefined about half of time. Suggestions? Thanks.
Your code fails the last sample test because you're doing recursion wrong.
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting/#post-discourse
Don't use sets, or at least, not like that. You can see why, you lose information about how many of each number you have.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
If a is already empty, there is nothing to remove and the expected value is an empty array. Not a kata issue.
What you're thinking you should do is called Symmetric Difference: https://en.wikipedia.org/wiki/Symmetric_difference and it's not what is asked here.
hello, is it ok if i paste what i have so far? i think my logic is messed up but i have no clue as to why, it should be possible but the code isnt doing what id like it to.... im having trouble removing duplicates from array A
im using python 3
Your code does not follow this rule (latter) -->
It should remove all values from list a, which are present in list b keeping their order.
This comment has been hidden.
That happens when you mutate the array you're iterating through.
Thank you very much!😊
This comment has been hidden.
[1, 2, 4, 7, 8, -20, -18, -17, -16, -9, -4] should equal [2, 7, -18, -9, 8, -16, 1, 4, -17, -20, -4]
pls help
That's a random test, without the input too there is very little we can do. But it looks like you're messing the order.
in JS passed all 40 tests 1 fail a was [], b was [1,2]: expected [ 1, 2 ] to deeply equal [] console.log show [1,2] it's return array with [1,2]
even when i write (if a.length === 0) return [1,2]
it's doesnt work. Fix it please
Fix what? Your solution? That's not our job, but yours.
If
a
is empty, then so should be the result. Why are you returning something else?why does the system prompt not have the b.forEach function
In Rust test case
assert_eq!(array_diff(vec![], vec![1,2]), vec![]);
can't be executed Please fix itThere is no problem with this test. Your code is failing other tests before even reaching it.
7 or maybe 8
All tests in Python failed the same way:
a was [1,2], b was [1], expected [2]: 'a was [1, 2], b was [1], expected [2]' should equal [2] should equal is always the same with expected
What is a problem with output?
You should return a list, not the error message string.
Tests are saying "a was [1,2], b was [1], expected [2]", but not 2 and it confused me :)
In my C# solution all Sample Tests succeed but when trying to Attempt it comes alway back with a failure. Unfortunatly with not enough feedback to reproduce.
Test Failed Expected is <System.Int32[963]>, actual is <System.Int32[1042]> Values differ at index [963] Extra: < 142, 358, 735... >
What I did was create a second array omitting all of the zeroes.
nice
6kyu and its easier than 50% of 8kyu katas
How to scratch your head while the answer is right in front of you ;)
Lua translation fork! (this time I only updated the description to solve the merge conflict)
julia: how is it possible that I see ∉ in the solutions, despite that it is introduced only by 1.6 (i tested it w/ 1.0,1,6,1,7)?
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting#post-discourse
Sadly I can't help you, I don't know much C.
D translation
all tests are passed except the random array test why ?
Because your code is incorrect. Fixed tests don't cover all the possibilities.
a was [1,2,2], b was [1], received [] but expected [2,2]
This answer seems like the instructions aren't giving us enough information. Why if B has more than 1 of the number do you remove, but if it's in a, you don't remove? If you're doing a diff, it shouldn't matter which array you are doing the diff off of to get the result, which should be the same regardles of which is the starting sample array.
The instructions are clear about what to do, read them again. What you're thinking you should do is called Symmetric Difference: https://en.wikipedia.org/wiki/Symmetric_difference and it's not what is asked here.
Hola, estoy intentando hacer este ejercicio Array.diff, pero no entiendo porque si array b[] esta vacio me devuelve el array vacio, si pongo 0 si respeta la condicion. Que se podria hacer ?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
read the error callback again
you are trying to remove an element which is not present in the list
think of what you are doing when there are multiple occurences of an element in
b
but only one ina
This comment has been hidden.
Nasm translation
In Rust, the parameters are not mutable nor is copy implemented for type 'T'. I think the kata needs work, but it is quite possible I am missing something. Please let me know. Thanks!
Set up is always like this in Rust katas. Not an issue.
T doesn't implement Copy but Vec implements IntoIterator, which means you can call into_iter() and get T. (instead of &T that you get from iter() that you can't collect() )
I had to look at the solutions to notice usage of into_iter() and google the difference between it and iter() I was used to. Not an obvious thing if you're new to the language like I am.
Or just add
Copy
trait toT
...This comment has been hidden.
The problem was the user could modify the input. Fixed.
This comment has been hidden.
You are only removing the first element of
b
, you need to make sure none of the elements inb
are in your return value.This is too easy for a 6kyu level. Should be an 8kyu max.
~7kyu because of the basic functional concepts
'max' as if 8 isn't the easiest, probably a 7 since 8s are pretty beginner oriented
This comment has been hidden.
Fixed.
i used numpy it passed but there is a problem : 'use any or all ' how can i solve this ?
I would suggest not using numpy arrays. When you do a comparison on a numpy array, you get an array of booleans as a result. The tests are essentially testing
is what you returned (numpy array) == (expected result)
. If your numpy array is[2]
, and the expected result is a list that looks like:[2, 2]
, Then the result of that comparison is a numpy array of[True True]
. What the test expects to get back after the comparison is a simpleTrue
orFalse
. In your case, you need to return alist
and not a numpy array. The reason you get an error message of 'use any or all' is because one of the ways you can compare numpy arrays is something like(array1 == array2).all()
becausearray1 == array2
would return something like[True True]
, andall()
would check if every value wereTrue
and return aTrue
orFalse
.Oh thank you ! maybe i didn't read well teh Kata
Certain issues with JavaScript autotest. Without changing the code, the tests passed on the third attempt..
That is quite impossible. The initial code returns nothing, therefore
undefined
, expected answer in all tests is anArray
, and they can never be equal.Possibly you solved the kata earlier and the solution box remembered your own code.
If you mean your code is buggy and failed twice before passing randomly on the third attempt, please suggest a fixed test that would consistently fail your code - because it is imposible to always randomly test against an unknown bug.
Closing for now. If your problem persists, please reraise with sufficient information to reproduce it.
Go translation (author is inactive).
approved
This comment has been hidden.
The test is ok. The only number of a that's in b is -15.
Can anyonw tell me why my code only works when i have both for loops, and not just the first one (for item in a:)?
The problem is you are modifying
a
at the same time you are iterating on it. This is something you should avoid since it can lead to weird / unexpected results. One solution could be to iterate over a copy ofa
(while still modifyinga
) or to work with indices Look at the other submitted solutions to see other ideas of code working here.For example, if you add
print(item)
at the entry of you first loop, you will see that on fixed test #6 the2
is never parsed.I had not thought to use a copy of list. Thank you for such valuable advice.
a was [1,2,2], b was [1]: expected [] to deeply equal [ 2, 2 ]
Physically can't complete this kata because of this.
Your code not working is not an issue: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
I have the same problem and I dont understand it
This comment has been hidden.
Don't post solution in Discourse. The solutions need to be submitted in the trainer and then they appear in the Solutions page, that's enough. Also 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).
Ohh sorry, I am new to this platform. I Joined today hence i don't know much about the platform.By the way, thanks for the info.
I think there is a problem with the tests for JavaScript it is calling 'array_diff' but the function name is 'arrayDef'
No, there is no problem, the function's name was changed from snake_case to camelCase at some point and if there is some call to that function is for retrocompatibility.
@Chrono79 it is a problem, snake_case and camelCase is not the same, so that will affect the problem or test if the names are not same or defined.
It works with both, I already explained why.
Initial code:
Sample tests and attemp tests use camelCase.
If camelCase name doesn't exist, it looks for snake_case.
So, what's the problem?
Hello everyone (:
Why is always giving me the result
Submitted: 0 Expected: 1
being that in tests that I did separately it gave me correct ? If anyone has had this problem please help meThx for ur time ! :)
what language, what test?
quite hard to help if you don't give any more information
OP solved it, closing
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
You declared
uniqueArray
outside of the function, so every timearrayDiff
is called, you write in the same array and thus, your function will only work the first time you call it..
Is that error message saying it expected a '2' when the goal was to remove all occurences of b?
a was [1,2,2], b was [2]: expected [ 1, 2 ] to deeply equal [ 1 ]
Why?
Thanks! I skipped an index when removing. For some reason it worked for all other 40 test cases before too.
This comment has been hidden.
Your code has a couple problems. The first one is that
remove
only removes the first matching element from the list. The other is that you're iterating througha
and you're removing elements from it at the same time, which causes your code to skip over elements. I see you tried to avoid this at the beginning of your code, but you haven't avoided it successfully.Also, "Issue" is meant for bugs with the kata itself. If you want help with your code, you should mark these kinds of posts as "question" in the future.
Not a kata issue.
I liked this one
log:[2] error:a was [1,2], b was [1], expected [2]: None should equal [2] why?
I forgot the ‘return’ in the function.
This is a good kata but i don't think it should be rated as 6 kyu maybe 7.
There's some sort of issue with it parsing the answers improperly in javascript. The output returns arrays like this [ 3 ] and it wants it to look like this [3] so it thinks the space is an error. Unless I'm missing something?
I faced the same problem.Have you solved that?
Did you get a 'return' in your function?
This comment has been hidden.
I did the same thing
This comment has been hidden.
passed 40 test unless this one a was [], b was [1,2]: expected [ 1, 2 ] to deeply equal []
is this really worth 6kyu in python? its just one line in py
brevity is not the mark of difficulty. That's why the words are different.
It was really hard 3 hours X-)
COBOL translation (author inactive).
approved
This comment has been hidden.
Sure, read this: https://docs.codewars.com/training/troubleshooting
Thank you! I solved it!
This comment has been hidden.
This comment has been hidden.
Description says: "It should remove all values from list
a
, which are present in listb
" . You removed only one2
from lista
, and you should remove all2
's.It's not a kata issue, your solution is incorrect.
This comment has been hidden.
Which language are you using? If it's Python, make sure you return a list, seems you return a set.
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting#expected-the-same and your code should only return
[2]
there, all the other text is part of the tests.This comment has been hidden.
Hello Henfox, I am new here, I think the idea is to create a new list with the difference, if you run that code what it does is to modify the 'a' list
Your code works but according to the practice it is not what they are asking for.
My suggestion is that "for" can skip some occurencies in "a" due to changes in indices of the elements of "a". And "for" goes through "a" by index (not sure here). For example. a=[1, 2, 2, 3], b=[2] "for" goes through "a" taking item by it's index. Length is 4 so it should pick 1st, 2nd, 3rd and 4th item. 2nd step: a[2] is "2", it is in "b", "a" changes to [1, 2, 3] 3rd step: a[3] is "3", it isn't in "b", "a" remains [1, 2, 3] 4th step: there's no a[4] so loop ends.
This code could be refractored into one two lines of code, with
list comprehension
It's damn easy doing it with python. This kata is nice!
Good introduction !
nice kata,I spent about 3 hours working on it
yup, took me 2hrs, but I was working with a mentor
it was kind of my first Kata, and first program in Java (not counting Hello World!) :D
This comment has been hidden.
The correct result for
arr1 = [1, 2, 2]
andarr2 = [1]
is[2, 2]
; your solution is wrong.Oh, I assumed the repeat answer was not good. Thx for the response The fix worked, you a real one
This comment has been hidden.
Even copy of solutions cant pass the test. Nice kata 10/10.
array lengths differed, expected.length=2 actual.length=1
Please state which language, which solutions?
This comment has been hidden.
That one worked just fine for me, in both Java versions, 8 and 11.
I am sorry, may be I don't understand something, kind of newbie, but I can't go through tests.
Just to be sure, click
Reset
and try again. Print the input too to check which test your code is failing.Yup, after reset it works. But i have no idea, how it could give this error, and just stop after reset. Thank you. May be my decision could also work lol. Should i delete this messages somwhow?
i have exeption System.IndexOutOfRangeException : Index was outside the bounds of the array. when i try to attemp. But all standart tests is passed
who can help with samples of tests that can help solve this problem
here tests that passed
Assert.AreEqual(new int[] {2}, Kata.ArrayDiff(new int[] {1, 2}, new int[] {1})); Assert.AreEqual(new int[] {2, 2}, Kata.ArrayDiff(new int[] {1, 2, 2}, new int[] {1})); Assert.AreEqual(new int[] {1}, Kata.ArrayDiff(new int[] {1, 2, 2}, new int[] {2})); Assert.AreEqual(new int[] {1, 2, 2}, Kata.ArrayDiff(new int[] {1, 2, 2}, new int[] {})); Assert.AreEqual(new int[] {}, Kata.ArrayDiff(new int[] {}, new int[] {1, 2})); Assert.AreEqual(new int[] {3}, Kata.ArrayDiff(new int[] {1, 2, 3}, new int[] {1, 2})); Assert.AreEqual(new int[] {26, 3, 3},Kata.ArrayDiff(new int [] {13, 2, 26, 13, 2, 3, 3}, new int[] {2, 13})); Assert.AreEqual(new int[] {}, Kata.ArrayDiff(new int [] {-20, 5, 5, 5, 5, 5, -11, 42, 0, 55, 0, 0, 1}, new int[] {-20, 5, -11, 42, 55, 0, 1}));
Actually when i do ATTEMPT, everytime it runs random test cases and i understand that if my return length is bad then free corresponding to malloc will give runtime error. Bit i want to check which test case failed, to correct my code. Initial 7 test cases pass for me.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#print-input
Description has very few test cases. That why I failed for a long time. The test below helped me to see my mistakes and solve this kata:
assertArrayEquals(new int[] {26, 3, 3}, Kata.arrayDiff(new int [] {13, 2, 26, 13, 2, 3, 3}, new int[] {2, 13}));
Thank you fellow Human!
This comment has been hidden.
Same. I do not understand the description. Looks interesting. Poor prompt though.
it's C, and I have the exact same problem...
Go, same thing
should be fixed (made description language agnostic in the meantime
I am getting this error on Test.
array lengths differed, expected.length=1 actual.length=0
results are OK on IDE. What could be the reason?
this suggests that you carried out the same test and it had a different result but I find that it more often means that this wasn't the same test carried out, or the result wasn't measured, and that's why nothing wrong was seen.
suggestion: start printing out information from your function to find out what happened. maybe print out what the input was and use that in your local environment.
This is a good kata for practicing fundamentals of a programming language and to start thinking how a programmer
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting
a is an empty list, your code returns b.
Except basic test all other tests passes for me. please review tests.
Should pass Basic tests
a was [1,2], b was [1]: expected [] to deeply equal [ 2 ]
Random tests - 40 of them passed Testing for arrayDiff([-13,-13,14,6,-9,-15,3,-16,-1,-11,8,4,-4,-11,-6,-10,-2,-10,-12,-10],[-13,-11,-13,-16,6,-9,14,-6,-10,3,-11,-15,-1,-10,-10,-2,-12]) Testing for arrayDiff([-6,0,-11,-5,7,4,-1,-1,-5,17,12,-19,2,-15,-19,-14,-16,-2,-16],[17,-11,-14,-16,-6,-19,-1,-15,-5,-2,-19,-1,0,4]) Testing for arrayDiff([5,-6],[]) Testing for arrayDiff([-16,-18,-15,-13,18,8,11,-16,14,-11,-8,-6,-4,-7,-16,1,-5,-4,-8,-19],[14,-4,-4,-8,-7,-15,-18,-8,1,18,11,-16]) Testing for arrayDiff([-17,-1],[-17]) Testing for arrayDiff([9,16,-10,12,4,-17,8,-20,14,-7,-3,7,-9,-11,-15,5,-16,-2,8],[-10,-20,8,-16,-9]) Testing for arrayDiff([8,12,2],[2]) Testing for arrayDiff([-8,-15,-12],[-15,-8])
The tests are fine:
And you can see the expected value for that input is ok.
Please chek this basic test as it says that all accourences of b should be deleted, but test expected output don't match this condition.
"a was [1,2,2], b was [2]: expected [ 1, 2 ] to deeply equal [ 1 ]"
It does, read the error message properly. Not an issue.
Ignore
Too easy for 6 kyu
TypeScript Translation.
Approved