-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample3.py
More file actions
46 lines (37 loc) · 965 Bytes
/
example3.py
File metadata and controls
46 lines (37 loc) · 965 Bytes
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
39
40
41
42
43
44
45
46
def sort_string(str):
"""
This function sorts a string based on the following rules:
1. Digits are sorted in ascending order.
2. Letters are sorted in ascending order.
3. No two adjacent characters should be the same.
Args:
str: The string to be sorted.
Returns:
The sorted string.
"""
digits = []
letters = []
for char in str:
if char.isdigit():
digits.append(char)
else:
letters.append(char)
digits.sort()
letters.sort()
sorted_str = "".join(digits + letters)
# Remove duplicate adjacent characters.
i = 0
while i < len(sorted_str) - 1:
if sorted_str[i] == sorted_str[i + 1]:
sorted_str = sorted_str[:i] + sorted_str[i + 2:]
else:
i += 1
return sorted_str
# Example 1
str = "z3b1a2"
print(sort_string(str)) # Output: "1a2b3z"
# Additional test cases
str2 = "a55b1c2"
print(sort_string(str2)) # Output: "1a5bc"
str3 = "d1123"
print(sort_string(str3)) # Output: "123d"