Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    anagrams recursion

    I can't use any for or while loops. This is what I have in Python:

    def anagrams(s):
    if s == "":
    return [s]
    else:
    ans = []
    for w in anagrams(s[1:]):
    for pos in range(len(w)+1):
    ans.append(w[pos]+s[0]+w[pos:])
    return ans


    Also how do I convert this code to java? For example, the for loop:

    for w in anagrams(s[1:]):

  • #2
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone? I'm been stuck on this problem for days. I have no idea how to find the anagrams without using for or while loops. Someone please help. I would think that one would need to use a for loop to add each starting letter to each position between the characters of the string. Am I correct?

    Thanks

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Why not use a for loop?

    As for the java for-in loop, the syntax is like so:
    Code:
    // abstractly
    for(T t : TCollectionOrTArray) {
     . . .
    }
    
    // concretely
    String[] strs = new String[] {"a", "ab", "abc"};
    for(String str : strs) {
      System.out.println(str);
    }
    Trinithis

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Can you explain the goal of the program a bit more? What exactly are you trying to do?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •