Print String Permutations in Lexicographic Order Using Recursion in Python

In this tutorial, we will learn how to program "Print String Permutations in Lexicographic Order Using Recursion in Python." The objective is to generate all possible permutations of a given string in lexicographic order using recursion. This tutorial will guide you step by step through the entire process of implementing the recursive logic to achieve this task. By the end of this tutorial, you will have a solid understanding of how to generate string permutations effectively, helping you strengthen your problem-solving abilities and enhance your Python coding skills.

This topic is straightforward to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of computing exponentiation. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. from math import factorial
  2.  
  3. def print_permutations_lexicographic_order(s):
  4.     """Print all permutations of string s in lexicographic order."""
  5.     seq = sorted(list(s))  # start with smallest (lexicographic order)
  6.  
  7.     while True:
  8.         print(''.join(seq))
  9.         if not get_next_permutation(seq):
  10.             break
  11.  
  12. def get_next_permutation(seq):
  13.     """Transforms the sequence into the next lexicographic permutation.
  14.       Returns False if there is no next permutation."""
  15.     # Step 1: Find longest non-increasing suffix
  16.     i = len(seq) - 2
  17.     while i >= 0 and seq[i] >= seq[i + 1]:
  18.         i -= 1
  19.     if i == -1:
  20.         return False  # no more permutations
  21.  
  22.     # Step 2: Find successor to pivot
  23.     j = len(seq) - 1
  24.     while seq[j] <= seq[i]:
  25.         j -= 1
  26.     seq[i], seq[j] = seq[j], seq[i]
  27.  
  28.     # Step 3: Reverse suffix
  29.     seq[i + 1:] = reversed(seq[i + 1:])
  30.     return True
  31.  
  32.  
  33. while True:
  34.     print("\n=============== Print String Permutations in Lexicographic Order ===============\n")
  35.  
  36.     s = input('Enter the string: ')
  37.     print_permutations_lexicographic_order(s)
  38.  
  39.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  40.     if opt == 'no':
  41.         print("Exiting program...")
  42.         break
  43.     elif opt != 'yes':
  44.         print("Invalid choice. Exiting program...")
  45.         break

This Python program generates and prints all **permutations of a string in lexicographic order**. It first sorts the characters of the input string to ensure the sequence starts from the smallest lexicographic arrangement. The function `get_next_permutation()` is used to transform the current sequence into the next lexicographic permutation by identifying a pivot point, swapping with the rightmost successor, and reversing the suffix. The main function `print_permutations_lexicographic_order()` keeps printing permutations until no further arrangement is possible. The program runs in a loop where the user inputs a string, all its permutations are displayed, and then the user is prompted whether to try again or exit.

Output:

There you have it we successfully created Print String Permutations in Lexicographic Order Using Recursion in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials