-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
41 lines (34 loc) · 1.29 KB
/
utils.py
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
def find_combinations(s, prefix="", index=0):
"""
Recursively find every combination of letters in a string from left to right
starting with the first character.
Args:
- s: The input string.
- prefix: The current combination being built.
- index: The current index in the string.
Returns:
- result: A list containing all combinations starting with the first character
and excluding the empty string, sorted by shortest string first.
"""
result = []
if index == len(s):
if prefix:
result.append(prefix)
return result
# Include the current character only if it's the first character or part of a previous combination
if index == 0 or prefix:
result.extend(find_combinations(s, prefix + s[index], index + 1))
# Exclude the current character
result.extend(find_combinations(s, prefix, index + 1))
return result
def find_all_combinations(string):
if len(string) == 1:
return [string]
else:
combos = []
for i, char in enumerate(string):
remaining_chars = string[:i] + string[i + 1 :]
sub_combos = find_all_combinations(remaining_chars)
for sub_combo in sub_combos:
combos.append(char + sub_combo)
return combos