Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:50:14+00:00 2026-06-09T20:50:14+00:00

I was thinking about this problem today, and I came with the following pseudocode

  • 0

I was thinking about this problem today, and I came with the following pseudocode (Python 3.2) :

def anagrams( string ):

    for c in string:
      anagram = c + anagram( string - {c} ) # remove the char from its position in the string
      print(anagram)

    return

def main():

    word = "abcd"
    anagrams( word )

    return

However, I’d like to know a pythonic way to do this operation:
anagram = c + anagram( string – {c} )

How could I remove that char from the string? so for example:

"abc" -> 'a' + "bc" -> 'a' + 'b' + "c" -> 'a' + 'b' + 'c' = 'abc'
             + "cb" -> 'a' + 'c' + "b" -> 'a' + 'c' + 'b' = 'acb'
      -> 'b' + "ac" -> 'b' + 'a' + "c" -> 'b' + 'a' + 'c' = 'bac'
             + "ca" -> 'b' + 'c' + "a" -> 'b' + 'c' + 'a' = 'bca'
      -> 'c' + "ba" -> 'c' + 'b' + "a" -> 'c' + 'b' + 'a' = 'cba'
             + "ab" -> 'c' + 'a' + "b" -> 'c' + 'a' + 'b' = 'cab'

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:50:15+00:00Added an answer on June 9, 2026 at 8:50 pm

    Why not just use itertools?

    >>> import itertools
    >>> ["".join(perm) for perm in itertools.permutations("abc")]
    ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
    

    The documentation also contains code how the permutation is done.


    Edit:

    Without itertools:

    def all_perms(elements):
        if len(elements) <=1:
            yield elements
        else:
            for perm in all_perms(elements[1:]):
                for i in range(len(elements)):
                    yield perm[:i] + elements[0:1] + perm[i:]
    
    
    word = "abc"
    print list(all_perms(word))
    

    Without itertools and without generators:

    def all_perms(elements):
        if len(elements) <=1:
            return elements
        else:
            tmp = []
            for perm in all_perms(elements[1:]):
                for i in range(len(elements)):
                    tmp.append(perm[:i] + elements[0:1] + perm[i:])
            return tmp
    

    Result:

    [‘abc’, ‘bac’, ‘bca’, ‘acb’, ‘cab’, ‘cba’]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking about this problem to myself previously, and came up with the
I am thinking about this topcoder problem . Given a string of digits, find
This is more thinker than problem. I have a problem with thinking about how
This question got me thinking about bare strings. When PHP sees a string that's
I am thinking about this problem now for very long. I try to use
I was thinking about this when I ran into a problem using std::ofstream. My
Let me describe the situation, and I'm sure I'm just thinking about this problem
I've been thinking about this problem for a while now and I'm still not
I have been thinking about this problem for long enough but cannot come up
in the last few days I've been thinking about this problem without finding an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.