Porting Python algorithm to C ++ is another solution

Thanks everyone for the help. Below this post, I am placing a corrected version of both scripts, which now give equal output.


Hello,

I wrote a small generation script in python to generate all possible alphabet combinations in a given length. It works pretty well, but for the reason that I won't be faster, I am trying to port it to C ++.

The problem is that my C ++ code creates too many combinations for one word. Here's my example in python:

./test.py

gives me

aaa
aab
aac
aad
aa
aba
....

while ./test

(C ++ program gives me)

aaa
aaa
aaa
aaa
aa

Here I also get all possible combinations, but I get them twice as often.

Here is the code for both programs:

 #!/usr/bin/env python
 import sys
 #Brute String Generator
 #Start it with ./brutestringer.py 4 6 "abcdefghijklmnopqrstuvwxyz1234567890" ""
 #will produce all strings with length 4 to 6 and chars from a to z and numbers 0 to 9
 def rec(w, p, baseString):
    for c in "abcd":
        if (p<w - 1):
            rec(w, p + 1, baseString + "%c" % c)
         print baseString

 for b in range(3,4):
     rec(b, 0, "")

      

And here's the C ++ code

 #include <iostream>
 using namespace std;
 string chars="abcd";

 void rec(int w,int b,string p){
    unsigned int i;
    for(i=0;i<chars.size();i++){
        if(b < (w-1)){
            rec(w, (b+1), p+chars[i]);
        }
        cout <<  p << "\n"; 
    }
 }


 int main ()
 {
    int a=3, b=0;
    rec (a+1,b, "");
    return 0;
 }

      

Does anyone see my error? I don't have much experience with C ++.

Thank you really


Here's the adjusted version:

C ++

#include <iostream>
using namespace std;
string chars="abcd";

void rec(int w,int b,string p){
    unsigned int i;
    for(i=0;i<chars.size();i++){
        if(b < (w)){
            rec(w, (b+1), p+chars[i]);
        }
    }
    cout << p << "\n";
}


int main ()
{
    rec (3,0, "");
    return 0;
}

      

Python

#!/usr/bin/env python
import sys

def rec(w, b, p):
    for c in "abcd":
        if (b < w - 1):
            rec(w, b + 1, p + "%c" % c)
    print p

rec(4, 0, "")

      

Equal output:

$ ./test > 1
$ ./test.py 3 3 "abcd" "" > 2
$ diff 1 2
$ 

      

+2


a source to share


4 answers


I think the Python code is broken as well, but maybe you won't notice it because there is print

too much space indented (hey, now I've seen a Python program with a one-shot error!)

Shouldn't the output be the only case else

? And the reason the quit happens more often is because you are calling print

/ cout

4 times. I suggest changing the code:



def rec(w, p, baseString):
    if w == p:
        print baseString
    else:
        for ...

      

+1


a source


Just out of curiosity, is this fast enough?

import itertools, string
alphabet = string.lowercase + string.digits
for numchars in (3, 4):
    for x in itertools.product(alphabet, repeat=numchars):
        print ''.join(x)

      



(And make sure you redirect the output to a file while scrolling through huge amounts of text on the screen can be surprisingly slow).

+1


a source


The rec

line p

is printed at each iteration of the loop:

for(i=0;i<chars.size();i++){
    // ...
    cout <<  p << "\n"; 
}

      

The Python code you posted seems to do the same thing, but maybe something is mixing up the indentation? Perhaps you have mixed tabs and spaces in your Python file, resulting in unexpected results?

0


a source


You speak...:

./test.py

gives me

aaa aab

(etc.), but this is not true for the code you posted: instead, you get

aa
aa
aa
aa
a

      

with four repetitions of the leader aa

, etc. etc. Of course you do: you have a statement print baseString

inside the loop for c in "abcd":

, so it is necessarily executed four times. I assume you want to print

exit the loop - and similarly for C ++ code, where you also put the output statement in the loop so that it repeats.

0


a source







All Articles