-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathprogram1.py
More file actions
38 lines (26 loc) · 1.15 KB
/
Copy pathprogram1.py
File metadata and controls
38 lines (26 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def sort_string_by_frequency(input_string):
# Create a dictionary to store the frequency of each character in the input string
char_freq = {}
for char in input_string:
if char in char_freq:
char_freq[char] += 1
else:
char_freq[char] = 1
# Sort the characters in the input string by their frequency
sorted_chars = sorted(char_freq, key=lambda x: char_freq[x], reverse=True)
# Create the final string by concatenating the sorted characters
final_string = ''
for char in sorted_chars:
final_string += char * char_freq[char]
return final_string
print(sort_string_by_frequency("abc adfsdfsd"))
def test_sort_string_by_frequency():
# Test case 1: Empty string
assert sort_string_by_frequency("") == ""
# Test case 2: String with one character
assert sort_string_by_frequency("a") == "a"
# Test case 3: String with repeated characters
assert sort_string_by_frequency("aaabbbccc") == "aaabbbccc"
# Test case 4: String with mixed characters
assert sort_string_by_frequency("abcabcabc") == "aaabbbccc"
test_sort_string_by_frequency()