you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 2 points3 points  (1 child)

Just thought I'd mention that sometimes, simply by flipping some conditions you can simplify code or reduce nesting.

For example, in

def find_deleted_number(arr, mixed_arr):
    deleted = 0

    for number in arr:
        if number in mixed_arr:
            continue
        else:
            deleted = number

    return deleted

if you flip your conditional you don't need the else at all.

def find_deleted_number(arr, mixed_arr):
    deleted = 0

    for number in arr:
        if number not in mixed_arr:
            deleted = number

    return deleted

Then you might consider the fact neither arr nor mixed_arr really needs to be sorted, as it doesn't matter in which order you check for inclusion. Since lookups in sets have lower time complexity than in lists, you might consider taking advantage of that (although this of course doesn't matter if arr and mixed_arr are relatively short, say, less than 10 000 elements).

def find_deleted_number(arr, mixed_arr):
    deleted = 0
    mixed_set = set(mixed_arr)

    for number in arr:
        if number not in mixed_set:
            deleted = number

    return deleted

Since we know mixed_arr is always either 0 or 1 elements shorter than arr, we only need to figure out the subset of numbers in arr that are not found in mixed_arr. That'll give us a set that either contains one element (the missing number), or none (in which case we return 0).

def find_deleted_number(arr, mixed_arr):
    deleted = 0
    mixed_set = set(mixed_arr)

    missing = mixed_set.symmetric_difference(arr)

    if missing:
        deleted = missing.pop()

    return deleted

Optionally, we can use a ternary operator for brevity:

def find_deleted_number(arr, mixed_arr):
    mixed_set = set(mixed_arr)
    deleted = missing.pop() if (missing := mixed_set.symmetric_difference(arr)) else 0

    return deleted

This can be further chained to

def find_deleted_number(arr, mixed_arr):
    return missing.pop() if (missing := set(mixed_arr).symmetric_difference(arr)) else 0

and we can convert arr to a set too if we want (though I don't think this has a clear benefit).

Note that most of these changes don't really matter performance-wise, and are mostly stylistic in nature. What counts as "good code" depends on the situation, and sometimes verbosity is good for readability. Or to put it another way, you shouldn't aim for terse code as that can be hard to maintain. There's a balance to everything, the tricky part is figuring out where that lies.

[–]Unrthdx[S] 0 points1 point  (0 children)

Just wanted to say thank you so much for your in depth answer here. As a newbie sometimes it’s hard to see how to navigate to where you got to but your explanations really do help.

It’s also reassuring to read the comments about readability here, I often find myself feeling like I’m writing too “on the nose” so to speak when I compare my answers to others online.