# Combinator

Page 1 of 1

## 7 Replies - 3878 Views - Last Post: 28 May 2007 - 06:00 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=28186&amp;s=00cc0c41fc38f819efc2d547fd02608a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 obNiko

Reputation: 0
• 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=>"?"};

```

Thank to the helpers :]

Is This A Good Question/Topic? 0

## Replies To: Combinator

### #2 spullen

• D.I.C Regular

Reputation: 10
• 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

### #3 obNiko

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

## Re: Combinator

Posted 18 May 2007 - 11:21 AM

Thanks, Ill try it out.

### #4 obNiko

Reputation: 0
• 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

```

### #5 spullen

• D.I.C Regular

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

## Re: Combinator

Posted 18 May 2007 - 01:51 PM

can you post all the code you have.

### #6 obNiko

Reputation: 0
• 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

### #7 spullen

• D.I.C Regular

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

## Re: Combinator

Posted 19 May 2007 - 08:26 AM

so you did get it to work with recursion?

### #8 obNiko

Reputation: 0
• 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'

```