Advertisement
robertmarkbram

Recursion test using permutations of a string

Jun 18th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package com.rmb;
  2.  
  3. import java.util.Random;
  4.  
  5. /**
  6.  * Test of recursion using the idea of a brute force password attack by
  7.  * generating all permutations of a sequence of characters until we find the
  8.  * correct one.
  9.  */
  10. public class RecursionTest {
  11.  
  12.     public static void main(String[] args) {
  13.         RecursionTest rt = new RecursionTest(4);
  14.         rt.checkWord("");
  15.     }
  16.  
  17.     /**
  18.      * @param theMaxLength
  19.      *            how many characters should our target words have
  20.      */
  21.     public RecursionTest(int theMaxLength) {
  22.         maxLength = theMaxLength;
  23.     }
  24.  
  25.     /** Build up words as permutations of all characters in this sequence. */
  26.     private final String seq = "0123456789abcdefghijklmnopqrstuvwxyz";
  27.  
  28.     /** How long should our target word be. */
  29.     private int maxLength = 0;
  30.  
  31.     /** Have we finished? */
  32.     private boolean solved = false;
  33.  
  34.     /**
  35.      * If currentWord is one less than the correct length, go through every
  36.      * letter in the sequence, adding that letter to the word (making it the
  37.      * correct length) and check it to see if we have the correct work (changing
  38.      * solved to true). If currentWord is less than
  39.      * <em>one less than the correct length</em> call this function again for
  40.      * every letter in the sequence.
  41.      *
  42.      * @param currentWord
  43.      *            word to check or build next character onto
  44.      */
  45.     private void checkWord(String currentWord) {
  46.         // If puzzle was already marked as solved, stop.
  47.         if (solved) {
  48.             return;
  49.         }
  50.         // If word is one less than the correct length.
  51.         if (currentWord.length() == (maxLength - 1)) {
  52.             // Go through every letter in the sequence.
  53.             for (int index = 0; index < seq.length(); index++) {
  54.                 // Make a word of the correct length by adding next character
  55.                 // in the sequence.
  56.                 String newPass = currentWord + seq.charAt(index);
  57.                 // Check if it is correct.
  58.                 if (wordCorrect(newPass)) {
  59.                     // It is! Wow, we are done then.
  60.                     solved = true;
  61.                     return;
  62.                 }
  63.             }
  64.         } else {
  65.             // Word is not long enough. Go through every letter in the sequence.
  66.             for (int index = 0; index < seq.length(); index++) {
  67.                 // Add next letter to the word and call this function again.
  68.                 String newPass = currentWord + seq.charAt(index);
  69.                 checkWord(newPass);
  70.             }
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * This check is completely random because this is just testing code. There
  76.      * is a 1 in 300 chance that the word will be correct.
  77.      *
  78.      * @param currentWord
  79.      *            word to check
  80.      * @return true if word is the one we are looking for, false otherwise.
  81.      */
  82.     private boolean wordCorrect(final String currentWord) {
  83.         Random random = new Random();
  84.         if (random.nextInt(300) == 0) {
  85.             System.out.println(currentWord + " correct!");
  86.             return true;
  87.         }
  88.         System.out.println(currentWord + " ...");
  89.         return false;
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement