CodeKata 7

이성보·2020년 12월 13일
0

문제

숫자로 이루어진 배열인 nums를 인자로 전달합니다.

숫자중에서 과반수(majority, more than a half)가 넘은 숫자를 반환해주세요.

def more_than_half(nums):
  for i in nums:
    if nums.count(i) > len(nums) / 2:
      return i
def more_than_half(nums):
	majority_count = len(nums)//2
	for num in nums:
		count = sum(1 for elem in nums if elem == num)
		if count > majority_count:
			return num

0개의 댓글