7 kyu
Find the index of the first occurrence of an item in a list (with a twist)
587 of 605Chris_Rands
Loading description...
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.
Python: Random tests are vulnerable to input modification
Test framework and solution should be imported explicitly in sample tests.
New test framework should be used in Python.
Fixed
Thanks!
Spoiler alert : there is no twist!
my comment have been removed but i dont get an answer???
It's not removed, it's simply marked as spoiler. Your function returns incorrect results as you can see, there's nothing wrong with kata (you probably misunderstood the task). You need to get 2nd index IFF the first element in list matches
x
, otherwise you get the first one.my function does that,as you can see from 90%+ passed tests and the wrong ones dont have any common trand, also,below there is a comment about excatly the same issue. i would very appriciate if you, idk if you can, but if u can,look at my code and give me some hint.. thanks
so here for example: we can see that the array has a in 13 and 14 and i return 14,since index 13 matches x as per the demand, so why oh why should it equal 13??? ['P', 'f', 'W', '&', '$', 'y', ';', ',', '', 'e', '@', '{', 'Z', 'a', 'a'] a 2 - amount of times x apear in the arr 13 first index 14 second index Random input: [['P', 'f', 'W', '&', '$', 'y', ';', ',', '', 'e', '@', '{', 'Z', 'a', 'a'], 'a']: 14 should equal 13
Not the first match, the first item in the list. Please delete the other post, there is no need for 3 threads about the same problem.
thanks!
@Chris_Rands
,PEP8 - Names to Avoid
Using
l
as a variable name is something you should avoid, especially in user-facing code likeInitial Solution
.I'm curious as to why you would write the
Example Test Cases
in such an over-complicated and hard to read manner.When you write the examples as shown above (matching the Codewars Python Test Reference), the inputs and outputs are already clear. No need for three levels of nesting, for loops or custom messages.
After solving 400+ katas, haven't you noticed that only a single user (before your emulation) writes their
Example Test Cases
in this bizarre manner?Hi Zebulan, points taken, this was my first kata; feel free to edit the kata if you wish :)
@Chris_Rands
,I can't edit the kata since it has already been approved. You could simply copy/paste the tests from my previous reply into
Example Tests
if you wanted to.Another reason why you shouldn't run tests in a loop is that if one fails, none of the tests below it will pass. This is a bad pattern to teach beginners.
pytest
. As in the second example, all three tests will run (even if they all fail).Writing tests properly is a good habit to get into, especially for beginners! Unfortunately, some users seem to think it's funny to purposely teach beginners bad habits.
Thanks!
Modified the variable name from
l
tolst
. (and corrected the word "occurance" to "occurrence")In my opinion something is wrong with random test. In some cases it throw an error that position of "x" should be first occured "x" even though it occurs more than once. Please check it.
Sorry I don't understand, no-one else has had issues with this, can you paste a full example?
This comment has been hidden.
You'll need to show the code with the identation for me to read this
This comment has been hidden.
Ok you overcomplicate the kata; the count of the element is not relevant, read the kata description again. Pretend the 0th element doesn't exist and the index starts from 1, then you're looking for index of the first occurance of
x
. Clear?Clear, thx for advice :)
what do u mean the count is not relevant?ofc it is relevant,if it apears one time show it if it apear more than one time show the second so you must count them to know how many there is! this kata is no bueno!
No, the count is not relevant - consider the input
f(['a', 'b', 'b'], 'b')
- the answer is 1 (first occurrence ofb
, ignoring first element), and the fact thatb
occurs twice is irrelevant.The requirement is not to "show the second if it appears more than one time" as you have written - it is to show the first time it occurs, ignoring the first element.
This comment has been hidden.
This comment has been hidden.
Hi, consider adding random tests :)
Added some random tests, thanks
Thanks