Solved

recursive  permutation method

Posted on 2013-12-15
7
546 Views
Last Modified: 2013-12-17
i try to solve this method but i couldn't reach to anything . this is the Question :

You have an index card with the letter C written on one side, and S on the other. You want to see ALL of the possible ways the card will land if you drop it n times. Write a recursive method that prints each session of dropping the cards with C's and S's. For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:
CCCC
CCCS
CCSC
CCSS
CSCC
CSCS
CSSC
CSSS
SCCC
SCCS
SCSC
SCSS
SSCC
SSCS
SSSC
SSSS
*Note: The possible ways would have to print in this specific order.

AND this is my solve but it's wrong  :

static void permute(int n)
{
   int j;
  char a []={'C','S'};
  int i = n ;
 
   if (i == 0)
       System.out.println("Nothing to print");
   else
   {
       for (j = i; j <= n; j++)
       {
         System.out.println(a);

          permute(n-1);

          String u = a[j] * a[j];
         
}
   }
 
}
0
Comment
Question by:Nossa4ever
  • 3
  • 2
7 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39720450
Somehow, recursion or not, I believe you'd need to keep track of the perms. you'd previously created.
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39721236
public class Permute {
    static int positions = 4;
    static char[] sides = {'C', 'S'};
    static char[] item = new char[positions];
    static long number = 0;
   
    public static void permute(int i) {
        if (i == 0) {
            number++;
            System.out.print(number + " : ");
            for (int k = 0; k < item.length; k++) {
                System.out.print(item[item.length - k - 1]);
            }
            System.out.println();
            return;
        }

        for (char c : sides) {
            item[i - 1] = c;
            permute(i - 1);
        }
    }

    public static void main(String[] args) {
        permute(positions);
    }

}

// ---------------- end of code -------------------------------


The following class does what you want. The key lines are:
for (char c : sides) {
   item[i - 1] = c;
   permute(i - 1);
}

In order to have minimal memory consumption you have to have an array that keeps the current values. When recursion reaches "the bottom" the array that keeps current values is just dumped and the function ends.
"the bottom" is reached when "i" reaches 0.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39721239
Let permute(1) return a list of the Strings "C" and "S"

Then you can write permute(n) as
* Take the result of permute(n-1)
* for each String element in the result of permute(n-1)
       prefix it with "C" and add it to your result list
   for each String element in the result of permute(n-1)
       prefix it with "S" and add it to your result list
   return your result list

Now you can get your desired output by writing:

        for (String each : permute(4)) {
            System.out.println(each);
        }
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 37

Expert Comment

by:zzynx
ID: 39721241
@Valeri: don't post ready-out-of-the-box code for homework questions!
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39721267
@zzynx:
I agree! Probably you are right and this is homework...?! I don't know...?!
Anyway, understanding recursion, if the author of the question is student, is difficult... that's way I thing that "ready" code will be useful for him and will help him to solve next recursion tasks by himself...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39721300
Probably you are right and this is homework...?! I don't know...?!
I think there's no doubt at all that this is a homework/school question.

... understanding recursion (...) is difficult
Difficulty is never a good alibi for posting full blown code in the case of a homework question.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
pairstar challenge 2 42
computer science syllabus 3 52
Groovy:unable to resolve class error 2 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now