8 kyu
A Needle in the Haystack
76,238 of 181,399andrewMacmurray
Loading description...
Arrays
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.
This comment has been hidden.
You need to
return
your answer, notprint
it.Mark your post as having spoiler content next time, please. Read this, it'll help you get started: https://docs.codewars.com/training/troubleshooting
will do, sorry
hope u can do it other way without using find or index function by ordering the list by order a,b,c,d,e,f n use for loop for constant result
there is a problem with the first test, it told me this:
Time: 445ms Passed: 3Failed: 1Exit Code: 1 Test Results: Fixed Tests Basic Test Cases 'found the needle at position 3' should equal 'v 3' Test Passed Test Passed Test Passed Completed in 0.05ms Completed in 0.10ms
checkout --->should equal 'v 3'<---, only running it by "attemp" worked
my guess would be that you edited the test code
the random tests were just numbers. now strings, numbers, booleans fork python
NOTE: DO NOT DO THE TEST!!!! It's horrible
That's not a proper kata issue. Closing.
If you want to say that the kata needs to be fixed, you have to say what is wrong with it, and in what language, and what should be changed.
This comment has been hidden.
(🤖)
Shouldn't random tests be random and contain different types of data than just numbers?
random tests consist entirely of numbers for these languages python coffescript, javascript, and other languages
why is it an issue ?
I didn't know which option to choose for this.
Looks like I picked the wrong category
make Translation Dart
You should put the result like a string, if you put like a print, you gonna take error. Then the return should a complete string, until the index.
I really enjoyed this one (because I didn't struggle with it at all)
The key of this problem is how you return your output,if you return a string with the sentense "found the needle at position" plus the posicion of needle as a int you are gonna get the same error.you need to turn the int to string and you are good
What is the problem? Everything works fine on the PC
Read this: https://docs.codewars.com/training/troubleshooting
I dont think this is a 8 kata level question, should be changed to a more appropriate level.
I agree, this needs to be listed as 9 Kata
please change the name of the function in the tests as it does not match the description. I had to change it manually
Решение через генераторы работает отлично, но не принимается
Closing as per not enough details to address the issue.
This comment has been hidden.
This code is wrong: it returns a list, but it should return a single string.
This comment has been deleted.
OP solved it in a ugly way... closing!
There's no needle passed to the function - only a haystack.
correct
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting#expected-the-same
This comment has been hidden.
Please use appropriate markdown formatting when posting code. See https://docs.codewars.com/training/troubleshooting#post-discourse.
No, your function doesn't return the correct answer. See https://docs.codewars.com/training/troubleshooting#.print-vs-return and https://docs.codewars.com/training/training-example#writing-a-solution.
This comment has been hidden.
I don't understand what the problem is. The function accepts array called
haystack
, isnt this clear enough?in this kata the needle is a string constant,
"needle"
. this is why the function only takes one parameter, the array in which to search.I keep getting this error Basic Test Cases ('found the needle at position', 3) should equal 'found the needle at position 3' ('found the needle at position', 5) should equal 'found the needle at position 5' ('found the needle at position', 30) should equal 'found the needle at position 30'
no where in my code is it supposed to return the "," and adding the varible with a + just causes errors
From what you said it seems like you're returning the string 'found the needle at position' and the index separately, which effectively returns a tuple (or whatever that data structure is called in the language you did this in).
You need to add the index at the end of the string.
Almost all languages have some builtin function that converts numbers to strings. Try looking for it in your language.
In short, you returned the wrong data structure.
This comment has been hidden.
can you provide the code
Sounds like an infinite loop.
Sure this traning is non-basic level? I no way to start too hard
Some languages (like Python) have a builtin function that finds the needle for you, and even in other languages you can rather easily iterate through the array to find the needle' position.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#expected-the-same
This comment has been hidden.
Please, don't post solutions in Discourse.
why?
Because it's not the place to do it. There is a solutions section where you can see all solutions. From the docs:
Read them here: https://docs.codewars.com/training/troubleshooting#post-discourse
I didn't understand this testcase. can anyone explain?
'Test.assertNotEquals(findNeedle(haystack_1), undefined, "Your function didn't return anything");'
It's all in the error. Logging isn't enough. Your function didn't return anything. In JavaScript, the absence of something is
undefined
. Since your function didn't return anything, it implicitly returnedundefined
, which was caught and penalized by this test case.This comment has been hidden.
your code returns a
tuple
containing astring
and aninteger
, whereas it needs to return astring
This comment has been hidden.
You have a typo in your string: "posiotion".
Your code failing is not a kata issue.
ohh, my mistake, sorry
The description confusingly implies the function has to be called findNeedle(). But it actually doesn't.
I understand your frustration, but the function is still technically called "find needle". It is just that different languages have different naming conventions. This Kata isn't just for Python - you can do it in many languages. In most of them, functions/methods are named in
camelCase
; but when you see the function insnake_case
instead, don't be confused. Ideally, the description usually gets changed to include these different naming conventions once the Kata gets translated to other languages, but that's not always the case.Hello, I keep running into this problem (expected 'found the needle at position 5' to equal 'found the needle at position 3') Im unsure where I went wrong. According to my code this would be the correct answer. Any guidance?
Your function needs to accept an argument, which you then work with.
It should look like this:
Currently, your function just does the same thing over and over again and is ignoring the input. You need to use the argument instead of your
position
array.Awesome man thanks for your help.
This comment has been hidden.
C++ Translation.
Approved
Thought I was having a tough time figuring this one out, turns out I just spelled position wrong and made more work for myself for no reason. This is a lesson to everyone to REALLY check spelling on your answers, but also that even if you make a mistake like I did that it will help you problem solve because I "solved" this multiple different ways until I realized so in the long run it taught me more ways to solve problems in the future :)
I'm struggling to do this in C any advice
Sorry,I completed this in javascript so I won't be much help for C. I will say don't be afraid to search up ways to accomplish things and try different techniques. I don't wanna spoil too much for you, but if you get STUCK like have worked on it for a while use the solution button and go through and try to understand what each piece of the solution is doing so you will have more tools to use for future questions! Wish I could be of more help,but good luck. You've got this!
This comment has been hidden.
No "undefined" validation needed but I get this error message:
"Log found the needle at position 3 Your function didn't return anything: expected undefined to not equal undefined"
Read this: https://docs.codewars.com/training/troubleshooting#expected-the-same
This comment has been hidden.
This comment has been hidden.
Please see this paragraph.
ty, the print was the problem
IMHO This Kata should fix the tests or fix the Kata itself. It is not a good practice to use global variables especially when you are new to programming.
What global variable? I can't see global variables anywhere?
can't pass the tests, although i get the correct and exact same output, wtf...
Did you read the error message? Your function's name should be
find_needle
notfindNeedle
.ah true, thanks man!
This comment has been hidden.
Please:
See https://docs.codewars.com/training/troubleshooting/#post-discourse.
You're printing your output, instead of returning it. See https://docs.codewars.com/training/troubleshooting/#.print-vs-return.
Seeing you're new to codewars, I highly recommend reading this article.
So, I get the correct output and yet it tells me I am wrong. Curious.
No, because you're printing it instead of returning it. Read this: https://docs.codewars.com/training/troubleshooting#expected-the-same
yes, I was just about to correct myself! Thanks though
I put in the correct answer and got a fail message every time. I gave the right answer and it is so frustrating.
Does size_t count variable provided as the length of the array?
yes
This comment has been hidden.
Wdym it's not correct? The needle is on index 5. You returned blank string.
That was a mistake, as I thought we were supposed to put the actual position (index+1), and not the actual index. Secondly, I didn't return a blank string. The code works when it is outside kata, but here, it doesn't.
Whatever the case, you need to return this string, not print it to console.
Anyway, this is not an issue with the kata, but a problem with your code. Please use "question" label next time for similar problems.
This comment has been hidden.
try refreshing the page or relaunching your browser, usually it's a cache issue
Thanks
The Haskell one has a pretty sus test case. Why is there
Num
instance forString
? Just write the quotes already.Error: "not all code paths return a value". My code works fine in another compiler with same sample tests. What does it mean?
Ok this is strange: in C i have created a global buffer that will contain the return string and that is able to live through the various tests. All sample_tests are ok but the random_tests are not. Am i missing something?
OP solved the kata + problem not reproducible with his solution
Somehow a bit stupid... In the condition it is necessary to indicate that we are looking for the index of the element and not its position!!!
It's in the kata description.
This comment has been hidden.
This comment has been hidden.
I can't see your comment
This comment has been hidden.
Thank you. I did a two solution for it which the first one is just literally looks for the string "needle" and then the other one is to apply my first approach which is to convert the values inside of the array to string which is unnecessary but yea just to make it possible with case insensitive haha.
This comment has been hidden.
hello , i have one problem.The kata count spaces in youre RETURN!some example: need "aaa" if you resolt " aaa" it's incorect!
Those two strings are different. So failing when comparing both is the expected behaviour.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
There is a null in one of the arrays in the test. You have to handle it.
'found the neddle at position 30' should equal 'found the needle at position 30' I dont know what is the problem they seems same to me
Hi @Murathancakir and welcome to Codewars!
I tried debugging your solution, because the code looked OK to me - it turns out it's a spelling rather than a programming mistake!
Currently you have:
'neddle'
but the correct spelling is'needle'
with 2 e's. Now your code should work.No, they're not. Try putting the two strings like this when debugging your code, the difference becomes evident.
Sir please import find_needle in the unit test file. Due to this I am not able to run the tests and attempt my solution.
Not a kata issue, the function's name should be
find_needle
notfindNeedle
as you have.This comment has been hidden.
('found the needle at the position', 3) should equal 'found the needle at position 3'
That is my error and I dont know what to do in order to fix it. Any help?
Hi @austang and welcome to Codewars! Currently you are returning a
tuple
made up of 2 components: the first is thestring
'found the needle at the position'
and the 2nd component is theinteger
3
.You need to find a way to return one big
string
:'found the needle at position 3'
.If you are new to Python, you can join the Codewars Discord - link in sidebar on the left - there is a beginners channel where people can help you get started with the site.
Hey my log says ,found the needle at position 3, but It says that None should equal 'found the needle at position 3' , I don't understand why is it wrong.Thanks
Hi,
Not an
issue
, aquestion
.On CW, you're supposed to
return
the value, not to print it to the console.Cheers
Please read this: https://docs.codewars.com/training/troubleshooting
This comment has been hidden.
you are supposed to return the output, not print it
p.s., this is not a kata issue. kata issues are bugs in the kata. use the question tag next time
Scala translation
LGTM, Approved!!!
This comment has been hidden.
It always helps to mention your language. And yes, users who completed the kata in the same langiage can see your most recently attempted code.
Again, you sohuld try and run your solution locally, in your IDE, and call it twice in one test run. There's more than one problem with it currently, but the main issue is the
while
loop which is not really necessary, and causes the problem you see.Another issue is returning a statically allocated buffer, what is almost always wrong. This kata wants you to return a malloced, freeable buffer.
alright got it, thank you for the tips!
This comment has been hidden.
This comment has been hidden.
The
return print(answer)
is wrong, and makes your solution return wrong answers.Thank You for this tip!
I've just noticed that not only the print () function was redundant as a result. I stared at the sample answer and absolutely wanted mine to be the same as a string. Therefore, I have added single apostrophe on both sides. So the answer was the same on the printout. I was confused that as the correct output our Sensei gave the result in double quotation marks, ["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] -> "found the needle at position 5" - instead of this text without them. The better is the enemy of the good. ... B
OP solved the kata
This comment has been hidden.
This comment has been hidden.
Is it cheating if I look on google for help, and find a way to do it? It feels like cheating since I tried it myself a lot, and couldn't figure it out. I was trying to use for or while loops, but it wasn't working.
Hi and welcome to Codewars; the goal of the site is for you to develop your skills and progress with your understanding of coding in general and your language(s) in particular -- if you need to learn new concepts and/or features of your language, then of course you can research those.
In this case, since the kata is about working with basic language features, you should definitely make sure you are comfortable with them (how to move through an array etc).
Whether that is from reading a C# book or looking through some examples online, the important thing is to get those fundamentals down even if it means struggling a lot at first yourself. You can also check out the Codewars Discord as there is a dedicated "beginners" channel, and a C# channel.
Well, I found a command I didn't know about at first. So, I read about searching through an array and how to find the index something is in because I didn't know how to do that. I know about looping and everything, but I couldn't get it to work with a for loop, and because I've heard people google a lot, I didn't think it was cheating. The command I found I'd never heard of.
Google is nothing but another tool, and you're free to use it.
This comment has been hidden.
In C variation you doubled "const". const char *CONST(here) haystack[]
Thats deliberate. Both
const
mean something different.Memory management in C is all over the place. It's not specified how the result string should be allocated, and tests do not follow any kind of an allocation strategy for the returned string.
people have already been returning static buffers, so i think it would be unfair to invalidate their solutions
i added instructions and a call to
free()
in this forkfixed
Hi, I have passed all the preleminary test but when I attempt it returns to me that one random did not pass. The error is : Expected: found the needle at position 30 Received: It says to me that I returned nothing but when I printed the array returned in the log and here is what I have : found the needle at position 30 Which is the correct output. In the program I wrote I build other function. Is it possible that when we attempt it does not take these function into consideration?
Your solution returns a buffer which is allocated on the stack, what is an error in C. Your answer needs to be accessible for tests, so you need to return a pointer to a buffer which lives longer than just a call to the solution function.
OP solved the kata (while still returning a stack array from the function, remarkably)
This comment has been hidden.
There are big problems with this Kata created by the author
Which?
This comment has been hidden.
This is not the proper way to raise an issue:
Please read carefully this and don't raise issues so lightly in the future: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/#post-discourse and this: https://docs.codewars.com/training/troubleshooting/#.print-vs-return
Não estou entendendo esse site , eu resolvo o problema no vsCode , mas aqui aqui da erro. Tem que escrever o código todo em inglês para funcionar ? Alguem sabe me dizer
You must
return
the result, not print it. Also, it's no use to call the function inside your code. The tests call the function with diverse range of inputs to test if it works correctly. Just do like this:This comment has been hidden.
this cannot be considered an issue. checking whether the address is on the heap, stack or .data segment is not supported by standard C and involves UB.
I dont understand why I get this : expected 'found the needle at position 6' to equal 'found the needle at position 3', what is wrong?
It means your code returns "found the needle at position 6" whereas it should have returned "found the needle at position 3".
Don't overwrite the function's argument like that, your function will always return
"found the needle at position 3"
Got it! My mistake was that in function I wrote the array too, which was not needed.
I solved this correctly in VS Code. Im getting "unauthorized" when testing on this site and its very odd.
Yes, I'm also getting "unauthorized" on any kata I try at the moment. This seems to be a problem with codewars, not this kata.
Yes - I've had this issue with several kata just now, so the site is definitely having some technical issues!
Works again for me
Yep! Site outages are always fun.
Hi I am having an issue with tests where there is no needle, it shows up as not solved because there is no needle
your code needs to handle the cases if there is no
needle
in the list.index()
will throw an error if the item it is looking for is not present in the listThis comment has been hidden.
I don't know what language this is - I think Ruby? - but I'm guessing that it's failing because you aren't returning anything - use whatever the return statement in Ruby is instead of
print
.Your code not working is not an issue (= a bug in the kata). Please read this: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting/
You're returning a tuple, not a string.
Description should be language-agnostic
fixed by myself ^^
Hi guys, need help i pass random tests, but one of them wrong Cannot invoke "Object.equals(Object)" because "[]" is null what does it mean? i just use equals method
This comment has been hidden.
Not a kata issue, use the initial code, don't get rid of the function's argument:
Read this: https://docs.codewars.com/training/troubleshooting/
the test result stands: Log found the needle at position 3 None should equal 'found the needle at position 3' what is wrong with my solution?
None should equal 'found the needle at position 3'
means your code returnsNone
, in general it means it returns nothing. You mustreturn
the result notprint
(you can print whatever you want for your convenience, but the tests don't take it into account). Justreturn your_result
, it's the same in all katas.This comment has been hidden.
No, it's not a kata issue, see the first sample test. The method you used only works with strings or arrays.
This comment has been hidden.
because you're calling your function from the same scope, where
haystack
is not defined. what's passed into your function is coming from the testing suite.In other words, you must include
console.log
insidefindNeedle
to be able to seehaystack
.This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please mark your comment as spoiler if you have code in it.
() => {}
is a function. Your return statement only returns in that function. Thus your main function doesn't return anything.Ah, understood, thanks! Does it perhaps execute locally because locally I have the array as a global variable rather than in a test function? And hopefully my question is now marked with the spoiler tag - apologies, still getting the hang of Codewars.
I would be surprised if this worked locally at all. It would only work locally if you were printing the value instead of returning it. As it is now, your return statement doesn't really do anything because it's only returning the value to the
forEach
function, and theforEach
function doesn't really do anything with values returned to it.This comment has been hidden.
That's strange. I tried running this on a server I have running node 17.4, it prints "undefined" to my console. I see no way that it could print "found the needle at position 3" with the code that you've presented here.
Hello,
In the instruction it says that it should return "found the needle at position 5" - which I think this is correct based in the Array given, but when I test it the reason of failure is: "it is expected to return - found the needle at position 3 -"
Yes, the needle is at a different position for every test.
This comment has been hidden.
Please mark your comment as spoiler if you have code in it.
Your code doesn't return a value, because the
return
is inside another function.This comment has been hidden.
This comment has been hidden.
Your function should return the string, not using alert.
This comment has been hidden.
You're getting an error because you're calling enumerate on the function instead of the argument. Also, remember to return the string, not print it!
In future when you post code, you can put it in a code block like so so that the formatting doesn't get messed up: https://docs.codewars.com/references/markdown/#code-block
I'm not passing the test even thought the output is exactly the same:
Log found the needle at position 3 None should equal 'found the needle at position 3'
You're not returning anything. Not an issue.
Thanks - I was using print() instead of return
sameee
This comment has been hidden.
Not a kata issue, read this: https://docs.codewars.com/training/troubleshooting#expected-the-same
COBOL translation (author inactive).
approved
The example from the description should be included in sample tests for all languages. Currently, most of them (if not all) don't.
Hi i did All the sampples asked, but setill got the error of the Funcion. And on Pycharm and Jupyter Notebook got the result of all of them.
Example in description added to sample tests.
Affected languages: CoffeeScript, C#, Elixir, Haskell, Java, JavaScript, PHP, Python, Racket, Ruby, Scala, TypeScript
This comment has been hidden.
Because
print
returnsNone
, return the string instead of printing it. Read this: https://docs.codewars.com/training/troubleshooting/thx
This comment has been hidden.
You're not returning anything.
yeap!
done!
I found it =D
Ruby 3.0 should be enabled.
done
This comment has been hidden.
Please, mark your post as having spoiler content next time, read this: https://docs.codewars.com/training/troubleshooting/ print returns
None
.You can try and remove 'print' from your code
This comment has been hidden.
Not a kata issue, string interpolation in Ruby only works with double quotes. And you have a typo there.
Oh brilliant. Thanks for the pointer!
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
You have a typo there
poistion
, and mark your post as having spoiler content next time.This comment has been hidden.
That's because your code is printing instead of returning. Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Ok i got the code right. But what i dont get is why printing the statement was not accepted??
That's just how the site works: the tests require your function to return a value. That value is use to check for a valid alogithm. :)
This comment has been hidden.
Yup I also think it would be nice if this kata failed for solutions which rely on behavior of
object.operator==
.Understanding where the warning comes from is a nice way to learn why in C# searching for a string by comparison with
operator==
works if haystack isstring[]
, but does not work if haystack isobject[]
, but still works if needle is a string literal.added a test to invalidate those solutions
Using C... The tests work, but on the random test it fails, says that:
Expected: found the needle at position 4 Received:
But when I printf the same variable I'm returning, it shows:
Log found the needle at position 4
Kata works, your problem is not a kata issue.
It's difficult to say what exactly happens without seeing your code, but if I were to guess, you do not handle memory management correctly. It's not said explicitly, but C version requires you to return a freeable string.
.
Not sure why, it worked for me. If you want, you can post your code with proper markdown (don't forget the spoiler tag) and I'll take a look.
This comment has been hidden.
Please read this. Your function should return, not print the result.
got it thanks.
how to unable autocorrect to get help in syntex
This comment has been hidden.
This comment has been hidden.
spoiler flag!
: ('found the needle at position', 3) should equal 'found the needle at position 3' : ('found the needle at position', 5) should equal 'found the needle at position 5' : ('found the needle at position', 30) should equal 'found the needle at position 30' ))
Your code is returning a tuple instead of a string.
I am getting something very similar: expected:<found the needle at [position ]3> but was:<found the needle at []3>
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Why are you posting this here? Use solutions tab for this. And mark your post having spoiler content...
sorry im new to site, have to find out how it works.
Ok, just don't post your solution in discourse tab (and don't share them on other public platforms), unless you can't solve it and asking for help using QUESTION label. (code formatting)
This comment has been hidden.
Even when marked as spoiler, kata's general discussion is not the place to review functional code, you should ask in your solution instead.
Ok. Thx for the correction, but the edit button is not available
This comment has been hidden.
I think it should tests also for cases such as no needle or multiple "needle".
not able to get result from Ruby. I proofed my syntax on other compiler... yet here it is saying all my "puts" are nil
Your function should return the result, not print it.
you're right, just figured it out and came back to correct it hehe
thanks!
Racket translation
Haskell translation
.
Thanks. You did read the comments?
Yes, I saw the comments. I'm fine with it.
This comment has been hidden.
you return the address of a local (stack-allocated) array, as the warning tells you:
Hi everyone! I did my code with JS on repl.it and it works, but then here output is "undefined" :(
99% sure your code is not returning the result, instead it's printing it. Are you using console.log?
im also did my code in replit) console.log)) works... but here... whats wrong?
you need to return the result. not print it
This comment has been hidden.
Undefined behavior because of
string_rt
passed asconst char *restrict
being passed as another argument as well. Extra\n
in the end.This comment has been hidden.
Java version has problem. I have tested my code in IDEA and it works perfectly, but I have different output, then I'm doing sample tests here.
Most probably it's a problem with your code:
The only way to know for sure is for you to paste your code using markdown formatting here and mark your post as having spoiler content.
This comment has been hidden.
Tests work perfectly fine. Your code must be wrong.
This comment has been hidden.
Just reading the test output should give you your answer - you need a space between the word 'position' and the index.
Haskell translation
Bump.
reposting on top
To pass all the test cases, nil must be counted as its own index.
This comment has been hidden.
Help us help you. Format your code as code.
( see Wiki Markdown Formatting )
This comment has been hidden.
Mark your post as having spoiler content when it does and use proper markdown Find how to put a var value in a string in Python (there is more than one way), what you did won't work.
I went with the if statement but when I went to grab the index I was getting '0'. This is true because once needle is found, needle was all I had to work with. You are close but lose the if statement.
This comment has been hidden.
There must be something really wrong with this kata. Your code is perfect, the other hundreds (thousands?) of people who solved it must be dirty cheaters.
Wait, no. Just read the description again: "return a message (as a string)". It doesn't say "print a message" or "return the index". It says: "return a message (as a string)"
Surely he's wrong, but did you even look at his code before saying that he's not returning anything?
Yes, I was wrong and figured out my error. However, how about reading the code before saying anything? It's clearly trying (albeit incorrectly) to return a string.
This comment has been hidden.
My bad. I'm happy you found the problem
This comment has been hidden.
Almost 7000 people solved it in js, but it doesn't work for you. Really?!
That's not js, it is Java. The problem is with
.equals()
. Inverting the order also works.try to read my comment again anter69, what's wrong with the .equals() anyway? I see now that works with "==" but I thought that to compare strings properly you had to use .equals(); If you could clear that for me, I'd be much appreciated.
null
is not a String object, so you can't use.equals()
. You could also invert the order and use it, "needle" is a String object (it'll work that way).I hope you found the solution
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
In my screen only two opions unlock solution and skip.
Probably this: https://github.com/Codewars/codewars.com/issues/1214 Try scrolling down or changing the window size.
This comment has been hidden.
Use
Mark as having spoiler content
checkbox when pasting code here. The return line is outside the function body it seems. Also, read again the instructions:To run your code there are 2 buttons:
RUN SAMPLE TESTS
andATTEMPT
This comment has been hidden.
You copy-pasted wrong
I wrote this myself actually, hence the mistakes. I realized my mistake and its solved now, no thanks to you.
Your repl.it code works fine, the code in your comment doesn't.
This comment has been hidden.
This comment has been hidden.
TypeScript Translation submitted!
Can anyone approve my translation?
done
Made it a little more convoluted than it needed to be, gave me a reminder of how to find things in a list.
This comment has been hidden.
You have to return it, not print it to the console.
Test Results: Log found the needle at position 3 : None should equal 'found the needle at position 3'
python not work
You have to return it, not print it to the console.
This comment has been hidden.
You have to return it, not print it to the console.
I'm using C to solve this but for some reason it's failing the random tests, it passes all the sample tests but it fails the random tests. It returns the correct output, I know because I have a printf statement to double check but for some reason it's not receiving my return statements for the random test cases.
Log found the needle at position 3 Expected: found the needle at position 3 Received:
The above line(2nd/middle line) is the printf statement and the last line is the test case, it appears for the random test case my return statements were not received, but for some reason it works fine for sample test cases.
Currently there are 161 accepted solution in C. If you still think that the C version is buggy, post your solution code surrounded by a line with just three backticks (```) and mark the comment as "having spoiler content".
No response, closing this issue.
same here, passes all the sample tests but fails at random
found my error, sorry
The (Java) Kata provides an array of Objects, but does only provide Strings in it. This leads to a lot of solutions, that skip type checking and casting (if necessary) and thus reinforces bad coding habits.
java isn't working
Please clarity what is not working.
This comment has been hidden.
This comment has been hidden.
You need to return your output and not print it to the console. All katas on this website work like this. Returning the value, means the value is retained from that function call. The value retained can be checked and checked for equality against the expected answer. Printing to the console is hard to check for equality.
This comment has been hidden.
You need to return your output and not print it to the console. All katas on this website work like this.
This comment has been hidden.
"found needle at position "
to"found the needle at position "
int Index = i;
since you have already declared that it would be an integer previouslyThis comment has been hidden.
C translation kumited! Please check and approve if all's good. :)
The Java version needs this test:
This is due to the Java compiler sharing String instances where possible, resulting in == being sufficient for the tests where .equals should really be used.
done (
new String("needle")
is enough to create a distinct object)Hey, Andrew! I am working on a C translation for your kata, however returning a formatted string in C requires dealing with extra memory operations, which makes it more difficult than 8kyu. I was wondering if I can alter the spec for the C translation a little and request that users simply return the index of the needle instead. Thank you!
This comment has been hidden.
You need to
return
the value as a single statement instead ofprinting
it. Returning a value allows it to be checked for equality, printing just outputs it to the console.All Katas on this website work like this
This comment has been hidden.
Separating values using a comma, creates a tuple in which the first value is the first index of the tuple ("found the needle at position") and the second value is the second index of the tuple (3). You need to concatenate them together as a string.
I'm formatting a string with the index. When I run tests, I get the proper output and the error message like this:
Test Results: found the needle at position 3
✘ : None should equal 'found the needle at position 3'
What am I missing?
you need to be very careful in how you represent the indexnumber of the element called "needle" Try to make a finalresult, and return that final result when you are ready with the index number and the location of the needle element
And you need to for example return for example as follows, ... you have the storyline which was 'found the needle at position' + ' ' +(indexnumbervariable) The middle thing in that sum is simply an empty spacebar press inside the ' ' markers.
'Looks like you've solved it'
PHP translation added.
Are you crazy? ;-)
Ruby translation submitted, if you want to approve it; I would anyway revise the Python code, as it seems to give away a bit too much in the example tests.
My code works with all the tests when tested with python IDLE but returns error on here, help?
Traceback: in in run_test in find_needle in TypeError: argument of type 'NoneType' is not iterable
I am also having issues with the basic test. I pass the random test cases but neither the default user test cases or the basic test cases will pass with my code.
What is basic test? Idk why i'm failing it
I did a simple for loop which WORKED and still failed.
For Java, I am getting the following error: basicTests(NeedleTests) Unknown Test Failure Other tests passed. Any idea what the above error is referring to? randomTests(NeedleTests) Testing 25 random haystacks... Test Passed
Would like to ask more test scenarios and it's results.
Also would like to clarrify some specifications
The extra test scenarios are not needed, but if they can't ever happen it should be added to the Description.
Why would a limit of the array matter? I am sure it will be ok as long as it fits in a
Int32
which is up in the billions.What the array can contain, well objects of course. If you wanna make sure you only check strings you can if each element is of type string before checking the element further.
So yeah I agree that the Kaka's Description could be a lot better, but the Kata itself is fine as it is.
For a good example of a simple but good description I will suggest you look at my Kata Highest and Lowest, never had much in the form of misunderstandings with that.
Good evening. My solution passed main test, but I get error such - basicTests(NeedleTests) Unknown Test Failure. What does it mean?
What does it mean - "basicTests(NeedleTests) Unknown Test Failure"?
I got an Unknown Test Failure? The tests in a previous kata worked well.
This comment has been hidden.
This comment has been hidden.
I get the correct output - "found the needle at position" [index] however it fails to accpet it and states that it's wrong
Are you returning a string formatted exactly like this:
"found the needle at position 5"
(or the actual index instead of 5)?Not sure what you mean, wolfcat. I'm getting a string that matches the test output exactly. Should I not be showing the actual index? The test itself is looking for a digit.
I am so stuck! I know it's pretty easy, i've dealt with arrays many times and I know how they work. However with this test I've tried lots of combinations and nothing seems to be doing the trick, I get an error stating that the needle should be at index of three but when I move it to that location it states that the needle should be in index 5. Any help would be greatly appreciated!
What do you mean when you say that you move the needle to a position? In this Kata you have to find the position of the needle that is already in the haystack.
Sorry, terrible explanation.. The comment above mine is exactly what's happening with me. I was concatenating the index to the string. I guess I'll try what you recommended.
Java translation kumited.
Thanks, approved
you should give user process more options
i think its 8kuy or lower
it not worth 3 points, only 1 i think
Fair enough, I did intend it to be a very much a beginners challenge but good suggestions (I'm not sure it's an issue though, rather more suggestions)
ok. its just advice