Ad
  • Custom User Avatar

    It's a method to ignore specific values.

    For instance:

    a,_,b = (1,2,3)
    Then: a=1 and b=3
    Another example:

    a,*_, b = (1,2,3,4,5,6,7,8)
    a = 1 and b = 8

    Other function to this is save the last result in "_" variable.

    So if you do this:

    2+4 -> result= 6

    _ -> result= 6 #save the last result in your program.

    For separate numbers:

    a=100000 #it's confuse to see how many zeros are there.

    But you can do:

    a= 10_000 #it's the same, but more understandable.

    This link helps so much: https://www.datacamp.com/tutorial/role-underscore-python

  • Default User Avatar

    If you slice sequences by specifying out of bound index, it will slice the sequence to nearest index

  • Custom User Avatar

    i believe it works due to fact that value of arr[:i+1] is not being called as ending value is not included in a slice of list.

    Example:
    arr = [1,2,3,4,5]
    len(arr) = 5
    [i for i in range(5)] --> i = [0,1,2,3,4]
    len(arr) --> IndexError
    arr[:i] -> arr[:4] --> [1,2,3,4]
    arr[:i+1] -> arr[:5] --> [1,2,3,4,5]

  • Default User Avatar

    I think I was playing around with diffetent solutions and accidently forget to remove the import.

  • Custom User Avatar

    Added a note.

  • Custom User Avatar
  • Custom User Avatar

    It seems that in the Python of this, the same object is passed as a variable to the function, and is also used to test the result. I haven't encountered this in other exercises before.

    I just resolved this issue in my own solution, but it seems several people are frustrated by this problem. This seems to be causing unnecessary hassle for people - maybe it's worth changing?

  • Custom User Avatar

    I agree, this issue is not resolved - I am having the same issue.

    Input array:
    [7, 0, 'z', 'c', 0, 7, 0, 'z', 'c', 0, 8, 0, 'y', -6, 'b', 0, 0, False, 2, 9, 8, -4, 0, 0, -2, False, -7, -9, -2, 0, -6, 'pippi', 0, 'z']

    My function's output:
    [7, 'z', 'c', 7, 'z', 'c', 8, 'y', -6, 'b', False, 2, 9, 8, -4, -2, False, -7, -9, -2, -6, 'pippi', 'z', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    The test's expected output:
    [7, 'z', 'c', 7, 'z', 'c', 8, 'y', -6, 'b', False, 2, 9, 8, -4, -2, False, -7, -9, -2, -6, 'pippi', 'z']

    There are no zeros in the expected output - this appears to be a mistake.

  • Custom User Avatar

    No.

    You mutated the input.

  • Default User Avatar

    It's worse than that.

    I thought to hard code the right thing for the basic tests and the zero elimination for the random tests. I then hit some random tests that do result in zeros at the end of the expected output.

    So, it is completely borked.

  • Default User Avatar

    Yes, and it will even keep the value, (i.e., _ is a valid variable name, and is not special in any way other than by convention). It's just seen as slightly more "Pythonic" to use _ as a sign that you're ignoring a value.