def lexicographic_permutations(s, current=""):
if len(s) == 0:
print(current)
else:
for i in range(len(s)):
lexicographic_permutations(s[:i] + s[i+1:], current + s[i])
def permutations_in_lexicographic_order(string):
sorted_string = ''.join(sorted(string))
lexicographic_permutations(sorted_string)
string = input("Enter a string: ")
print("Permutations in lexicographic order:")
permutations_in_lexicographic_order(string)
#source code --> clcoding.com
Code Explanation:
def permutations_in_lexicographic_order(string):
Definition:
This function takes a single parameter:
string: The input string for which permutations need to be generated.
sorted_string = ''.join(sorted(string))
Sorting the String:
sorted(string) converts the string into a sorted list of characters (e.g., "cba" → ["a", "b", "c"]).
''.join(...) joins the sorted characters back into a string ("abc").
Sorting ensures that permutations start from the smallest lexicographic order.
lexicographic_permutations(sorted_string)
Calling the Recursive Function:
The sorted string is passed to lexicographic_permutations to generate and print permutations.
3. Input and Execution
string = input("Enter a string: ")
Taking User Input:
The program prompts the user to enter a string.
This string will be used to generate permutations.
print("Permutations in lexicographic order:")
Message Display:
Prints a message indicating that the program will display permutations.
permutations_in_lexicographic_order(string)
Generating Permutations:
Calls the permutations_in_lexicographic_order function with the user-provided string.
0 Comments:
Post a Comment