# Write a recursive method about: Arrangements. Suppose we have a collection (i.
ID: 3626735 • Letter: #
Question
# Write a recursive method about: Arrangements.Suppose we have a collection (i.e. a set) of numbers:
1, 2, 3, 4
and we want to find all possible arrangements of these numbers. We'll define an arrangement of our set as a list of numbers consisting of every member of our set exactly once. So,
1, 2, 4, 3
and
2, 3, 4, 1
are valid arrangements. However,
2, 2, 4, 3
and
5, 1, 2, 3
are not valid arrangements. For our purposes, we'll treat our set of numbers as a Set of Integers and each specific arrangement as an array of ints.
Write the method
public Set<int[]> generateAllArrangements(Set<Integer> set)
that returns the set of all possible arrangements of the given set of Integers.
Hints:
# How many valid arrangements are there for a set of size n? Why? How does this reasoning help us construct an algorithm to actually generate all the arrangements?
# How would you solve this problem yourself by hand? How do you know you got the right answer?
# You may find it helpful to use helper method(s) to solve this problem. You can do whatever you want as long as your solution is a recursive one (i.e. you can call a recursive helper method).
# You are allowed to modify the input set if that helps you solve this problem, but you aren't required to.
Explanation / Answer
please rate - thanks
using strings, but should start you
import static java.lang.System.out;
import java.util.Scanner;
public class recursivePermutation
{ public static void permute(String prefix, String word)
{int i;
String beginning, middle,end;
if (word.length() <= 1)
out.println(prefix + word);
else
{for(i=0;i< word.length();i++)
{middle = word.substring(i, i + 1);
beginning = word.substring(0, i);
end = word.substring(i + 1);
permute(prefix+middle,beginning+end);
}
}
}
public static void main(String[] args)
{ Scanner scanner = new Scanner(System.in);
String word="1234";
permute("", word);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.