# -*- coding: utf-8 -*-
"""
Created on Fri Mar 3 20:07:11 2017
@author: zzpp220
"""
class Morethanhalf:
def findhalf(self,lst):
if not lst:
return None
dict={}
half=[]
for i in lst:
dict[i]=lst.count(i)
half=[k for k ,v in dict.iteritems() if v >=len(lst)//2]
return half
def CheckMoreThanHalf(self, numbers, length, number):
times = 0
for i in range(length):
if numbers[i] == number:
times += 1
if times*2 <= length:
return False
return True
def MoreThanHalfNum(self, numbers):
if numbers == None :
return 0
length = len(numbers)
result = numbers[0]
times = 1
for i in range(1, length):
if times == 0:
result = numbers[i]
times = 1
elif numbers[i] == result:
times += 1
else:
times -= 1
if not self.CheckMoreThanHalf(numbers, length, result):
result = 0
return result
solution=Morethanhalf()
lst=None#[1]#[1,2,6,14,4,4,3]
print solution.findhalf(lst)
print solution.MoreThanHalfNum(lst)
附件列表