LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-18-2009, 04:10 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 51
How to create permutations for a wordlist ?


Hello,

Wordlist, ok ... I forgot my password, very bright so john is there.

Is there better script for creating permutations of each word in their wordlist?

I found this code for PHP
Code:
<?php
  // Load the dictionary file
  $dictionary = file_get_contents("d:\wn.txt");
  // Extract into an array
  $words = split("\r\n", $dictionary);
  // Create an empty output string
  $output = "";
  // Loop through each word
  foreach ($words as $word)
  {
    // Test that the word is A-Z only and 4 letters in length
    if (eregi("^[a-z]{4}$", $word))
    {
      // Append to the output string
      $output .= ($word.",");
    }
  }
  // Write the output to file
  file_put_contents("d:\wn_filtered.txt", $output);
?>
my wordlist:
Quote:
linux
is
super
hello
this
is
wordlist

Last edited by frenchn00b; 08-18-2009 at 04:23 PM.
 
Old 08-18-2009, 04:16 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by frenchn00b View Post
I forgot my password, very bright so john is there.
It is probably easier to fix by booting with "init=/bin/bash" on the kernel command line, and give yourself a new password.
 
Old 08-18-2009, 04:16 PM   #3
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 51
so in progress, I need to cat wordlist.lst and pipe it into this code

Code:
/*  You could put your own name in for abcd to
    see if there are any valid word permutations

    Download:
    http://prdownloads.sourceforge.net/cpearls/simple_but_common_cpp.tar.gz?download

    You can find a large dictionary of words at the following link:
    http://prdownloads.sourceforge.net/souptonuts/linuxwords.1.tar.gz?download
*/

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
int main(void)
{
  char a[]="abcd";
  int r=1;
  size_t len = strlen(a);


  cout << a << "\t";
  while(next_permutation(a,a+len)){
    cout << a;
    (r++ % len+2 == len+1) ? cout <<  "\n": cout << "\t";
   }
}
then
Code:
g++ permute.cpp
then try to pipe with ./a.out
 
Old 08-18-2009, 04:17 PM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Hko View Post
It is probably easier to fix by booting with "init=/bin/bash" on the kernel command line, and give yourself a new password.
I recall my root too, but only my username. I would like to learn the real stuff, and leave intact my password. Learning learning ...
 
Old 08-18-2009, 05:09 PM   #5
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 51
I have found a solution with wine
http://www.ab-archive.com/download/i...generator.html
http://www.ab-archive.com/screenshot...generator.html

it would be better in linux and more than 4 fields
 
Old 08-18-2009, 07:03 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
A while ago someone asked how to find all the dictionary words in all permutation of an input string. (I suspect, but don' know if this is so, that the problem was in the context of some game show.) In any case, I wrote the following SQLite code to take a string and create all the (permuted) sub-strings of the input string. For example,

First, here's the code:
Code:
/* Find the next permutation of a string */
#include <stdlib.h>                        
#include <string.h>                        
#include <stdio.h>                         
#include <sqlite3.h>                       
#define debug 0                            
extern FILE * stdin;                       
extern FILE * stdout;                      
extern FILE * stderr;                      
/**********************************************/
/*                                            */
/* SQLite error report helper function        */
/*                                            */
/**********************************************/
int check_code(         // Return 0 is OK, 1 otherwise
  sqlite3 * db,         // Data base information pointer
  sqlite3_stmt *stmt,   // Statement being checked      
  const int rc,         // Value to check               
  const char * text,    // Text to print if check fails 
  const int target)     // SQLite expected return code  
{                                                       
  if (rc != target) {                                   
    fprintf(stderr, "%s: %s\n", text, sqlite3_errmsg(db));
    if (stmt) sqlite3_finalize(stmt);                     
    return 1;                                             
  }                                                       
  return 0;                                               
}                                                         
/**********************************************/          
/*                                            */          
/* Partition a string into all its substrings */          
/*                                            */          
/**********************************************/          
int partition(                  // Return 0 on success, 1 otherwise
    const char * const string,  // String to be partitioned        
    sqlite3 * db)               // Pointer to the SQLite data base to use for storage
{                                                                                     
  long int npart;               // Number of partitions                               
  long int bits;                // Binary value holder                                
  int n;                        // Length of input string                             
  register int pos;             // Character to be next printed                       
  register int k;               // Inner loop index                                   
  long int i, j;                // Current partition number                           
  char * str;                   // Word holder                                        
  sqlite3_stmt *stmt;           // Insert statement holder                            
  char * errmsg;                                                                      
  int rc;                       // SQLite return code holder                          
  char insert[256];                                                                   

  n = strlen(string);
  npart = ((long int) 1) << (n - 1);

  str = (char *) malloc(n*sizeof(char));
  if (!str) {                           
    fprintf(stderr,"partition: Could not allocate %d characters for \"str.\"\n", n);
    return 1;                                                                       
  }                                                                                 

// Find the (next) partition and insert it into the data base if it's longer than 1 character
  j = 0;                                                                                     
  for (i = npart - 1; i >= 0; --i) {                                                         
    bits = i;                                                                                
    pos=0;                                                                                   
    k = 0;                                                                                   
    while (bits) {                                                                           
      str[k++] = string[pos++];                                                              
      if (bits & 1) {                                                                        
        str[k] = (char) 0;                                                                   
// Add the word to the data base if it's long enough                                         
        if (k > 1) {                                                                         
          sqlite3_snprintf(255, insert, "INSERT INTO WORDS (WORD) VALUES(%Q)", str);         
          rc = sqlite3_exec(db, insert, NULL, NULL, &errmsg);                                
          if (check_code(db, stmt, rc, "partition: Problem inserting WORD", SQLITE_OK)) {    
            sqlite3_free(errmsg);                                                            
            sqlite3_close(db);                                                               
            exit (1);                                                                        
          }                                                                                  
        }                                                                                    
// And compute the next word . . .                                                           
        ++j;                                                                                 
        k = 0;                                                                               
      }                                                                                      
      bits = bits / 2;                                                                       
    }                                                                                        
// Finished with the current value of j. Process any remaining characters.                   
    while (pos < n) str[k++] = string[pos++];                                                
    str[k] = (char) 0;                                                                       
    pos = 0;                                                                                 
// Add the last string if it's long enough                                                   
    if (k > 1) {                                                                             
      sqlite3_snprintf(255, insert, "INSERT INTO WORDS (WORD) VALUES(%Q)", str);             
      rc = sqlite3_exec(db, insert, NULL, NULL, &errmsg);                                    
      if (check_code(db, stmt, rc, "partition: Problem inserting WORD", SQLITE_OK)) {        
         sqlite3_free(errmsg);                                                               
         sqlite3_close(db);                                                                  
         exit (1);                                                                           
      }                                                                                      
    }                                                                                        
    bits = --i;                                                                              
    k = 0;                                                                                   
  }                                                                                          
  return 0;                                                                                  
}                                                                                            

int comb(       // -1 If no more permutations,
                // length of string if returned string is unchanged,
                // otherwise index of character exchanged with next one
  char * string,// String whose characters are being permuted          
  int *new)     // Flag. 1 if string is a new string to be permuted, 0 otherwise
{                                                                               
  static int n = 0,                                                             
             i,j,                                                               
             * d = 0,                                                           
             * e = 0,                                                           
             * a = 0;                                                           
  char c;                                                                       
  int k;                                                                        
  // Are we starting a new string?                                              
  if (*new) {                                                                   
    n = strlen(string);                                                         
    // Remove any terminating new line character                                
    if (string[n-1]=='\n') string[--n]=0;                                       
    // Do we have anything left?                                                
    if (n < 2) {                                                                
      fprintf(stderr,"\"%s\" is too short to permute.\n", string);              
      return -1;                                                                
    }                                                                           
    d = malloc(n * sizeof(int));                                                
    e = malloc(n * sizeof(int));                                                
    a = malloc(n * sizeof(int));                                                
    if (!d || !e || !a ) {                                                      
      fprintf(stderr,"comb: Insufficient dynamic memory.\n");                   
      if (d) free(d);                                                           
      if (e) free(e);                                                           
      if (a) free(a);                                                           
      return -1;                                                                
    }                                                                           
    // Initialize the tracking arrays                                           
    for (i = 0; i < n; ++i) {                                                   
      d[i]=0;                                                                   
      e[i]=1;                                                                   
      a[i]=i+1;                                                                 
    }                                                                           
    // Start the exchange with the last character                               
    j = n - 1;                                                                  
    // Set the 'new string" flag off                                            
    *new=0;                                                                     
    return n;                                                                   
  }                                                                             
// If here, not initializing. Find the next permutation, if any                 
  while (1) {                                                                   
    a[j] = a[j] - e[j];                                                         
    if ((a[j] == j + 1) || (a[j]== 0)) {                                        
      e[j]=-e[j];                                                               
      d[j]=1 - d[j];                                                            
      j = j - 1;                                                                
// Are we done?                                                                 
      if (j == 0) {                                                             
        free(d);                                                                
        free(e);                                                                
        free(a);                                                                
        return -1;                                                              
      }                                                                         
    }                                                                           
    else break;                                                                 
  }                                                                             
  k = a[j];                                                                     
  for (i = j + 1; i < n; ++i) {                                                 
    k += d[i];                                                                  
  }                                                                             
  if (string[k-1] != string[k]) {                                               
    c = string[k];                                                              
    string[k] = string[k - 1];                                                  
    string[k - 1] = c;                                                          
    return k - 1;                                                               
  }                                                                             
  // Recursive call to deal with exchange of duplicate characters               
  return comb(string, new);                                                     
}                                                                               

/******************************************************/
/*                                                    */
/* Call-back function used by sqlite3_exec()          */
/*                                                    */
/******************************************************/
static int process_sql( // Return 0 on success          
  sqlite3 *db,          // Data base being processed    
  int argc,             // Number of row values returned by query
  char **argv,          // Data values returned                  
  char **azColName)     // Name of the data columns returned     
{                                                                
  if (argc < 0) return 1;                                        
  if (partition(argv[0], db)) {                                  
    // If we get here we had a data base error. Abort.           
    sqlite3_close(db);                                           
    exit (1);                                                    
  }                                                              
  // Partition the root word                                     
  if (partition(argv[0], db)) {                                  
    // If we get here we had a data base error. Abort.           
    sqlite3_close(db);                                           
    exit (1);                                                    
  }                                                              
  return 0;                                                      
}                                                                

int main(int argc, char **argv)
{                              
  char word[256];       // Current word holder
  char msg[256];        // Potential error message holder
  int i;                                                 
  register int j,k,l;   // Temporary integers (loop index, etc.)
// SQLite stuff                                                 
  sqlite3 *db;                                                  
  sqlite3_stmt *stmt = 0;                                       
  sqlite3_stmt *root = 0;                                       
  int rc;                                                       
  char create[]= "CREATE TABLE WORDS (WORD TEXT PRIMARY KEY ON CONFLICT IGNORE)";
  char select[]= "SELECT WORD FROM WORDS ORDER BY WORD";                         
  char roots[]=  "CREATE TABLE ROOTS (ROOT TEXT PRIMARY KEY ON CONFLICT IGNORE)";
  char getroot[]="SELECT ROOT FROM ROOTS ORDER BY ROOT";                         
  char command[256]; // Command string holder                                    
  char *errmsg = (char *) NULL;                                                  

  // Create a temporary data base to hold the strings found
  rc = sqlite3_open_v2(NULL, &db, 0, errmsg);              
  sprintf(msg, "%s: Could not open an ephemeral SQLite data base", argv[0]);
  if (check_code(db, stmt, rc, msg, SQLITE_OK)) {                           
    close(db);                                                              
    exit(1);                                                                
  }                                                                         
  // Create the table to store the root "words"                             
  rc = sqlite3_exec(db, roots, NULL, NULL, &errmsg);                        
  sprintf(msg, "%s: Could not create table ROOTS", argv[0]);                
  if (check_code(db, stmt, rc, msg, SQLITE_OK)) {                           
    sqlite3_close(db);                                                      
    exit(1);                                                                
  }                                                                         
  // Create the table to store the "words"                                  
  rc = sqlite3_exec(db, create, NULL, NULL, &errmsg);                       
  sprintf(msg, "%s: Could not create table WORDS", argv[0]);                
  if (check_code(db, stmt, rc, msg, SQLITE_OK)) {                           
    sqlite3_close(db);                                                      
    exit(1);                                                                
  }                                                                         
// End SQLite stuff                                                         

// For each input string . ' '
  while (fgets(word, 256, stdin))   {
    // Insert all permutations of "word' into the temporary data base
    i = 1;                                                           
    while (comb(word, &i) >= 0) {                                    
      sqlite3_snprintf(255, command, "INSERT INTO WORDS (WORD) VALUES(%Q)", word);
      rc = sqlite3_exec(db, command, NULL, NULL, &errmsg);
      sprintf(msg, "%s: Problem inserting WORD (\"%s\")", argv[0], word);
      if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
         sqlite3_free(errmsg);
         sqlite3_close(db);
         exit (1);
      }
    }
  }
  // O.K., now all the unique, permuted strings from the input are ready to be partitioned. Do it.
  //
  // Note: The processing is done in the call-back routine, "process_sql"
  //
  rc = sqlite3_exec(db, select, (sqlite3_callback) process_sql, db, &errmsg);
  sprintf(msg, "%s: Problem creating WORDS", argv[0]);
  if (check_code(db, stmt, rc, msg, SQLITE_OK)) {
    sqlite3_free(errmsg);
    sqlite3_close(db);
    exit(1);
  }
  // Write the "words" stored in the data base to the output file
  rc = sqlite3_prepare_v2(db, select, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    fprintf(stderr,"%s: Error preparing %s: %s\n", argv[0], select, sqlite3_errmsg(db));
    sqlite3_finalize(stmt);
    exit(1);
  }
  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
    strncpy(word, (char *) sqlite3_column_text(stmt, 0), 255);
    fprintf(stdout,"%s\n", word);
  }
  sprintf(msg, "%s: Error retrieving data from WORDS", argv[0]);
  check_code( db, stmt, rc, msg, SQLITE_DONE);
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  exit ((rc==SQLITE_DONE) ? 0 : 1);
}
And, if that was compiled to, say substrings, this command:
$ echo averylongstring | ./substrings | hunspell -G
would produce this output (truncated for brevity):
Code:
$ echo averylongstring | ./substrings | hunspell -G
an                                                                     
as                                                                     
at                                                                     
av                                                                     
ave                                                                    
aver                                                                   
avers                                                                  
avert                                                                  
avg                                                                    
ay                                                                     
ea                                                                     
en                                                                     
er                                                                     
erg                                                                    
err                                                                    
es                                                                     
gave                                                                   
gin                                                                    
gist                                                                   
go                                                                     
gong                                                                   
gongs                                                                  
gr                                                                     
grin                                                                   
gs                                                                     
gt                                                                     
in                                                                     
ion                                                                    
is                                                                     
it
. . .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript: Working out permutations Andy@DP Programming 2 02-17-2009 12:56 PM
LXer: Combinations Vs. Permutations on Linux and Unix LXer Syndicated Linux News 0 09-19-2008 03:50 AM
LXer: Lists vs. Strings: Perl List Permutations For Linux Or Unix LXer Syndicated Linux News 0 09-03-2008 04:00 AM
Permutations & recursive Function cdog Programming 3 01-23-2006 09:05 AM
fast algorithm - permutations kev82 Programming 3 08-10-2004 08:09 AM


All times are GMT -5. The time now is 07:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration