#๐Ÿ”’ Python enumeration error

28 messages ยท Page 1 of 1 (latest)

night moat
#

really unsure as to why the enumeration command isn't creating a list of items here? https://www.codewars.com/kata/5566b0dd450172dfc4000005/train/python
My Code:

def length_of_sequence(arr,n):
    index = [i for i, e in enumerate(arr) if e == n ]
    print(index)
    return arr[(index.pop(0)):(index.pop(-1))]

Test:

@test.describe("Fixed Tests")
def fixed_tests():
    @test.it('Basic Test Cases')
    def basic_test_cases():
        test.assert_equals(length_of_sequence([1], 0), 0, 'Returns zero when the element is not found in the array')
        test.assert_equals(length_of_sequence([1], 1), 0, 'Returns zero with only one element in the array')
        test.assert_equals(length_of_sequence([1, 1], 1), 2, 'Returns two when there are only two elements in the array');
        test.assert_equals(length_of_sequence([1, 2, 3, 1], 1), 4, 'Returns four for an array of length four and the number to be searched at the boundaries');
        test.assert_equals(length_of_sequence([-7, 5, 0, 2, 9, 5], 5), 5, 'Returns five');
        test.assert_equals(length_of_sequence([-7, 6, 2, -7, 4], -7), 4, 'Returns four');
        test.assert_equals(length_of_sequence([0, 8, -7, 6, 1, 2, -7, 4, 8, 9], 8), 8, 'Returns eight')
        test.assert_equals(length_of_sequence([-7, 3, -7, -7, 2, 1], -7), 0, 'Returns zero as when there are more than two instances')
        test.assert_equals(length_of_sequence([-7, 3, -7, -7, 2, -7], -7), 0, 'Returns zero as when there are more than two instances')
Codewars

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

sharp kayakBOT
#

@night moat

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

celest ruin
#

!code please

sharp kayakBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

celest ruin
#

Use ```py
print('Hello world!')
```

#

you'll know you've done it right when the text in the edit box changes font

#

those are backticks, not quotes

night moat
#

Done :)

celest ruin
#

What is your function supposed to do?

night moat
#

It's meant to create a list based on the list provided where it starts at the first instance of an integer n and ends at the final instance of that integer, creating a subsection

#

but I'm making the more basic mistake of not making a list at all unfortunately

celest ruin
#

so if the array has no value n, it creates an empty list?

night moat
celest ruin
#

and doing [].pop(0) raises an error

night moat
celest ruin
#

so your first asert of length_of_sequence([1], 0) should raise IndexError

night moat
#
Traceback (most recent call last):
  File "/workspace/default/.venv/lib/python3.11/site-packages/codewars_test/test_framework.py", line 112, in wrapper
    func()
  File "/workspace/default/tests.py", line 8, in basic_test_cases
    test.assert_equals(length_of_sequence([1], 0), 0, 'Returns zero when the element is not found in the array')
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/default/solution.py", line 4, in length_of_sequence
    return arr[(index.pop(0)):(index.pop(-1))]
                ^^^^^^^^^^^^
IndexError: pop from empty list
night moat
celest ruin
#

Yes.

#

You should add a check for that.

#

if it's length 0, return an empty list. if length 1, return [n]. otherwise do as normal

night moat
#

and that's at the core of the problem here, the enumeration is fine?

celest ruin
#

yes.

night moat
#

okay, I will try adding a check!

#

awesome I misunderstood the question since it turns out it was asking for length but yeah the check was something I was missing and was the reason fro that error above

#

thanks!

sharp kayakBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.