Permutation Generator

Discussion in 'Programming General' started by Xeox, Feb 8, 2011.

Permutation Generator
  1. Unread #1 - Feb 8, 2011 at 10:41 AM
  2. Xeox
    Joined:
    Jun 17, 2010
    Posts:
    357
    Referrals:
    0
    Sythe Gold:
    0

    Xeox Forum Addict
    Banned

    Permutation Generator

    I recently made this Permutation Generator to test out recursion in coding, and I thought you guys might want to take a look at it or try it out for yourselves! All you need to do is write in the word (for my computer up to 10 characters) into:
    Replace Exponentia with whatever you want, but dont forget to leave the ""'s!!

    This is found in the PermutationGeneratorDemo class. I know in bluej, a 5 character max is required to see all the permutations of the given string.

    PermutationGeneratorDemo:
    Code:
    /**
     * Write a description of class PermutationGeneratorDemo here.
     * 
     * @author (Mike Perugini) 
     * @version (13_tutorial_2)
     */
    import java.util.ArrayList;
    
    public class PermutationGeneratorDemo
    {
        public static void main(String[] args)
        {
            PermutationGenerator generator = new PermutationGenerator("Exponentia");
            ArrayList<String> permutations = generator.getPermutations();
            for (String s : permutations)
            {
                System.out.println(s);
            }
        }
    }
    
    PermutationGenerator:
    Code:
    /**
     * Write a description of class PermutationGenerator here.
     * 
     * @author (Mike Perugini) 
     * @version (13_tutorial_2)
     */
    import java.util.ArrayList;
    
    public class PermutationGenerator
    {
        private String word;
        
        /**
         * Constructs a permutation generator.
         * @param aWord the word to permute
         */
        public PermutationGenerator(String aWord)
        {
            word = aWord;
        }
        /**
         * Gets all permutations of a given word.
         */
        public ArrayList<String> getPermutations()
        {
            ArrayList<String> result = new ArrayList<String>();
            
            //The empty string has a single permutation: itself
            if (word.length() == 0)
            {
                result.add(word);
                return result;
            }
            //Loop through all character positions
            for (int i = 0; i < word.length(); i++)
            {
                //Form a simpler word by removing the ith character
                String shorterWord = word.substring(0, i) + word.substring(i + 1);
                
                //Generate all permutations of the simpler word
                PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(shorterWord);
                ArrayList<String> shorterWordPermutations = shorterPermutationGenerator.getPermutations();
                
                //Add the removed character to the front of each permutation of the simpler word
                for (String s : shorterWordPermutations)
                {
                    result.add(word.charAt(i) + s);
                }
            }
            //Return all permutations
            return result;
        }
    }
     
  3. Unread #2 - Feb 15, 2011 at 11:55 AM
  4. jamesm201
    Joined:
    Feb 7, 2011
    Posts:
    308
    Referrals:
    0
    Sythe Gold:
    22
    Discord Unique ID:
    804740920511561790
    Discord Username:
    CPT72BUG

    jamesm201 Forum Addict

    Permutation Generator

    Nice....
     
< Make a java diary | Andriod MMO Project >

Users viewing this thread
1 guest


 
 
Adblock breaks this site