Python
Python

 

 

Program Overview

This Python program generates all permutations of a given string. A permutation of a string is a rearrangement of its characters. The program utilizes recursion to explore all possible arrangements and prints each unique permutation.

Program Structure

  1. Function Definition: The main function, permute(s), is defined to generate permutations of the string s.
  2. Base Case: If the string is empty, return a list containing an empty string.
  3. Recursive Case: Iterate through each character in the string, fixing one character at a time and recursively generating permutations of the remaining characters.
  4. Combining Results: Combine the fixed character with the permutations of the remaining characters to form new permutations.
  5. Output: Print all unique permutations.

Python Code


def permute(s):
    """
    Generate all permutations of a string.

    Args:
        s (str): The input string for which permutations are to be generated.

    Returns:
        list: A list containing all unique permutations of the input string.
    """
    # Base case: if the string is empty, return a list with an empty string
    if len(s) == 0:
        return [""]
    
    # List to store all permutations
    permutations = []
    
    # Iterate through each character in the string
    for i in range(len(s)):
        # Fix the current character
        current_char = s[i]
        
        # Get the remaining characters after fixing the current character
        remaining_chars = s[:i] + s[i+1:]
        
        # Generate all permutations of the remaining characters
        for p in permute(remaining_chars):
            # Combine fixed character with permutations of the remaining characters
            permutations.append(current_char + p)
    
    return permutations

# Example usage
if __name__ == "__main__":
    input_string = "abc"
    result = permute(input_string)
    print("All permutations of the string '{}':".format(input_string))
    for perm in result:
        print(perm)

How to Run the Program

To run the program, follow these steps:

  1. Ensure you have Python installed on your system.
  2. Copy the code into a file named permutations.py.
  3. Open a terminal and navigate to the directory where the file is saved.
  4. Run the program using the command: python permutations.py.
  5. The output will display all unique permutations of the specified string.

Conclusion

This program effectively generates all permutations of a string using a recursive approach. The simplicity of the code allows for easy understanding and modification to suit various needs. By experimenting with different input strings, users can explore the full range of permutations available.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)