Permutations

Started by
23 comments, last by daerid 19 years, 7 months ago
Quote:Original post by Dmytry
26! is somewhere about 4*1026 , so, i think , anything will take alot of time... say if it can print 108 permutations per second (it certanly can't, i would be surprised if it can print 1000 per second on windows console!)....

Even if it'll print 108 per second, it will take about 100 billions years(alot longer than existence of universe) to show all permutations, if i'm right.[grin]
(edit:had mistake with orders-of-magnitude, fixed.)


...OK, bad example, but when I started it just to see what happened, it did a couple hundred or thousand and then paused for at least 20 minutes before I quit it.
I am the master of ideas.....If only I could write them down...
Advertisement
Quote:the test case is MISSISSIPPI ...

"kkk"


Did this bring to mind images of burning crosses for anyone?
Quote:Original post by Nathaniel Hammen
Quote:Original post by Dmytry
26! is somewhere about 4*1026 , so, i think , anything will take alot of time... say if it can print 108 permutations per second (it certanly can't, i would be surprised if it can print 1000 per second on windows console!)....

Even if it'll print 108 per second, it will take about 100 billions years(alot longer than existence of universe) to show all permutations, if i'm right.[grin]
(edit:had mistake with orders-of-magnitude, fixed.)


...OK, bad example, but when I started it just to see what happened, it did a couple hundred or thousand and then paused for at least 20 minutes before I quit it.

i looked at your code and understood what you mean - you check every combination and it's slower than other methods (except that, for 26 letters it doesn't matter anyway[grin])

How i'd do it:
make an array of unique letters and counts of 'em
(say,for `mississippi' it will be
m 1
i 4
s 4
p 2
,
then in pseudocode(almost compilable)(i can complete if you need)
struct same_letters{unsigned char c;int count;}same_letters arr[26];int arrlength;#define MAX_STRING_SIZE 256char str[MAX_STRING_SIZE];int strlength=0;void recursive_function(){ for(n=0;n<arrlength;n++)// it's possible to speed-up that piece of code but i don't sure it's reasonable.  if(arr[n].count)  {    str[strlength]=arr[n].c;   strlength++;     if(strlength<length_of_original_string){     arr[n].count--;     recursive_function();    arr[n].count++;    } else { printf(str); }     strlength--;   }//for() if}main{Read input;prepare arr[] and arrlength, it's trivial;recursive_function();};

btw, i hope it's not homework?
^^was me.... haven't noticed "Please re-enter your password!"
Python rocks... I just wish it was a little easier to distribute :-\
daerid@gmail.com

This topic is closed to new replies.

Advertisement