Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 736425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:36:12+00:00 2026-05-14T07:36:12+00:00

Thank you all for helping. Below this post I put the corrected version’s of

  • 0

Thank you all for helping. Below this post I put the corrected version’s of both scripts which now produce the equal output.


Hello,

I have written a little brute string generation script in python to generate all possible combinations of an alphabet within a given length. It works quite nice, but for the reason I wan’t it to be faster I try to port it to C++.

The problem is that my C++ Code is creating far too much combination for one word.
Heres my example in python:

./test.py

gives me

aaa
aab
aac
aad
aa
aba
....

while ./test (the c++ programm gives me)

aaa
aaa
aaa
aaa
aa

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

Here is the Code for both programms:

 #!/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 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 anybody see my fault ? I don’t have much experience with C++.

Thanks indeed


Here the corrected 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
$ 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:36:12+00:00Added an answer on May 14, 2026 at 7:36 am

    I think the Python code is also broken but maybe you don’t notice because the print is indented by one space too many (hey, now I’ve seen a Python program with a one-off error!)

    Shouldn’t the output only happen in the else case? And the reason why the output happens more often is that you call print/cout 4 times. I suggest to change the code:

    def rec(w, p, baseString):
        if w == p:
            print baseString
        else:
            for ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to thank all the users in this community for helping me
EDIT: I have fixed all but two warnings now, so thank you all for
First, I would to thank everyone for all the help they provide via this
I like to thank stackoverflow member AlbertEin for helping with this code. What I
Again thank you to all who spend a little of their time helping me
First of all, thank you all for helping me with my questions before. Input
Greetings all, First of all thank you all for helping me out with my
First of all, thank you very much in advance for any attempt at helping
thank you all so very much for helping. It's seemingly such an easy task,
Thank you all for your help. A number of you posted (as I should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.