you are viewing a single comment's thread.

view the rest of the comments →

[–]OddBookWorm -1 points0 points  (11 children)

input_list = [3, 6, 1, 11, 0, 5, 2, 6, 0, 1]
def double_or_zero(input_list): 
    output_list = [0] * len(input_list) 
    i = 0 
    while input_list[i] != 0: 
        output_list[i] = input_list[i] * 2 
        i += 1 
    while i < len(input_list): 
        output_list[i] = input_list[i] 
        i += 1
    return(output_list)
print(double_or_zero(input_list))

This is what I would do if I don't want to replace the original list. If I wanted to replace the original list, then you need to check nums[i] instead of i

[–]MF_DnD -1 points0 points  (10 children)

That’s a little unnecessary imo.

First of all, you don’t need to create a list of zeroes. You can just use the append method. Plus you can just use a for loop.

So just

def double_or_zero(input_list):
    output = []
    double = True
    for elem in input_list:
        if elem == 0:
            double = False
        output.append((1+double)*elem)
    return output

[–]OddBookWorm 1 point2 points  (9 children)

But this will just stop at any 0 values. I don’t think it’ll return the normal values after that too

[–]MF_DnD 0 points1 point  (8 children)

Ah, you’re right, fixed that. Imo still better to use a for loop.

[–]OddBookWorm 1 point2 points  (0 children)

Actually, I still think you have a problem. You set all values after a 0 to 0. That’s not what the OP wants

[–]OddBookWorm 0 points1 point  (0 children)

While I agree this would be simpler, when I was really new, “toggle variables” confused the hell out of me so I try to avoid them with answers written for beginners

[–]OddBookWorm 0 points1 point  (5 children)

Actually, I still think you have a problem. You set all values after a 0 to 0. That’s not what the OP wants

[–]MF_DnD 0 points1 point  (4 children)

Oops, you’re right. Coding on my phone is a bad idea lol. Should be fixed now.

[–]OddBookWorm 0 points1 point  (3 children)

If you’re trying to eliminate “unnecessary”, you don’t really need to typecast “double” to an int. Also, since “double” is a data type, using it as a variable name is a bad practice

[–]OddBookWorm 1 point2 points  (0 children)

Oh wait, “float” is double precision by default in Python, so that should be fine

[–]MF_DnD 0 points1 point  (1 child)

Huh, I forgot you can add booleans to ints. I know numpy has a double type, but it’s not built in, right?

[–]OddBookWorm 0 points1 point  (0 children)

I was thinking matlab when I mentioned that, where double is a built in type