Click here to Skip to main content
15,899,474 members
Home / Discussions / C#
   

C#

 
AnswerRe: keyword "dynamic" - real world applications? Pin
i.j.russell26-Feb-11 10:32
i.j.russell26-Feb-11 10:32 
QuestionRecursive Permutation Generator. Pin
shivamkalra25-Feb-11 13:37
shivamkalra25-Feb-11 13:37 
AnswerRe: Recursive Permutation Generator. Pin
PIEBALDconsult25-Feb-11 14:53
mvePIEBALDconsult25-Feb-11 14:53 
GeneralRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 15:01
sitebuilderLuc Pattyn25-Feb-11 15:01 
GeneralRe: Recursive Permutation Generator. Pin
PIEBALDconsult28-Feb-11 1:46
mvePIEBALDconsult28-Feb-11 1:46 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 16:47
shivamkalra25-Feb-11 16:47 
AnswerRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 15:02
sitebuilderLuc Pattyn25-Feb-11 15:02 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 16:39
shivamkalra25-Feb-11 16:39 
Thank you guys.

I'm very excited right now because I've just finished the question, and I did it all by myself Shucks | :-\ . It took 1/2 hour to think, 1 hour to write and 1 hour to correct it using hit and trial..But finally I succeeded. I'm attaching code below, it is in java but it will still run in C# because all syntax-es are same.


public class PermutationIterator
{
	private String s;
	private int index = 0;
	private PermutationIterator tailIterator = null;
	
	public PermutationIterator(String s)
	{
		this.s = s;
		if(s.length() > 0)
			tailIterator = new PermutationIterator(s.substring(1));
	}
	
	public boolean hasMorePermutations()
	{	
		if(tailIterator == null)
			return false;
		
		if(s.length() == 1)
			return true;
		
		return  index != s.length() - 1 || tailIterator.hasMorePermutations();
	}
	
	public String nextPermutation()
	{
		if(s.length() == 1)
		{
			tailIterator = null;
			return s;
		}
		
		if(!tailIterator.hasMorePermutations())
			tailIterator = new PermutationIterator(s.substring(0, ++index) + 
					s.substring(index + 1));
		
		return s.charAt(index) + tailIterator.nextPermutation();
	}
}


It is still less readable because I just used hit trial thing to correct array out of bound exceptions. I've started to love recursion..There are only few things in world that gives to pain and pleasure and one of them is recursion Big Grin | :-D , lol I just made that up..

Anyways, if you could improve my code and I would really appreciate it.

Thanks
Shivam K
AnswerRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 16:58
sitebuilderLuc Pattyn25-Feb-11 16:58 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 17:22
shivamkalra25-Feb-11 17:22 
GeneralRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 17:32
sitebuilderLuc Pattyn25-Feb-11 17:32 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 17:53
shivamkalra25-Feb-11 17:53 
AnswerRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 17:58
sitebuilderLuc Pattyn25-Feb-11 17:58 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 18:20
shivamkalra25-Feb-11 18:20 
AnswerRe: Recursive Permutation Generator. [modified] Pin
Luc Pattyn25-Feb-11 17:14
sitebuilderLuc Pattyn25-Feb-11 17:14 
QuestionHelp with diagnosing a service failing [modified] Pin
turbosupramk325-Feb-11 11:15
turbosupramk325-Feb-11 11:15 
AnswerRe: Help with diagnosing a service failing Pin
RobCroll25-Feb-11 12:22
RobCroll25-Feb-11 12:22 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk325-Feb-11 12:37
turbosupramk325-Feb-11 12:37 
GeneralRe: Help with diagnosing a service failing Pin
RobCroll25-Feb-11 13:12
RobCroll25-Feb-11 13:12 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk325-Feb-11 18:06
turbosupramk325-Feb-11 18:06 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk328-Feb-11 5:30
turbosupramk328-Feb-11 5:30 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk328-Feb-11 9:12
turbosupramk328-Feb-11 9:12 
GeneralRe: Help with diagnosing a service failing Pin
RobCroll28-Feb-11 11:33
RobCroll28-Feb-11 11:33 
GeneralRe: Help with diagnosing a service failing Pin
RobCroll28-Feb-11 11:29
RobCroll28-Feb-11 11:29 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk328-Feb-11 15:12
turbosupramk328-Feb-11 15:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.