Python_Matter/[Check_IO]Home

# Sort Array by Element Frequency

728x90

Quiz>

Sort the given iterable so that its elements end up in the decreasing frequency order,

that is, the number of times they appear in elements.

If two elements have the same frequency,

they should end up in the same order as the first appearance in the iterable.

Input:

Iterable

Output:

Iterable

Example:

``````frequency_sort([4, 6, 2, 2, 6, 4, 4, 4]) == [4, 4, 4, 4, 6, 6, 2, 2]
frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob']) == ['bob', 'bob', 'bob', 'carl', 'alex']``````

Precondition:

elements can be ints or strings The mission was taken from Python CCPS 109 Fall 2018.

It's being taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen

``````def frequency_sort(items):
return None

if __name__ == '__main__':
print("Example:")
print(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4]))

# These "asserts" are used for self-checking and not for an auto-testing
assert list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])) == [4, 4, 4, 4, 6, 6, 2, 2]
assert list(frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob'])) == ['bob', 'bob', 'bob', 'carl', 'alex']
assert list(frequency_sort([17, 99, 42])) == [17, 99, 42]
assert list(frequency_sort([])) == []
assert list(frequency_sort([1])) == [1]
print("Coding complete? Click 'Check' to earn cool rewards!")``````

Solve>

1. 고유 한 키를 찾아 표시된 순서대로 목록에 저장한다.

``````def frequency_sort(items):
keys = []
for item in items:
if not item in keys:
keys.append(item)``````

2. KEY와 FREQ를 튜플 목록으로 변경한다.

``````def frequency_sort(items):
(KEY, FREQ) = (0, 1)
freq_dict = []
for key in keys:
freq_dict.append((key, items.count(key)))``````

3. 딕셔너리를 사용해서 정렬된 목록을 구성하고 freq_dice가 비어있으면 종료된다.

``````def frequency_sort(items):
sorted_list = []
while freq_dict:
highest_freq_pair = ("", 0)
for entry in freq_dict:
if entry[FREQ] > highest_freq_pair[FREQ]:
highest_freq_pair = (entry[KEY], entry[FREQ])

sorted_list += [highest_freq_pair[KEY]] * highest_freq_pair[FREQ]
freq_dict.remove(highest_freq_pair)``````

Code>

``````def frequency_sort(items):
keys = []
for item in items:
if not item in keys:
keys.append(item)

(KEY, FREQ) = (0, 1)
freq_dict = []  # [(KEY, FREQ), ...]
for key in keys:
freq_dict.append((key, items.count(key)))

sorted_list = []
while freq_dict:
highest_freq_pair = ("", 0)
for entry in freq_dict:
if entry[FREQ] > highest_freq_pair[FREQ]:
highest_freq_pair = (entry[KEY], entry[FREQ])

sorted_list += [highest_freq_pair[KEY]] * highest_freq_pair[FREQ]
freq_dict.remove(highest_freq_pair)

return sorted_list``````

Example>

``````if __name__ == '__main__':
print("Example:")
print(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4]))

# These "asserts" are used for self-checking and not for an auto-testing
assert list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])) == [
4, 4, 4, 4, 6, 6, 2, 2]
assert list(frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob'])) == [
'bob', 'bob', 'bob', 'carl', 'alex']
assert list(frequency_sort([17, 99, 42])) == [17, 99, 42]
assert list(frequency_sort([])) == []
assert list(frequency_sort([1])) == [1]
print("Coding complete? Click 'Check' to earn cool rewards!")``````

Result>

``````Example:
[4, 4, 4, 4, 6, 6, 2, 2]
Coding complete? Click 'Check' to earn cool rewards!``````
반응형

#### 'Python_Matter > [Check_IO]Home' 카테고리의 다른 글

 Non-unique Elements  (0) 2020.04.15 2020.04.15 2020.04.15 2020.04.15 2020.04.15 2020.04.15