Jump to content

Command line tools to generate combinations ?


 Share

Recommended Posts

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

1 hour ago, jchd said:

Yes, my client claim they discover special method to 'interpret" it and their method can help people match the right couple and find the right employee ~~~

My task is to help generate different combination and match the result with per-defined rules. Therefore I am looking for tools to generate combinations (fast).

 

Regds

LAM Chi-fung

Link to comment
Share on other sites

3 hours ago, CFLam said:

And it make me more difficult to apply  PrintCombi >_<

Do you even need to run more than one PrintCombi?

Sure it’s slower as a single process, but it’s still less than a minute.
But how often do you need to run it?

Code hard, but don’t hard code...

Link to comment
Share on other sites

4 hours ago, JockoDundee said:

Do you even need to run more than one PrintCombi?

Sure it’s slower as a single process, but it’s still less than a minute.
But how often do you need to run it?

I can't apply PrintCombi directly since my input is not consecutive numbers. May be I need not to run 4 process since I tried to reduce the 'n' of nCr now i.e. n not longer 50, can be 49, 47, 45 even 36.

 

Regds

LAM Chi-fung

Link to comment
Share on other sites

5 hours ago, JockoDundee said:

How do you want to provide the starting input?

a comma delimited line in a file?

with command line args?

Yes, something like this e.g. GenCombi input.txt 6

The input file contain the list of numbers and the next parameter is the number per line i.e. nCr's r.

 

Regds

LAM Chi-fung

Link to comment
Share on other sites

On 4/23/2021 at 2:26 PM, CFLam said:

The input file contain the list of numbers and the next parameter is the number per line i.e. nCr's r.

Ok, try this:

#pragma compile(Console,True)
If $CmdLine[0]<>2 Then Exit(ConsoleWrite("Usage: GenCombi [#] [INPUT FILE]"))

Local $sInFile=$CmdLine[1], $sOutFile="out-"& $sInFile,$hOutFile=FileOpen($sOutFile, 2)
Local $aR=StringSplit(StringStripWS(FileRead($sInFile),8),",")
Local $hTimer=TimerInit(), $sResult="", $iR=$ar[0],$iN=$CmdLine[2], $iD=$iR-$iN

ConsoleWrite("Input File:  "& $sinFile &@CRLF& "Running:     "& $iR &"C"& $iN & " ")
ForNest(1,1,$iD+1)
FileClose($hOutFile)
ConsoleWrite(@CRLF&@CRLF &"Run Time:    "& Round(TimerDiff($htimer)/1000,3) &" secs."&  @CRLF &"Output File: "& $sOutFile &@CRLF)

Func ForNest($iLevel, $iStart, $iEnd, $sLine="")

For $i = $iStart To $iEnd
   If $iLevel < $iN Then
      ForNest($iLevel+1,$i+1,$iEnd+1,$sLine & $ar[$i] &",")
   Else
      $sResult&=$sLine & $aR[$i] &@CRLF
   EndIf
Next
If $iLevel<3 Then
    FileWrite($hOutFile, $sResult)
    $sResult=""
    ConsoleWrite(".")
EndIf

EndFunc

Notes:

Needs to be compiled to .exe for ConsoleWrites to show

Usage: GenCombi [Comma Delimited Input File]  [Number Per Line]

Output: out-[INPUT FILE NAME]

The output file name is the input file name prepended with "out-".  So Input file 50C6.txt is output file out-50C6.txt

Its single threaded now, but pretty quick, on my machine runs a 50C6 in under 30 seconds.  Let me know if you want it to run multiple concurrently.

Input File is just one line, e.g. 

1,2,3,4,5,6,7,8,9,10 etc...

I've tested the 50C6 output and spot checked others, but you need to verify.

Edited by JockoDundee

Code hard, but don’t hard code...

Link to comment
Share on other sites

7 hours ago, JockoDundee said:

Ok, try this:

#pragma compile(Console,True)
If $CmdLine[0]<>2 Then Exit(ConsoleWrite("Usage: GenCombi [#] [INPUT FILE]"))

Local $sInFile=$CmdLine[1], $sOutFile="out-"& $sInFile,$hOutFile=FileOpen($sOutFile, 2)
Local $aR=StringSplit(StringStripWS(FileRead($sInFile),8),",")
Local $hTimer=TimerInit(), $sResult="", $iR=$ar[0],$iN=$CmdLine[2], $iD=$iR-$iN

ConsoleWrite("Input File:  "& $sinFile &@CRLF& "Running:     "& $iR &"C"& $iN & " ")
ForNest(1,1,$iD+1)
FileClose($hOutFile)
ConsoleWrite(@CRLF&@CRLF &"Run Time:    "& Round(TimerDiff($htimer)/1000,3) &" secs."&  @CRLF &"Output File: "& $sOutFile &@CRLF)

Func ForNest($iLevel, $iStart, $iEnd, $sLine="")

For $i = $iStart To $iEnd
   If $iLevel < $iN Then
      ForNest($iLevel+1,$i+1,$iEnd+1,$sLine & $ar[$i] &",")
   Else
      $sResult&=$sLine & $aR[$i] &@CRLF
   EndIf
Next
If $iLevel<3 Then
    FileWrite($hOutFile, $sResult)
    $sResult=""
    ConsoleWrite(".")
EndIf

EndFunc

Notes:

Needs to be compiled to .exe for ConsoleWrites to show

Usage: GenCombi [Comma Delimited Input File]  [Number Per Line]

Output: out-[INPUT FILE NAME]

The output file name is the input file name prepended with "out-".  So Input file 50C6.txt is output file out-50C6.txt

Its single threaded now, but pretty quick, on my machine runs a 50C6 in under 30 seconds.  Let me know if you want it to run multiple concurrently.

Input File is just one line, e.g. 

1,2,3,4,5,6,7,8,9,10 etc...

I've tested the 50C6 output and spot checked others, but you need to verify.

Ah............. ahhhh.......... I am working on a C++ program to do this task.... havn't worked on C++ for over 3 years and I spend 2 days to refresh my memory..... after I finished the program, I get the notification from the forum!!!! Your program run 10 seconds on my machine. Thanks a lot. ^_^

Regds

LAM Chi-fung

Link to comment
Share on other sites

1 hour ago, CFLam said:

Ah............. ahhhh.......... I am working on a C++ program to do this task.... havn't worked on C++ for over 3 years and I spend 2 days to refresh my memory..... after I finished the program

No worries.

How long does yours take?  It should scream.  The string cat thing in autoit is the time killer.

Post your code, we can handle it :)

Code hard, but don’t hard code...

Link to comment
Share on other sites

3 hours ago, JockoDundee said:

No worries.

How long does yours take?  It should scream.  The string cat thing in autoit is the time killer.

Post your code, we can handle it :)

I finished it, but most checking are omitted. Its raw, but worked~ ^_^

#include<iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;



void loadCSV(istream& in, vector<int>& data) {

    vector<int>* p = NULL;
    string tmp,dt;
    dt = "";
    stringstream tmpline;

    while (!in.eof()) {
        getline(in, tmp, '\n');                    
        tmpline << tmp;
        size_t count = std::count(tmp.begin(), tmp.end(), ',');
        for (int v=0;v<=count;v++) {
            getline(tmpline, dt, ',');
            data.push_back(stoi(dt));
        }
    }
}

void Combi(int a[], int reqLen, int s, int currLen, bool check[], int l)
{
    int count = 0;
    if (currLen > reqLen)
        return;
    else if (currLen == reqLen) {
        for (int i = 0; i < l; i++) {
            if (check[i] == true) {
                count++;
                cout << a[i];
                if (count < reqLen) {
                   cout << ",";
                }

            }
        }

        cout << "\n";
        return;
    }
    if (s == l) {
        return;
    }
    check[s] = true;
    Combi(a, reqLen, s + 1, currLen + 1, check, l);
    check[s] = false;
    Combi(a, reqLen, s + 1, currLen, check, l);
}
int main(int argc, char* argv[]) {
    int n,i;
    ifstream in(argv[1]);

    if (!in)
        return(EXIT_FAILURE);

    vector<int> data;

    loadCSV(in, data);
    n = data.size();

    bool* check{ new bool[n] {} };
    int* arr{ new int[n] {} };
    for (i = 0; i < n; i++) {
        arr[i] = data[i];
    }
    Combi(arr, stoi(argv[2]), 0, 0, check, n);

    return 0;
}

 

Link to comment
Share on other sites

nice.  interesting that you make the recursive call outside of the for loop, i do it inside.

so do you capture standard out from the command line and redirect to a file?

and how fast for the 50C6?

Edited by JockoDundee

Code hard, but don’t hard code...

Link to comment
Share on other sites

btw, @CFLam, I really like just writing to the Console on every line, it simplifies everything, of course its several times slower, but it is clearer...

#pragma compile(Console,True)
Local $aR=StringSplit(StringStripWS(FileRead($CmdLine[1]),8),","),$iR=$ar[0],$iN=$CmdLine[2],$iD=$iR-$iN

ForNest(1,1,$iD+1)

Func ForNest($iLevel, $iStart, $iEnd, $sLine="")

For $i = $iStart To $iEnd
   If $iLevel < $iN Then
      ForNest($iLevel+1,$i+1,$iEnd+1,$sLine & $ar[$i] &",")
   Else
      ConsoleWrite($sLine & $aR[$i] &@CRLF)
   EndIf
Next

EndFunc

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

10 hours ago, JockoDundee said:

nice.  interesting that you make the recursive call outside of the for loop, i do it inside.

so do you capture standard out from the command line and redirect to a file?

and how fast for the 50C6?

No, just read in the argv[]. I havn't implemented time capture in the program, just use echo:| time to log it. The C++ program run 7~8 seconds. And I redirect the output to a file (lazy enough not implement inside the program ^_^ )

Regds

LAM Chi-fung

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...