Welcome to Dream.In.Code
Become an Expert!

Join 149,483 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,390 people online right now. Registration is fast and FREE... Join Now!




Combinator

 
Reply to this topicStart new topic

Combinator, Finding permutations

obNiko
18 May, 2007 - 08:26 AM
Post #1

New D.I.C Head
*

Joined: 2 May, 2007
Posts: 7


My Contributions
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:
CODE

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".
CODE

words = {0=>"a",1=>"b",......,n=>"?"};


Please Help.
Thank to the helpers :]

User is offlineProfile CardPM
+Quote Post

spullen
RE: Combinator
18 May, 2007 - 10:19 AM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
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:
CODE

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 - 10:21 AM
User is offlineProfile CardPM
+Quote Post

obNiko
RE: Combinator
18 May, 2007 - 10:21 AM
Post #3

New D.I.C Head
*

Joined: 2 May, 2007
Posts: 7


My Contributions
I tought about recursion but I didnt had an idea about how to use it.
Thanks, Ill try it out.
User is offlineProfile CardPM
+Quote Post

obNiko
RE: Combinator
18 May, 2007 - 11:45 AM
Post #4

New D.I.C Head
*

Joined: 2 May, 2007
Posts: 7


My Contributions
This code doesnt works at Python.
I tried to change it a bit to the python syntex:
CODE

def permutate(s, stringLength):
    if(stringLength != 0):
        s = dropOne(s, stringLength);
        permutate(s, stringLength-1);

But I keep getting this error:
CODE

  File "<pyshell#13>", line 3
    print dropOne(s, stringLength);
        ^
IndentationError: expected an indented block

User is offlineProfile CardPM
+Quote Post

spullen
RE: Combinator
18 May, 2007 - 12:51 PM
Post #5

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
can you post all the code you have.
User is offlineProfile CardPM
+Quote Post

obNiko
RE: Combinator
18 May, 2007 - 11:29 PM
Post #6

New D.I.C Head
*

Joined: 2 May, 2007
Posts: 7


My Contributions
EDIT:
I solved the problem.



Here is the code:
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: 18 May, 2007 - 11:39 PM
User is offlineProfile CardPM
+Quote Post

spullen
RE: Combinator
19 May, 2007 - 07:26 AM
Post #7

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
so you did get it to work with recursion?
User is offlineProfile CardPM
+Quote Post

obNiko
RE: Combinator
28 May, 2007 - 05:00 AM
Post #8

New D.I.C Head
*

Joined: 2 May, 2007
Posts: 7


My Contributions
Not really.
Whats this line do:
CODE


               tempString = string.charAt(i);

because when I run it, its says that there is an error:
CODE

    tempString = string.charAt(i);
AttributeError: 'str' object has no attribute 'charAt'

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month