Combinator

Finding permutations

Page 1 of 1

7 Replies - 3808 Views - Last Post: 28 May 2007 - 06:00 AM Rate Topic: -----

#1 obNiko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-May 07

Combinator

Posted 18 May 2007 - 09:26 AM

Hi guys,

I wish to write a prog. that will take X chars and will present them
in any possiable order.

For example if X=3:
CHAR1 = a
CHAR2 = b
CHAR3 = c

will print:
abc
acb
bac
bca
cab
cba

Here is my function that will input the chars:
def getWords(amount):
	global words;
		amount = int(amount);
	a = 0;
	while a < amount:
		words[a] = raw_input("Enter Word: ");
		a = a+1;



As you can see all the CHARS are in "words".
words = {0=>"a",1=>"b",......,n=>"?"};



Please Help.
Thank to the helpers :]

Is This A Good Question/Topic? 0
  • +

Replies To: Combinator

#2 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Combinator

Posted 18 May 2007 - 11:19 AM

this is the way I would think about this problem. For one I would use recursion, and I would have a method that drops one and changes where it goes.

So here is the psuedocode I came up for it:
permutatedString = ""

permutate(string, stringLength){
	  if(stringLength == 0)
			  //do nothing
	  else
		   generate random number based on length
		   string = dropOne(string, stringLength)
		   permutate(string, stringLength-1)
}

dropOne(string, n)
		  tempString = ""
		  for(int i = 0; i < n; i++)
			  if i != n
				 tempString = string.charAt(i)
		  permutatedString = permutatedString + string.charAt(n)
		  return tempString


hope this helps. I would also google permutation for more in depth info. And you could also do it with an array of char's if you don't want to use a string. And the reasoning behind using recursion is that it looks like a factorial, so stringLength = n, n!

This post has been edited by spullen: 18 May 2007 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 obNiko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-May 07

Re: Combinator

Posted 18 May 2007 - 11:21 AM

I tought about recursion but I didnt had an idea about how to use it.
Thanks, Ill try it out.
Was This Post Helpful? 0
  • +
  • -

#4 obNiko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-May 07

Re: Combinator

Posted 18 May 2007 - 12:45 PM

This code doesnt works at Python.
I tried to change it a bit to the python syntex:
def permutate(s, stringLength):
	if(stringLength != 0):
		s = dropOne(s, stringLength);
		permutate(s, stringLength-1);


But I keep getting this error:
  File "<pyshell#13>", line 3
	print dropOne(s, stringLength);
		^
Indentationerror: expected an indented block


Was This Post Helpful? 0
  • +
  • -

#5 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Combinator

Posted 18 May 2007 - 01:51 PM

can you post all the code you have.
Was This Post Helpful? 0
  • +
  • -

#6 obNiko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-May 07

Re: Combinator

Posted 19 May 2007 - 12:29 AM

EDIT:
I solved the problem.



Here is the code:
>>> def permutate(s, stringLength):
	if(stringLength != 0):
		print dropOne(s, stringLength);
				permutate(s, stringLength-1);

>>> def dropOne(string, n):
		  tempString = "";
		  i = 0;
		  while i < n:
		  
		  if i != n:
			  tempString = string.charAt(i);
			  permutatedString = permutatedString + string.charAt(n);
				  i=i+1;
		  
		  return tempString;

>>>#Test		
>>> permutate("abc",3);
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
	permutate("abc",3);
  File "<pyshell#25>", line 3, in permutate
	print dropOne(s, stringLength);
  File "<pyshell#29>", line 7, in dropOne
	tempString = string.charAt(i);
AttributeError: 'str' object has no attribute 'charAt'



As you can see I edited the functions to Python Syntex.
But still an error apears.

and one more question, the "return tempString;" is a part of the LOOP ?

This post has been edited by obNiko: 19 May 2007 - 12:39 AM

Was This Post Helpful? 0
  • +
  • -

#7 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Combinator

Posted 19 May 2007 - 08:26 AM

so you did get it to work with recursion?
Was This Post Helpful? 0
  • +
  • -

#8 obNiko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-May 07

Re: Combinator

Posted 28 May 2007 - 06:00 AM

Not really.
Whats this line do:

			   tempString = string.charAt(i);


because when I run it, its says that there is an error:
	tempString = string.charAt(i);
AttributeError: 'str' object has no attribute 'charAt'


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1