[CodeKata] -9
def top_k(nums, k):
max_val = []
result = []
number = set(nums)
for i in number:
max_val.append(nums.count(i))
# max_val = sorted(max_val, reverse=True)
max_val.sort(reverse=True)
print(max_val)
for j in range(k):
for n in number:
if nums.count(n) == max_val[j]:
result.append(n)
if len(result) != k:
return "숫자를 똑바로 입력하세요!!!!"
return sorted(result)
def top_k(nums, k):
my_dict = {}
for i in nums:
if i not in my_dict:
my_dict[i] = 1
else:
my_dict[i] += 1
count_list = sorted(list(my_dict.values()), reverse=True)
result = []
for i in count_list[:k]:
# for key, value in my_dict.items():
# if i == value:
# result.append(key)
max_number = [ key for key, value in my_dict.items() if value == i ]
result += max_number
if len(result) != k:
return False
return result
def top_k(nums, k):
set_nums = set(nums)
result = [(target, nums.count(target)) for target in set_nums]
result.sort(key = lambda x : -x[1])
return [result[i][0] for i in range(k)]
Reference
この問題について([CodeKata] -9), 我々は、より多くの情報をここで見つけました https://velog.io/@kimkrh/CodeKata-9テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol