Python:每日一题 72(答题领鱼币)
本帖最后由 新手·ing 于 2017-8-6 12:06 编辑今天给大家带来一道算法题~
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
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.
minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
minMinMax(); //[-3, 0, 8]
minMinMax(); //[-5, -3,9]
英文看不懂?
回复给你中文版!
**** Hidden Message *****
为了方便理解,下面是例子:
minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
minMinMax(); //[-3, 0, 8]
minMinMax(); //[-5, -3,9]
{:10_297:} 大家一起来啊
解决方案:
**** Hidden Message ***** def minMinMax(lst):
Max1 = max(lst)
min1 = min(lst)
Min1 = min1 + 1
while Min1 < Max1:
if not Min1 in lst:
break
Min1 += 1
else:
Min1 = 'None'
return @李金龙 @lumber2388779 @冬雪雪冬 @hldh214 @MSK @ba21 def largest(array):
array.sort(reverse=True) #从大到小排序
return array #返回0号元素,最大值
def smallest(array):
array.sort() #从小到大排序
return array #返回0号元素,最小值
def minimumAbsent(array, smallest, largest):
minilist=list(range(smallest, largest))
lastlist=[]
for each in minilist:
if each not in array:
lastlist.append(each)
lastlist.sort() ##从小到大排序
return lastlist #返回0号元素,最小值
def minMinMax(array):
le = largest(array)
se = smallest(array)
mma = minimumAbsent(array, se, le)
newarray=[]
newarray.append(se)
newarray.append(mma)
newarray.append(le)
return newarray
支持楼楼~ ba21 发表于 2017-8-6 12:59
最大和最小用内置函数就行 @hldh214 666是不够的{:10_249:}做题! MSK 发表于 2017-8-6 13:28
支持楼楼~
做题{:10_249:} # _*_ coding: utf-8 _*_
import math
def minMinMax(array):
newarray = []
Minnum = 0
array.sort()
length = len(array)
newarray.append(array)
for i in range(array, array):
if i not in array:
Minnum = i
break
newarray.append(Minnum)
newarray.append(array)
print(newarray)
minMinMax() 支持楼主 我是来大打酱油的。。。 我要看中文版 看不懂英文 看看 这就尴尬了 加油! def minMinMax(list1):
b = []
list1.sort()
b.append(list1)
for i in range(list1,list1):
if i not in list1:
b.append(i)
break
b.append(list1.pop())
print(b)
a =input("请输入你的数据(例:1 2 3 4):")
b = []
for i in a.split(' '):
b.append(int(i))
minMinMax(b)
{:10_279:} 参考答案好简洁,学到了{:10_254:} 新手·ing 发表于 2017-8-6 15:09
最大和最小用内置函数就行
没考虑到。直接就上代码了{:10_262:} 我要看中文版 本帖最后由 chunchun2017 于 2017-8-6 20:47 编辑
def minMinMax(list2):
list1=sorted(list2)
minnum=list1
Maxnum=list1[-1]
flag=0
Minnum=minnum
for each in list1:
if(Minnum==each):
Minnum+=1
if(Minnum>=Maxnum):
flag=1;
break;
else:
break;
if flag==1:
return #*表示中间值不存在
else:
return
str0=input("请按[*,*]格式输入一个列表:")
list1=list(eval(str0))
print(minMinMax(list1))
=======================
运算结果:
请按[*,*]格式输入一个列表:
[-3, 0, 8]
请按[*,*]格式输入一个列表:
[-5, -3, 9]
请按[*,*]格式输入一个列表:[-3,-2,-1]
[-3, '*', -1]