Question

In: Computer Science

Create a class that generates permutations of a set of symbols. Requirements The class must be...

Create a class that generates permutations of a set of symbols.


Requirements


The class must be named PermutationGenerator.


The PermutationGenerator class has two methods as follows.
hasNext
This method has no parameters.  It returns true if at least one permutation remains to be generated.
next
This method has no parameters.  It returns an array of the symbols (char[]) in a permutation (if any remain) or null otherwise.


The following main method MUST be used, with NO CHANGES to test your class.

public static void main(String[] args) { int count = 0; PermutationGenerator pg = new PermutationGenerator(new char[] { 'R', 'E', 'G', 'A', 'L' }); while (pg.hasNext()) { count++; char[] permutation = pg.next(); for (char symbol : permutation) System.out.print(symbol + " "); System.out.println(); } System.out.println("Total permutations = " + count); }


Recursion MUST NOT be used.



Solutions

Expert Solution

Here is the program in java

import java.util.ArrayList;
import java.util.List;

class PermutationGenerator{
List<String> permutation;
int currentIdx = 0;
PermutationGenerator(char word[]) {
  
String s = new String(word);
   // create an empty ArrayList to store partial permutations
       permutation = new ArrayList<>();
  
       // initialize the list with the first character of the string
       permutation.add(String.valueOf(s.charAt(0)));
  
       // do for every character of the specified string
       for (int i = 1; i < s.length(); i++)
       {
           // consider previously constructed partial permutation one by one
           // (iterate backwards to avoid ConcurrentModificationException)
           for (int j = permutation.size() - 1; j >= 0 ; j--)
           {
               // remove current partial permutation from the ArrayList
               String str = permutation.remove(j);
              
               // Insert next character of the specified string in all
               // possible positions of current partial permutation. Then
               // insert each of these newly constructed string in the list
  
               for (int k = 0; k <= str.length(); k++)
               {
                   permutation.add(str.substring(0, k) + s.charAt(i) + str.substring(k));
               }
           }
       }
       //System.out.println(permutation);
       //System.out.println(permutation.size());
}
public char[] next() {
  
if((currentIdx+1) <= permutation.size())
return permutation.get(currentIdx++).toCharArray();
return new char[0];
}
public boolean hasNext() {
return (currentIdx < permutation.size());
}
  
}

public class Main
{
   public static void main(String[] args) {
   int count = 0;
   PermutationGenerator pg = new PermutationGenerator(new char[] { 'R', 'E', 'G', 'A', 'L' });
  
   while (pg.hasNext()) {
   count++;
   char[] permutation = pg.next();
   for (char symbol : permutation)
   System.out.print(symbol + " ");
   System.out.println();
   }
  
   System.out.println("Total permutations = " + count);
  
   }
}

The part of Sample output:

It generate 120 permutations of the word. which is correct , because the length of the array is 5 and fact(5) = 120.


Related Solutions

Create a class and name it MyArray. This class must have an internal array of integers...
Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions: 1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows. 2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out)....
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
Assignment requirements You will be creating your custom Set (think a list of unique elements) class...
Assignment requirements You will be creating your custom Set (think a list of unique elements) class in C++. Please do NOT use STL, or any pre-defined library for this assignment. 1. The data type of the set collection: array (or vector). 2. Create three operations (based on Set ADT descriptions -union() -intersection() -difference() 3. Put some test code in main()
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
You shall implement a class named DnaSequence, which models a sequence of DNA. Requirements: You must...
You shall implement a class named DnaSequence, which models a sequence of DNA. Requirements: You must implement all public constructors and methods described in this documentation. Your class must have only 1 field, and it must be a private char[]. No print statements are allowed anywhere in your class. The constructors require you to discard any character that is not 'A', 'C', 'G', or 'T'. This is very easily accomplished using a regular expression in String.replaceAll: // returns a String...
Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Let S be a non-empty set (finite or otherwise) and Σ the group of permutations on...
Let S be a non-empty set (finite or otherwise) and Σ the group of permutations on S. Suppose ∼ is an equivalence relation on S. Prove (a) {ρ ∈ Σ : x ∼ ρ(x) (∀x ∈ S)} is a subgroup of Σ. (b) The elements ρ ∈ Σ for which, for every x and y in S, ρ(x) ∼ ρ(y) if and only if x ∼ y is a subgroup of Σ.
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
In C++, create a class to hold a set of strings called setTree. setTrees contain copies...
In C++, create a class to hold a set of strings called setTree. setTrees contain copies of the strings inserted so that the strings cannot be changed due to the fact that changing the strings inside the set could break the binary search tree. The strings are case sensitive. TreeSet implements the following: bool add(const string& s) -- add s to the set, if it's not already there. Return true if the set changed, false otherwise. void clear() -- remove...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT