7 kyu
List Filtering
123,746 of 301,893cmgerber
Loading description...
Lists
Filtering
Data Structures
Fundamentals
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.
Groovy Translation
Where is my error ? :src/test/java/SolutionTests.java:43: error: incompatible types: List cannot be converted to List List result = Kata.filterList(List.copyOf(input)); ^ 1 error
you altered the return type of the method. it should return a
List<Object>
, not aList<Integer>
. the input list can contain potentially any type, therefore so can the output, expectString
sYou have a typo in the example namely in the output of the last number:
ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", "aasf", "1", "123", 123}) => {1, 2, 231}
There should be:
ListFilterer.GetIntegersFromList(new List<object>(){1, 2, "a", "b", "aasf", "1", "123", 123}) => {1, 2, 123}
This comment has been hidden.
Posting a spoiler solution is not a kata issue, it's a user issue.
so easy, so fun!
More Optimal
Not a kata suggestion
This comment has been hidden.
Hi, I replied to your other comment. I see your logical point, though. Would you like to consider re-writing the description? You could leave that posted with a suggestion flag.
the situation depends on the language. in C# and F# at least, the user's solution is required to return a list of
int
by the type signature, while e.g. Java returns a list of objects. there has likely been some translation drift over time, as this is a very old kataI thought that the Test examples asked to read the numbers from the string and remove repeated cases! I should pay more attention next time :/. But better examples with diferent numbers than the ones into the string would be awesome.
This comment has been hidden.
That was fun! I was also able to use foreach this time instead of plain old for loop, so happy to have this opportunity!
8 kyu
I'm getting the following error message even though my code is correct.
STDERR: /workspace/node/test.js:22
SyntaxError: Unexpected end of input at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1088:15) at Module._compile (node:internal/modules/cjs/loader:1123:27) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Module.load (node:internal/modules/cjs/loader:1037:32) at Module._load (node:internal/modules/cjs/loader:878:12) at ModuleWrap. (node:internal/modules/esm/translators:169:29) at ModuleJob.run (node:internal/modules/esm/module_job:193:25) at async Promise.all (index 0) at async ESMLoader.import (node:internal/modules/esm/loader:530:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15) at async formattedImport (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/nodejs/esm-utils.js:9:14) at async exports.requireOrImport (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/nodejs/esm-utils.js:42:28) at async exports.loadFilesAsync (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/nodejs/esm-utils.js:100:20) at async singleRun (/workspace/node/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/cli/run-helpers.js:125:3) at async exports.handler
Your code is not exactly correct, it is missing some closing braces.
This comment has been hidden.
The
Issue
label is for provable kata issues. Please use theQuestion
label when asking for help and code formatting when posting code. See https://docs.codewars.com/training/troubleshooting#post-discourse.Regarding your attempted solution, see https://docs.codewars.com/training/training-example#writing-a-solution.
TS: function name should use
camelCase
Agreed.
Hello! In te VS I'm getting good results, but here i'm getting this message: Fixed tests ReferenceError: filter_list is not defined at Context. (test.js:15:12) at process.processImmediate (node:internal/timers:471:21) Random tests ReferenceError: filter_list is not defined at Context. (test.js:37:14) at process.processImmediate (node:internal/timers:471:21)
maybe the problem is that you have to put your solution in a funtion with the name "filter_list".
You have right. When I try it in that way, everything was right. I didn't now that the name of the function is also important. Thank you for your answer!
This comment has been hidden.
This comment has been hidden.
Your code is wrong. Read the docs about how to post code in discourse, please.
non-negative integers is not the same as positive.
There is no confirmation of your words in the JS documentation, I ask for a link.
You will not find definitions of "positive" and "non-negative" in JS documentation, because these are not JS concepts, but mathematics. "Mathematically speaking" (at least w.r.t. what is, I believe, the most common interpretation), the set of non-negative numbers and positive numbers are not exactly the same, and they differ very slightly (by one element, to be precise).
I meant read these docs: https://docs.codewars.com/training/troubleshooting#post-discourse
As for the link you asked, try this one
Lua translation!
Approved
Hi, maybe you could add negative integers in the array, and also some corner cases like Infinity or NaN
Your suggestion goes against the current description, won't happen.
Error in examples in task description: "filterList(ListOf(1, 2, "a", "b", 0, 15)) == [1,0,15] filterList(ListOf(1, 2, "a", "b", "aasf", "1", "123", 231)) == [1,2,123]"
There are no these errors in the test tasks
That's in Kotlin, right? Please mention the language next time so it's easier to fix.
Yes, this is Kotlin.
I assumed that the description of the kata is common to all languages, but I learn that it is necessary to indicate the language
Fixed examples in the description.
Can smbd tell my why that error in my code class java.lang.Integer cannot be cast to class java.lang.Character (java.lang.Integer and java.lang.Character are in module java.base of loader 'bootstrap')
OP solved the kata
This comment has been hidden.
I passed my tests but the system still shows that I failed.
this isn't actionable information. there's probably also nothing wrong given that you say that you pass some tests and fail others, which is normal behaviour when your code is wrong. closing.
This comment has been hidden.
I didn't ask for more details because I didn't need it. Although it did not describe a problem sufficiently, it did make clear that there is nothing to fix with the kata.
The
issue
tag is specifically for tracking things that need to be changed in a kata. You usually won't get help with your actual problem when you tag somethingissue
because that would encourage using the tag for asking questions. Stating that the kata is wrong isn't a good approach to getting help!Maybe you meant that you personally had an issue, but that's not what the tag is for. I think the name of the tag is a bit unfortunate.
This comment has been hidden.
Hi, as a beginner and a new user who just signed up, I have the issue of my code producing the expected output in Pycharm, but not passing tests here. Why is that and is there some guidelines I have to adhere to? Thanks
But are you running the same test? Because if not then what you've got is: I didn't pass the test and I didn't try it locally.
Coffeescript fork
Adds random tests and changes function name to
camelCase
(backwards compatibility provided).This comment has been hidden.
Please don't post solutions in the discourse. If you want to discuss solutions, use the solutions page instead. Also, use appropriate markdown formatting when posting code. See https://docs.codewars.com/training/troubleshooting/#post-discourse.
Not a kata suggestion.
I just solve it and there is sooo much sht with zero which has typeof number but did not work as it shoould. It simply did not return it as a number. Also got problems with empty strings, because in my solution i have a validation for number. BUT empty strings somehow becomes a NUMBERS and returs back!! So much time spent for this just cos of.... I was really diappointed when the top solution is a (spoiler) method and it did not work for me!!!
I believe the instructions could be more clear to indicate that what is being passed into the function is an array, and not just some generic list. Which is perhaps most meaningful for JS developers.
What datatype would be a generic list in javascript?
An Array.
Then, what's to be written more clear in the description? Note that the description is shared amongst languages and in Python there are lists and arrays. I don't know if the original language was Python, but I think talking about lists in the description is not so wrong as you think.
Closing.
This comment has been hidden.
What am I mentally missing to break this down to where I can solve it?
This comment has been hidden.
Please don't post solutions in the discourse. See https://docs.codewars.com/training/troubleshooting#post-discourse
This comment has been hidden.
Please don't post solutions in the discourse.
A gentle reminder that the comment you directly replied to also says "Please don't post solutions in the discourse".
This comment has been hidden.
hello bro! filter is a higher order function and actually it doesn't work with if statements it just filter your code it's has own condition inside it. try to take a lesson about it, and try again.
So, this is the first time I'm hearing of List (I'm relatively new). I googled around to get a basic idea and managed to write code that passed the tests. But I'm curious to know more and play around with it. What are lists mainly used for and what are things I can try to do with them?
Read this for more info
I believe my code works- it prints out what it should and the results ae exactly the same as what they should be but it says its wrong?
You need to
return
the result rather than print it.OMG that's the first exercise I do here and I've been like 30 minutes looking at the screen thinking why it wasn't working. Thank you!
This comment has been hidden.
This comment has been hidden.
Your solution tries to modify the original list, while the original list should be left unchanged, and you should return a new list with values filtered out.
This comment has been hidden.
Read the error message:
Your function is returning a
List<object>
instead of aIEnumerable<int>
.This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I can not figure this out either. I had to create an empty list and add only integers from the original list in order to meet the requirements. I'm not sure how to simply just remove the strings from list. Curious to see!
This comment has been hidden.
you're removing elements if they're not strings rather than if they are strings.
Not a major issue, one of the test cases starts with 'Fot' instead of 'For'.
This comment has been hidden.
"1" is string, you need to filter by variable type. Here is a good doc about datatypes in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
Not a kata issue. And that looks like Python.
This comment has been hidden.
Please don't post solutions in Discourse, use Solutions section to discuss solutions.
Updated pending Scala translation to Scala 3
The previous two revisions should not be approved as they do not use top-level method definitions.
Approved by someone
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#expected-the-same and this: https://docs.codewars.com/training/troubleshooting/#post-discourse
This comment has been hidden.
i also want to know that too
(Python) Why is that outputs starting with "For input" and "Fot input" (the third value in the test asserts) get put out as the test result instead of what it should output, even if my function only contains return? Is it an issue with the tests?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Logs show that my code gives the right output, yet I get test failed :(
Read this: https://docs.codewars.com/training/troubleshooting/#.print-vs-return
This comment has been hidden.
isinstance
looks correct, but what is that ?This comment has been hidden.
Already told you not to post solutions in discourse, please, don't do it again.
This comment has been hidden.
list comprehension
hey mates, i wonder why isNaN is notworking??
It's working for what it is supposed to work. Read the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
This comment has been hidden.
Not a kata suggestion.
Rust translation
gooooooooooooddddd.
This comment has been hidden.
Iterating over a list and modifying it at the same time is strongly discouraged for leading possibly to unexpected results (it's probably the case here). It is considered a bad practice in programming (BTW, some modern languages like Rust make it just impossible). You'd better find another way to do it.
I mark your post with a spoiler flag since it gives hints to the solution. You should do the same whenever you post some code or elements of solution somewhere.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
It expects you to return a list of Integers though
This comment has been hidden.
What makes you think the order of elements is supposed to change? This has nothing to do with sorting.
You simply return the input without strings, like taking an eraser to the elements you want to filter out.
This comment has been hidden.
Please don't post your solution on
Discourse
page as other warriors can see your solution. This is not a question. If you want to see others' solution you can go to this section or simply head to aSolution
button in between theDetails
andForks
button.This comment has been hidden.
not a question
This comment has been hidden.
Not a kata suggestion, please don't post solutions in Discourse.
This comment has been hidden.
what's your code
good
The expected test is incorrect.
The tests are fine, you have a typo in your code.
Global Tests for Python are incorrect and do not match actual results.
No, your code is wrong, not a kata issue.
This comment has been hidden.
Please, don't post solutions in Discourse.
Where to post it?
You can ask in the discord group if you have a question. But what is the point in posting the solution here?
@Duysenbinov: I don't know if it's your actual question, but the solutions must be submitted in the Solutions page from the code trainer, and that's enough.
This comment has been hidden.
Your code not working is not a kata issue. Please use format tags to format your code, or your post is unusable. Read carefully the documentation: https://docs.codewars.com/training/troubleshooting/
Improved Python version use CW test framework, added random tests
.
Improved JavaScript version upgraded to Node v14, use
chai
for assertions, added random testsApproved by myst-6
This comment has been hidden.
.
This comment has been hidden.
Your program's issue is due to indexing. Let's say your initial list, l, is the following
When Python executes the 'for' loop line of code, it assigns the elements in the list the following indexes:
'hello' = 0; 'world' = 1; 1.25 = 2; 5 = 3; 20 = 4; 'hi' = 5
Since lists are a mutable, once you remove the first instance of a string from the the list using the 'remove' method, the indexes in the original list, l, shift:
'world' = 0; 1.25 = 1; 5 = 2; 20 = 3; 'hi' = 4
Because the 'for' loop has already iterated over the first index [0] by removing the first string 'hello', it would then move onto index 1 which is the element 1.25 in this example. It would be essentially passing over 'world' since 'world' has an index of 0, and index 0 was already addressed in the previous loop.
I'd recommend sticking with list aliases moving forward, so you don't run into this issue.
This comment has been hidden.
Test Results: Tests test expected undefined to deeply equal [ 1, 2 ] Completed in 3ms Completed in 6ms STDERR assertSimilar is deprecated, use assertDeepEquals
i couldn't solve this error
and i have problem with string having values greater than 0 like '5'
This comment has been hidden.
There are tabs before pass and other things
ok never mind, idk why this list should be in the funcion, but it's working
This comment has been hidden.
item
is a temporary reference to elements in the list and is tracked based on their location in memory which is arranged based on the positioning of elements in the list. So, if one item is removed, the items behind get moved forward and the loop will continue to iterate through elements after the last registered position despite there is a change in previous elementsThis comment has been hidden.
This comment has been hidden.
Java translation: https://www.codewars.com/kumite/5820615efd5794b27200001f?sel=61e3fd6baf28c2000d57c65a - thanks for a review and approval.
approved
in the 4th test result, I'm getting "[1] should equal []". How is that possible? Shouldn't positive integers stay?
See the last example in the kata description. That
1
in your case is not an integer.oh ok thanks
It is nowhere specified that the new list should contain only one occurence of each integer, yet the test doesn't allow it. Just saying.
Which language/test are you talking about?
Python. But it was my mistake. One of the tests was with a number as a string AND an integer, and I oversaw that detail.
example [1, 4, 23, "1", 123]
This comment has been hidden.
Please don't post solutions in Discourse.
ohk! I saw your badge which is 1kyu as you CAN see my badge is 6 kyu. can u please advise me that how are you able to solve these hard problems i am just scared of 6kyu problems just tell me best resources to get there...
Try some online free course first, https://www.freecodecamp.org/ is just one example. Then if you're still unsure about 6kyu katas, it doesn't matter your badge is 6kyu, do more 7 and 8 kyu until you feel confident enough and try 6 kyu katas again.
ok!
This comment has been hidden.
Your function isn't returning anything.
loved this kata
This comment has been hidden.
Because 'not pattern' matching was introduced in 9.0? https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#pattern-matching-enhancements
Thanks a lot, it's a pity that the latest version available here is 8.0... I wouldn't have thought about this...
good kata
This comment has been hidden.
The function doesn't need to print the list but to return it :-)
This comment has been hidden.
It's never a good idea mutating the array/list you're iterating through. You're skipping values like that.
This comment has been hidden.
Not a kata issue, read this: https://docs.codewars.com/training/troubleshooting/
Your code must return a list
This comment has been hidden.
You need to not only filter the list, but also return the result as a list of ints. You currently return a list of objects, and tests don;t like it.
Description for statically typed languages could use some clarification here.
How does this leave room for returning a new list with newly created elements?
Also, how does F# get this wrong ?!?
Fixed.
Hi, this is my first time doing this, can someone guide me how to code here? The question is confusing me.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Not sure what kind of help exactly you need, but please see if these links are any helpful:
Okay, one way to approach it is thinking, how would you convert a data type into another data type? How would you verify then if that is the correct type? Is there a way to check that before ?
This comment has been hidden.
This comment has been hidden.
Not a kata issue.
This comment has been hidden.
OP solved it, closing
Seems a trivial problem, but it taught me the difference between types and instances.
This comment has been hidden.
This comment has been hidden.
.
This comment has been hidden.
duplicate issue
I know for a fact my code works. Tested and completes all requirements but still wont pass the test on here. Doesn't make any sense.
Edit: apparently my globally scoped newArray was messing me up. I had to define the empty array within the function's highest scope so that it would reset with whatever samples it was given. Otherwise the previous values would be stored in the newArray. So yes my previous code did work, but it was not reusable for new inputs.
You should resolve/delete the thread after you figure out the source of the "issue".
I'm quite new to this, how do I do that? I cannot seem to find any options on the comment itself?
There should be a checkbox saying "Mark issue as resolved" when replying to a comment, although idk whether you can see it under your own threads without the comment-resolving privilege. Deleting a comment is possible as long as nobody has replied to it.
This comment has been hidden.
This comment has been hidden.
Not a suggestion. Don't post solutions on discourse, there's solutions tab for it.
This comment has been hidden.
This comment has been hidden.
Check how filter works.
Got it, thanks!
Good.
This comment has been hidden.
That's your problem. Don't print the result, but return it.
No random tests
Please specify which language(s) this applies to.
I think it was Python, but it has random tests now.
This comment has been hidden.
It's a reference to the same list, so it's not a copy. You should make sure it's a copy :P
thank you :D
This comment has been hidden.
???
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please mark your post as having spoiler content next time. What is your function returning?
Firstly I am sorry for my spoiler mistake.
My function returning correct in every example but codewars didn't approve it.
console.log
andreturn
aren't the same.Why the l.filter based solutions are ranked above, when for loop was faster ?
maybe because its nice to look at other's creative solutions rather something anyone could've easily came up with
Hi Inventer000,
In Short: efficiency of the code.
I guess the filter solution has a lower compexity (Big O)and this will require less resources when run on big datasets.
Loops will have Logarithmic Time Complexity: O(log n), Quadratic Time Complexity: O(n²), or worst case Exponential Time Complexity: O(2^n).
Quadratic is when you have a loop within a loop Every loop iterates through every instance of the dataset. This becomes a very slow program that requires many resources. So loops are not the best way to program when working with big datasets.
Exponential is probably when you have a recursive function. They look so clean but they are often not the most effecient.
Hope this is helpful?
This comment has been hidden.
looks like u solved it ~~
This comment has been hidden.
This comment has been hidden.
Okay... I found my mistake. I forget to using Linq... ""(>.<)""
This comment has been hidden.
This comment has been hidden.
When i use return statment directly like this: return typeof a == "number";(this works fine) But when I put it inside if statment like this: if(typeof(a)=='number'){ return a; } it does not return 0 .. please explain this
This comment has been hidden.
Because although 0 is a typeof integer but when you return 0 . It will not been include in the array as it is a false value in javascript. Basically javascript considered 0 as false. But in the same code if you write this
if(typeof(a)=='number'){ return true; }
It will include 0 too.
In the case of typeof a == "number" ,it statisfy the condition as it is a interger.
Hope it would be helpful to you.
This comment has been hidden.
The isinstance function is safer because it takes into account the class and subclass (direct, indirect or virtual) to check the object.
why should [1] be []?
I have the same problem...
Not sure how you both had this problem . . . as there is no such test like that in Python (I see you are both coding only in Python). If there were a test like this,
[1]
should NOT return[]
because it contains an integer it should return unchanged[1]
.They surely are misreading the error message:
[1] should equal []
where the first value is what they're returning, not the input.That makes total sense, but there is no test that expects
[]
, so...¯\_(ツ)_/¯
In Python, the penultimate test.
Indeed, I stand corrected. (facepalm)
You could just add an if to solve that problem c:
how can i return int without strings
This comment has been hidden.
This comment has been hidden.
although my func returns empty list on Jupyter my attempt always fail here? Anyone who managed to solve this issue? [1] should equal []
Not a kata issue, it's a problem with your code, read my answer below.
This comment has been hidden.
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Print the input, check why your code fails.
thank you for the answer now I have submitted but it needs to be said that the description of this task is awful
Why? It seems clear to me.
'1'
is not an integer. You're assuming the numbers should be unique in the returned list and the description says nothing about that.The kata starts with wrong comment. No # and wrong indentation, not 4 spaces.
my solution worked in pycharm, but not here
That's not very specific... What errors do you see?
This comment has been hidden.
return not print. return actually has a value.
You need to return the result, not print it.
gotcha ty
This comment has been hidden.
Same here ??
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Never mutate a list while iterating over it :)
That makes sense, thanks!
This comment has been hidden.
ez
This comment has been hidden.
Not a suggestion.
This comment has been hidden.
The problem is that your function's signature is
obj list -> obj list
. The code is actually expecting a function signatureobj list -> int list
One possible way of allowing for this is to update the line to be
| Int a | Bool a | Decimal a -> Some (unbox a)
inside of thechooseNotStrings
function.(you may also need to rethink the use of
TryParse
functions, as this will incorrectly include a integer represented as a string in the output, e.g. "1" will be included in your result, when it should not be as it is a string - it is irrelevant that you can actually parse "1" as an integer)This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
same issue as well
test.assert_equals(filter_list([1,2,'aasf','1','123',123]) - in this list one of the digits "1" - is a string, not a number. So it doesn't show in results ([1,2,123]) not because it is a duplicate, but because it's a string. So you should check in your code if you have any "string" numbers and exclude them too. You are welcome)
did your test pass for the list having only 1 string element? mine isn't goin. it's the fourth one when you go to attempt
This comment has been hidden.
This comment has been hidden.
i
is a temporary reference to elements in the list and is tracked based on their location in memory which is arranged based on the positioning of elements in the list. So, if one item is removed, the items behind get moved forward and the loop will continue to iterate through elements after the last registered position despite there is a change in previous elementsHow about this? +item === item
Regarding my previous comment, I am using Python.
This comment has been hidden.
Read this first. Print the input, see why your code fails (it'll fail in Pycharm too). Mutating the list you're iterating is a bad idea.
Thank you so much! I had looked something over in the range function in my for loop. I solved it!
This comment has been hidden.
Please, don't post solutions in Discourse.
Sorry! Noob here. Can I delete it? I dont see the option
Once it has a reply, you can't. No problem, just don't do it again.
I wrote a Golang translation . I would like to receive feedback from anyone, it's my first time doing it!
This comment has been hidden.
Because
isNaN('123') //false
(it treats'123'
as a number)!isNaN
isfalse
not a function.@Nameson Gaudel, check out this doc for details in using
.filter
passing in
isNaN
would be passing in a function. but the error message wasTypeError: false is not a function
because what's passed in is!isNaN
(which is boolean)@steffan153, even incase there is a logical error(apart from the syntax error)
Negative numbers need to be tested as per the description, or that should be removed completely. To not invalidate everything I opt for removing the note in the description.
Description seems fixed now!
No random tests in python, fixed tests are weak.
Fixed some time ago
This comment has been hidden.
I tested the code in vs code it worked with no issues but here it just failed
This comment has been hidden.
This comment has been hidden.
I tested the code in the IDE and it worked, but here it gives the message "syntaxError: unexpected token" :(
Post your code with spoiler flag.
This comment has been hidden.
Hi, you should know it's forbidden to post code like that. anyone can see it on their dashboard
I've prepared a Factor translation of this kata. Thank you for your consideration.
Approved by someone
This comment has been hidden.
Please, don't post your solutions in the kata discourse. It's forbidden.
This comment has been hidden.
Not a kata suggestion. Please, don't post code in Discourse unless it doesn't work and you need help. You can comment your own solution or another solution if you need it in Solutions directly.
Little hint for F# users, it took me a while to understand what's wrong: Don't forget about :?> operator ;)
This comment has been hidden.
Read how filter works and what are falsy values. The if is returning char, not true.
This comment has been hidden.
I get an "IndentationError: expected an indented block" error message on a for loop that is already indented consitently with the rest of the code. I made sure several times. Using Python.
Just something I noticed: I didn't read the instructions carefully and instead of filtering out strings I just filtered in integers, and it passed all the tests. Is this intended?
They are same.
This comment has been hidden.
If you have a problem with your code, please raise a question, not an issue.
Never Ever mutate an iterable while iterating over it.
because 'a' is a char
I can not do this
my solution works on test cases but it just tells me result shold not be [1, 2] , etc. whats going on here?
so my function passes the test cases but when i hit attempt it says 'should not return [1, 2] ' etc. please help?
It's hard to help you when you don't state the language nor we know your code. If it is Python, the default error message for
assert_equals
format isA should equal B
whereA
is what your function returned andB
the expected result.state your language, post your code with proper markdown formatting, tag the post as a spoiler, and then someone may be able to help you
This comment has been hidden.
This comment has been hidden.
My for loop won't run correctly (Using python). I have it go through each item in the list, and if it's a string, it removes it. For some reason it only removes one item and then doesn't continue the loop at all after that. I even tried copy-pasting the same exact code into PyCharm, and it works perfectly fine there.
18,794 solved in python ... not an issue! for loops don't remove strings. post your code with a spoiler tag and someone may guide you to a solution
edit: upon hearing it works perfectly in PyCharm, I'm wondering if you're printing the result. on CW you need to
return
the result. is that it? ;)This comment has been hidden.
This comment has been hidden.
Do you even know how
str.isdigit
works?There's no test case for negative numbers.
from the description : "a list of non-negative integers"
This comment has been hidden.
use the copy() method. so instead of x = l, say: x = l.copy()
return l.filter(item => typeof(item) == 'number' ? item : false);
Hi. Why filter doesn't return 0?
in [0,1,2,3,4] out [1,2,3,4]
Because 0 is a falsy value.
thx
This comment has been hidden.
Not a suggestion, and check the other solutions, maybe you were using a wrong condition for that method? What you did there (the if condition) would work just fine with it.
my bad Chrono79, I saw other people sharing solutions.
del
you dont compare no-negative numbers
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I don't think executes right. I don't know if I should declare the objects of the list at my instance or not. I haven't worked with Lists that much in C# so if anyone can give me any suggestions on how to improve the code, then please do
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
You skip values like that, mutating the array you're iterating through.
understood, thank you!!
This comment has been hidden.
This comment has been hidden.
Mark your post as having spoiler content next time. Mutating the array you're using in the loop is a bad idea, you'll skip values like that. Create a new one.
l[i]
is a temporary reference to elements in the list and is tracked based on their location in memory which is arranged based on the positioning of elements in the list. So, if one item is removed, the items behind get moved forward and the loop will continue to iterate through elements after the last registered position despite there is a change in previous elementsTest results keep fialing: (Python)
Test Passed [1, 0, 1, 5] should equal [1, 0, 15] [1, 2, 1, 1, 2, 3, 1, 2, 3] should equal [1, 2, 123] [1] should equal [] Test Passed
It's a problem with your code, you're casting the strings to numbers and also returning numbers as single digit ones.
This comment has been hidden.
Because that's not how you use typeof and mark your post as having spoiler content next time.
The test cases should include some negative numbers.
That would be against the kata description.
Could just be due to me being a noob, but I could see how "create a function that takes a list of non-negative integers and strings" could be interpreted as "negative integers are invalid input". I don't think the statement in the description unambiguously states that negative integers won't be used in the tests... so if a negative integer was provided and returned in the final list, the function would not be serving its stated purpose, no?
(Again, just one noob's thoughts!) :)
In that case, it should say
...and returns a new list with the strings and negative integers filtered out.
Or I would expect that.This comment has been hidden.
Read how type works and even when you fix that, you're overwriting p, not adding values to it. Mark your post as having spoiler content next time and check how to use proper markdown
This comment has been hidden.
No random tests for JS
Neither for Python nor Ruby (I suspect there are no random tests for any language).
Added some time agooo in JS
This comment has been hidden.
Why does everybody check only types? What about the non-negative constaint? There's no tests for that though.
def filter_list(l): for i in l: if type(i) == str: l.remove(i) return l
Why can't this work?
Quote: 'Thou Shalt Not Modify A List During Iteration' Maybe this will explain: https://unspecified.wordpress.com/2009/02/12/thou-shalt-not-modify-a-list-during-iteration/
maybe the problem is in the third line,it should be 'str', not str without '',or maybe you can replace the line for: if type(i) is str:
This comment has been hidden.
This comment has been hidden.
Scala translation ready for review.
Looking for a Scala reviewer
You can always come to discord to promote your translation ~~ https://discord.gg/mSwJWRvkHA
I have ported your translation to Scala 3. Scala Translation
looks fine to me
This comment has been hidden.
Have you tried to return an array?
Another easy kata after binary array to number one!
This comment has been hidden.
I think you're a little bit off with your statement... like 100% wrong.
This comment has been hidden.
Hi, I'm newbie, and I don't understand why my solution is considered a failiure. All examples are like that:
Log [1, 2] None should equal [1, 2] Log [1, 0, 15] None should equal [1, 0, 15]
and so on... I'll be very gratefull for any help.
Your function is printing the result (to the console log) instead of returning it. In the katas your function should always return the result, and you can use print, but for debugging process.
Thanks :)
Using Python 3.4.3, it doesn't allow for concatenation of sublists within given list, instead giving the following:
Or, your solution is simply wrong. Considering how you're trying to iterate over an integer, it definitely is...
Well, running the code in IDLE, for instance, shows no errors, so I'm not sure why it does so here.
In any case, is there a way to concatenate two sublists in a list without iterating over an integer? Or, is there a better solution that doesn't require doing so? I'm still a beginner so I'd appreciate the advice.
This comment has been hidden.
the function add() is used to add an object at the end of the list, so you result is the opposite of the solution, you should change the for parameters like this: for(int i = 0; i < listOfItems.Count; i++)
OP solved it, closing
why am i getting undefined; i used isNaN function;
function filter_list(l){ if(!isNaN(l)){ console.log(l); } can someone help me with this?
Your function doesn't return a value, so the default value for that kind of function is
undefined
.This comment has been hidden.
It is a pretty basic kata, if you don't know where to start, try doing some free course like the one on freecodecamp or another site. I can't tell you what to do without giving away the solution for this one.
i see where the correct array was logged, but I am still getting undefined...javascript
Is your function returning?
I get the following message when I run my code:
/home/codewarrior/index.js:23 }); ^
SyntaxError: Unexpected token ) at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.require (module.js:504:17) at require (internal/module.js:20:19) at [eval]:1:1
Is this a problem with the site? My code seems to be alright to me.
Check if your code has unclosed braces:
This error happens because the submitted code is concatenated and wrapped in boilerplate code before execution for historical reasons.
This comment has been hidden.
Not a kata issue, it's a problem with your code and some test, try printing
data
to see the input and then try your code in hereMine says that [0, 1, 15] should equal [1, 0, 15] but the Instructions never said you should change the order! (I'm using Python3 btw)
No need to sort the result. Integers are kept in their order in the input array. Check your code.
Not a kata issue.
[1, 0, 15]
is the expected, unsorted result.But if input is [0, 1, 15] then the output should also be [0, 1, 15] not [1, 0, 15]
Can't even run the sample tests! I'm using python
It works on Python (I have just tested it), your code is the likely culprit there, does it run in some other environment?
This comment has been hidden.
That code isn't ok, you shouldn't modify the list you're looping through (you'll skip values that way). That error message seems like a problem with CW servers, not a kata issue.
Thanks for a great kata!
Having problem in F# - solution which passes tests using type checking gives this error with the real attempt: /home/codewarrior/program.fsx(37,55): error FS0001: The type 'int' does not match the type 'Object'
I was able to get this to pass by specifiying the result of the of the function (the kata) as
int list
.i.e:
let GetIntegersFromList (listOfItems: Object list):int list =
F# is inferring that the result is an
Object list
so theGetIntegersFromList
function isObject list -> Object list
. My guess is that the tests are not casting correclty, resulting in the error messageerror FS0001: The type 'int' does not match the type 'Object'
when they loop/map over the results and try to compare values.Fixed.
Honestly I think all answers that contain filters for numbers are technically wrong. They could be passing in object, null, undefined, etc. and it would remove them. '!== 'string'' fits the actual question.
The tests don't have a case for non-negative integers being returned as described in the challenge instructions. Due to this missing test case codewarriors are able to pass the kata even though it's not truly fulfilled.
It's given that the integers are non-negative rather than required to be checked.
Regular expression's "match" not working in this editor
This comment has been hidden.
Please don't raise this as an issue, as it is a problem with your code, not the kata.
For your code, you have forgotten to define result.
This comment has been hidden.
Not an issue
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.