Re: brute force

Discussion in 'C++' started by Stuart Golodetz, Aug 22, 2003.

  1. "rosty" <> wrote in message
    news:bi4vce$591i2$-berlin.de...
    > does anybody know how a brute force algorithm looks like? the result

    should
    > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
    > i imagine there should be a vector of char holding the chars that are

    going
    > to participate in the brute forcing,
    > vector<char> vc(3);
    > vc[0]='a';
    > vc[1]='b';
    > vc[2]='c';//only three char for simplicity
    >
    > then a string that is going to be the pass itself. then an iteration
    > through the vector and adding those chars to the pass string, and an int
    > holding the actual size of the pass (untill the pass reaches its maximum)
    > and then... i just cannot put it all together, is it so difficult, or am i
    > just so stupid?
    > any code example?
    > tia


    Here's code which does what you want for a given width, it won't be very
    hard to modify it to increase the width by one after each pass:

    #include <iostream>
    #include <string>
    #include <vector>

    int main()
    {
    std::string characters = "abc";
    int characterCount = characters.length();

    int width;
    std::cin >> width;
    std::string output(width, characters[0]);
    std::vector<int> indices(width);

    bool done = false;
    while(!done)
    {
    std::cout << output << '\n';
    for(int i=width-1; i>=0; --i)
    {
    if(indices == characterCount - 1)
    {
    indices = 0;
    output = characters[0];
    if(i == 0) done = true;
    }
    else
    {
    ++indices;
    output = characters[indices];
    break;
    }
    }
    }
    return 0;
    }

    If there's anything you don't understand, feel free to ask.

    HTH,

    Stuart.

    P.S. This is just one example of a "brute force" algorithm, there are
    infinitely many others. Brute force just implies trying every possible thing
    (in this case, every sequence of characters) until you get it right.
    P.P.S. Next time, post your attempt at the actual code, it will encourage
    more people to help you. :)
    Stuart Golodetz, Aug 22, 2003
    #1
    1. Advertising

  2. Stuart Golodetz

    rosty Guest

    your code is very interesting, that's all i have to say, i would never think
    of testing an uninitialized value, i mean if(indices == characterCount -
    1). and it works...
    i certanly own you a beer, i have some reading for this weekend
    another beer goes to Karl, still trying to make it in a way of recursion
    :)
    ps. i'm certainly not a good programmer, but! only god knows how much fun i
    have doing this things
    "Stuart Golodetz" <> wrote in message
    news:3f463a46$0$13641$...
    > "rosty" <> wrote in message
    > news:bi4vce$591i2$-berlin.de...
    > > does anybody know how a brute force algorithm looks like? the result

    > should
    > > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
    > > i imagine there should be a vector of char holding the chars that are

    > going
    > > to participate in the brute forcing,
    > > vector<char> vc(3);
    > > vc[0]='a';
    > > vc[1]='b';
    > > vc[2]='c';//only three char for simplicity
    > >
    > > then a string that is going to be the pass itself. then an iteration
    > > through the vector and adding those chars to the pass string, and an int
    > > holding the actual size of the pass (untill the pass reaches its

    maximum)
    > > and then... i just cannot put it all together, is it so difficult, or am

    i
    > > just so stupid?
    > > any code example?
    > > tia

    >
    > Here's code which does what you want for a given width, it won't be very
    > hard to modify it to increase the width by one after each pass:
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    >
    > int main()
    > {
    > std::string characters = "abc";
    > int characterCount = characters.length();
    >
    > int width;
    > std::cin >> width;
    > std::string output(width, characters[0]);
    > std::vector<int> indices(width);
    >
    > bool done = false;
    > while(!done)
    > {
    > std::cout << output << '\n';
    > for(int i=width-1; i>=0; --i)
    > {
    > if(indices == characterCount - 1)
    > {
    > indices = 0;
    > output = characters[0];
    > if(i == 0) done = true;
    > }
    > else
    > {
    > ++indices;
    > output = characters[indices];
    > break;
    > }
    > }
    > }
    > return 0;
    > }
    >
    > If there's anything you don't understand, feel free to ask.
    >
    > HTH,
    >
    > Stuart.
    >
    > P.S. This is just one example of a "brute force" algorithm, there are
    > infinitely many others. Brute force just implies trying every possible

    thing
    > (in this case, every sequence of characters) until you get it right.
    > P.P.S. Next time, post your attempt at the actual code, it will encourage
    > more people to help you. :)
    >
    >
    rosty, Aug 23, 2003
    #2
    1. Advertising

  3. "rosty" <> wrote in message
    news:bi7gir$62flm$-berlin.de...
    > your code is very interesting, that's all i have to say, i would never

    think
    > of testing an uninitialized value, i mean if(indices ==

    characterCount -
    > 1). and it works...


    That's because it's initialized to 0. :)

    Cheers,

    Stuart.

    > i certanly own you a beer, i have some reading for this weekend
    > another beer goes to Karl, still trying to make it in a way of recursion
    > :)
    > ps. i'm certainly not a good programmer, but! only god knows how much fun

    i
    > have doing this things
    > "Stuart Golodetz" <> wrote in message
    > news:3f463a46$0$13641$...
    > > "rosty" <> wrote in message
    > > news:bi4vce$591i2$-berlin.de...
    > > > does anybody know how a brute force algorithm looks like? the result

    > > should
    > > > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
    > > > i imagine there should be a vector of char holding the chars that are

    > > going
    > > > to participate in the brute forcing,
    > > > vector<char> vc(3);
    > > > vc[0]='a';
    > > > vc[1]='b';
    > > > vc[2]='c';//only three char for simplicity
    > > >
    > > > then a string that is going to be the pass itself. then an iteration
    > > > through the vector and adding those chars to the pass string, and an

    int
    > > > holding the actual size of the pass (untill the pass reaches its

    > maximum)
    > > > and then... i just cannot put it all together, is it so difficult, or

    am
    > i
    > > > just so stupid?
    > > > any code example?
    > > > tia

    > >
    > > Here's code which does what you want for a given width, it won't be very
    > > hard to modify it to increase the width by one after each pass:
    > >
    > > #include <iostream>
    > > #include <string>
    > > #include <vector>
    > >
    > > int main()
    > > {
    > > std::string characters = "abc";
    > > int characterCount = characters.length();
    > >
    > > int width;
    > > std::cin >> width;
    > > std::string output(width, characters[0]);
    > > std::vector<int> indices(width);
    > >
    > > bool done = false;
    > > while(!done)
    > > {
    > > std::cout << output << '\n';
    > > for(int i=width-1; i>=0; --i)
    > > {
    > > if(indices == characterCount - 1)
    > > {
    > > indices = 0;
    > > output = characters[0];
    > > if(i == 0) done = true;
    > > }
    > > else
    > > {
    > > ++indices;
    > > output = characters[indices];
    > > break;
    > > }
    > > }
    > > }
    > > return 0;
    > > }
    > >
    > > If there's anything you don't understand, feel free to ask.
    > >
    > > HTH,
    > >
    > > Stuart.
    > >
    > > P.S. This is just one example of a "brute force" algorithm, there are
    > > infinitely many others. Brute force just implies trying every possible

    > thing
    > > (in this case, every sequence of characters) until you get it right.
    > > P.P.S. Next time, post your attempt at the actual code, it will

    encourage
    > > more people to help you. :)
    > >
    > >

    >
    >
    Stuart Golodetz, Aug 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Apps
    Replies:
    3
    Views:
    7,833
    Alvin Bruney [MVP - ASP.NET]
    May 26, 2005
  2. David List

    Re: brute force

    David List, Aug 22, 2003, in forum: C++
    Replies:
    4
    Views:
    606
    Karl Heinz Buchegger
    Aug 22, 2003
  3. Bas

    Brute force sudoku cracker

    Bas, Sep 16, 2005, in forum: Python
    Replies:
    21
    Views:
    3,215
    Dennis Lee Bieber
    Sep 23, 2005
  4. ago
    Replies:
    11
    Views:
    685
    Anton Vredegoor
    Jan 20, 2006
  5. Replies:
    8
    Views:
    449
Loading...

Share This Page