feat: added comments to P-1.29

This commit is contained in:
baphim
2026-09-30 16:30:12 -04:00
parent d23b39d984
commit e7682164d6
+5 -2
View File
@@ -2,12 +2,15 @@
Write a Python program that outputs all possible strings formed by using the characters 'c', 'a', 't', 'd', 'o', and 'g' Write a Python program that outputs all possible strings formed by using the characters 'c', 'a', 't', 'd', 'o', and 'g'
exactly once. exactly once.
""" """
# define 'permute' function
def permute(char_pool, current_str): def permute(char_pool, current_str):
# base case: if char_pool is empty, print value of 'current_str'
if len(char_pool) == 0: if len(char_pool) == 0:
print((current_str)) print(current_str)
return return
# try each remaining character as the next pick
for i in range(len(char_pool)): for i in range(len(char_pool)):
# pick character at i: pass down pool without it, and string with it added
permute(char_pool[:i] + char_pool[i+1:], current_str + char_pool[i]) permute(char_pool[:i] + char_pool[i+1:], current_str + char_pool[i])