Discuss / Python / 交作业啦(≧∀≦)ゞ

交作业啦(≧∀≦)ゞ

Topic source

#不运用迭代的方法(直接运用max(),min()函数,简略写……)

def findMinAndMax(L):

 Max=max(L)

 Min=min(L)

 return (Max,Min)

#运用迭代的方法

def findMinAndMax(L):

 from collections import Iterable

 if not isinstance(L,Iterable):

  raise TypeError('please enter iterable objects')

 if len(L)==0:

  return 'None'

 else:

  i=L[0]

  j=L[0]

  for num in L:

   if num >= i:

    i = num

   elif num <= j:

    j = num

   else:

    pass

  return ('Max:',i,'Min:',j)

#不运用迭代的方法(直接运用max(),min()函数,简略写……)
def findMinAndMax(L):
 Max=max(L)
 Min=min(L)
 return (Max,Min)
#运用迭代的方法
def findMinAndMax(L):
 from collections import Iterable
 if not isinstance(L,Iterable):
  raise TypeError('please enter iterable objects')
 if len(L)==0:
  return 'None'
 else:
  i=L[0]
  j=L[0]
  for num in L:
   if num >= i:
    i = num
   elif num <= j:
    j = num
   else:
    pass
  return ('Max:',i,'Min:',j)

  • 1

Reply