물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Number Zoo Patrol

BEstyle 2022. 11. 7. 14:25

DESCRIPTION:

Background:

You're working in a number zoo, and it seems that one of the numbers has gone missing!

Zoo workers have no idea what number is missing, and are too incompetent to figure it out, so they're hiring you to do it for them.

In case the zoo loses another number, they want your program to work regardless of how many numbers there are in total.


Task:

Write a function that takes a shuffled list of unique numbers from 1 to n with one element missing (which can be any number including n). Return this missing number.

Note: huge lists will be tested.

Examples:

[1, 3, 4]  =>  2
[1, 2, 3]  =>  4
[4, 2, 3]  =>  1

def find_missing_number(n):
    n=sorted(n)
    if n==[]:
        return 1
    if n[0]!=1:
        return 1
    if n[-1]!=len(n)+1:
        return len(n)+1
    for i in range(0,len(n)-1):
        if n[i]+1!=n[i+1]:
            return n[i]+1

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Square Matrix Multiplication  (0) 2022.11.08
Twisted Sum  (0) 2022.11.08
Loose Change  (0) 2022.11.07
Triangle Type  (0) 2022.11.07
Transform to Prime  (0) 2022.11.06