7 kyu

Find the index of the first occurrence of an item in a list (with a twist)

587 of 605Chris_Rands
Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • saudiGuy Avatar

    Python: Random tests are vulnerable to input modification

  • FArekkusu Avatar

    Test framework and solution should be imported explicitly in sample tests.

  • FArekkusu Avatar

    New test framework should be used in Python.

  • RealKenshiro Avatar

    Spoiler alert : there is no twist!

  • bluiisdave Avatar

    my comment have been removed but i dont get an answer???

    • B1ts Avatar

      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.

    • bluiisdave Avatar

      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

    • bluiisdave Avatar

      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

    • Chrono79 Avatar

      but ignoring the first item in the list.

      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.

  • zebulan Avatar

    @Chris_Rands,

    PEP8 - Names to Avoid

    Never use the characters 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye) as single character variable names.

    In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use 'l', use 'L' instead.

    Using l as a variable name is something you should avoid, especially in user-facing code like Initial Solution.


    I'm curious as to why you would write the Example Test Cases in such an over-complicated and hard to read manner.

    • Packing up arguments simply to unpack them in the very next step is useless.
    • It is also unnecessary to add a custom message to show the input when a user can simply look at the inputs.
    test.assert_equals(index_finder(list('abc'), 'b'), 1)
    test.assert_equals(index_finder(list('bbb'), 'b'), 1)
    test.assert_equals(index_finder(list('bcba'), 'b'), 2)
    test.assert_equals(index_finder([0, 2, 'a', '5', 0, 1, 0], 0), 4)
    

    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?

    • Chris_Rands Avatar

      Hi Zebulan, points taken, this was my first kata; feel free to edit the kata if you wish :)

    • zebulan Avatar

      @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.

      • https://bpaste.net/show/9c1d7715d63a
        • In the first example, only one test will run (out of three) since the first test fails.
        • Although the second example is more verbose, all three tests will run (even if they all fail).
        • The third example is a nice feature of 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!

    • Haksell Avatar

      Modified the variable name from l to lst. (and corrected the word "occurance" to "occurrence")

      Issue marked resolved by Haksell 5 years ago
  • SebastianZ Avatar

    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.

    • Chris_Rands Avatar

      Sorry I don't understand, no-one else has had issues with this, can you paste a full example?

    • SebastianZ Avatar

      This comment has been hidden.

    • Chris_Rands Avatar

      You'll need to show the code with the identation for me to read this

    • SebastianZ Avatar

      This comment has been hidden.

    • Chris_Rands Avatar

      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?

    • SebastianZ Avatar

      Clear, thx for advice :)

    • bluiisdave Avatar

      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!

    • neilm Avatar

      No, the count is not relevant - consider the input f(['a', 'b', 'b'], 'b') - the answer is 1 (first occurrence of b, ignoring first element), and the fact that b 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.

  • Chris_Rands Avatar

    This comment has been hidden.

  • suic Avatar

    Hi, consider adding random tests :)