Created
August 22, 2017 19:32
-
-
Save vijayanandrp/990dcbde4a259aac39f7bab72a0bfbcc to your computer and use it in GitHub Desktop.
PermMissingElem.py #Codility Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you can write to stdout for debugging purposes, e.g. | |
# print "this is a debug message" | |
def solution(A): | |
# write your code in Python 2.7 | |
# input validation | |
# empty input | |
if not len(A): | |
return 1 | |
# single input | |
if len(A) == 1: | |
if 1 in A: | |
return A[0]+1 | |
else: | |
return 1 | |
# solution | |
minimum = min(A) | |
maximum = max(A) | |
if minimum <= 0: | |
return 1 | |
if minimum > 1: | |
return minimum - 1 | |
B = [_ for _ in range(minimum, maximum + 1)] | |
C = set(B) - set(A) | |
C = list(C) | |
# output validation | |
if not len(C): | |
return maximum + 1 | |
if len(C) == 1: | |
return C[0] | |
else: | |
mi = min(C) - 1 | |
ma = max(C) + 1 | |
return mi if mi >= 0 else ma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment