http://bbs.fishc.com/thread-93788-1-1.html 鱼C论坛上相关Python练习题
Given an unsorted array of integers, find the smallest number in the array, the largest number in the array, and the smallest number between the two array bounds that is not in the array.
For instance, given the array [-1, 4, 5, -23, 24], the smallest number is -23, the largest number is 24, and the smallest number between the array bounds is -22. You may assume the input is well-formed.
You solution should return an array [smallest, minimumAbsent, largest]
The smallest integer should be the integer from the array with the lowest value.
The largest integer should be the integer from the array with the highest value.
The minimumAbsent is the smallest number between the largest and the smallest number that is not in the array.
以下是我写的代码:
# -*- coding: utf-8 -*-
l = [2, -4, 8, -5, 9, 7]
L1 = sorted(l)
L2 = []
L2.append(L1[0])
a = L1[0]
for i in L1:
if i == a:
a = a+1
else:
L2.append(a)
break
L2.append(L1[-1])
print L2
运行结果: