有更好的替代方法或改进代码的方式吗?

huangapple go评论75阅读模式
英文:

Is there a better alternative or way to improve code?

问题

private static String allCharacters =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@";

public static void main(String[] args) {

    String pass;
    int desiredLength = 3; // Change this to the desired length

    String[] text = allCharacters.split("");

    generateCombinations(text, desiredLength);
}

public static void generateCombinations(String[] characters, int length) {
    generateCombinationsHelper(characters, length, "", 0);
}

public static void generateCombinationsHelper(String[] characters, int length, String current, int currentIndex) {
    if (currentIndex == length) {
        System.out.println(current);
        return;
    }

    for (String character : characters) {
        generateCombinationsHelper(characters, length, current + character, currentIndex + 1);
    }
}

Replace the desiredLength value with the length of pass you want. This updated code uses recursion to generate all combinations of characters of the specified length efficiently, avoiding the need for multiple nested loops.

英文:

I have a string which has uppercase & lowercase letters with numbers and @ symbol which I have split up into individual elements. The goal here was to create this simple brute force code that cycles through the string and returns the element given the counter. It's inefficient and slow.

    private static String allCharacters =
          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@";

    public static void main(String[] args) {

    String pass;
    //split into individual letters
    String[]text = allCharacters.split("");

    for (int counter = 0; counter < text.length; counter++) {
        for (int c2  = 0; c2 < text.length; c2++) {
            pass = text
0
+
网站访问量
+ text[c2]; System.out.println(pass); } } }

I also want to add a certain length instead of using multiple for loops, Any tips ?

答案1

得分: 0

If you want to combine more than 2 characters, it is likely better to use recursion rather than a loop:

如果要组合超过2个字符,最好使用递归而不是循环:

public void generatePossibilities(String prefix, int targetLength) {
if (prefix.length() == targetLength) {
System.out.println(prefix);
return;
}

generatePossibilities(prefix + "a", targetLength);
generatePossibilities(prefix + "b", targetLength);
// you get the idea. Of course, you should use a loop for this
// this is left as an exercise to the reader

}

Two side notes:

两个附带说明:

a) Password brute-forcing is supposed to be slow. If this problem didn't have exponential complexity, passwords would be useless.

a)密码暴力破解应该很慢。如果这个问题没有指数复杂度,密码将毫无用处。

b) Your performance bottleneck is most likely System.out.println. Try dumping the passwords into a file instead, that is likely faster than your terminal.

b)您的性能瓶颈很可能是System.out.println。尝试将密码写入文件,这可能比您的终端速度更快。

英文:

If you want to combine more than 2 characters, it is likely better to use recursion rather than a loop:

public void generatePossibilities(String prefix, int targetLength) {
    if (prefix.length() == targetLength) {
         System.out.println(prefix);
         return;
    }
    
    generatePossibilities(prefix + "a", targetLength);
    generatePossibilities(prefix + "b", targetLength);
    // you get the idea. Of course, you should use a loop for this
    // this is left as an exercise to the reader
}

Two side notes:

a) Password brute-forcing is supposed to be slow. If this problem didn't have exponential complexity, passwords would be useless.

b) Your performance bottleneck is most likely System.out.println. Try dumping the passwords into a file instead, that is likely faster than your terminal.

答案2

得分: 0

Your code now generates all combinations of characters from a given set.
I think the use of String.charAt(int index) is more compact.

for two-letter words it looks like this:

for (int i = 0; i < allCharacters.length(); i++) {
    for (int j = 0; j < allCharacters.length(); j++) {
        System.out.println(allCharacters.charAt(i) + "" + allCharacters.charAt(j));
    }
}

if you need performance, indeed leave out the System.out.println it is very slow.
Also you may want to rewrite to ++i instead of i++, that makes a little difference

If you are really concerned with benchmarks, you would also replace the temporary String object and work directly on a char[] like this

char[] all = allCharacters.toCharArray();
char[] word = new char[2];

for (int i = 0; i < all.length; ++i) {
    for (int j = 0; j < all.length; ++j) {
        word[0] = all[i];
        word[1] = all[j];
        //System.out.println(word);
    }
}

If you need even more speed, then make a lookup table. That means, write all words into the memory once (again as an array), there they are accessible much faster for the rest of your program's execution than generating them on the fly.
Lookup is of course limited by available memory.

But the real question is: is brute-force really necessary? For that to answer, we would need to understand more about the underlying problem.

英文:

Your code now generates all combinations of characters from a given set.
I think the use of String.charAt(int index) is more compact.

for two-letter words it looks like this:

for (int i = 0; i &lt; allCharacters.length(); i++) {
	for (int j = 0; j &lt; allCharacters.length(); j++) {
	System.out.println(allCharacters.charAt(i) +&quot;&quot;+ allCharacters.charAt(j));
	}
}

if you need performance, indeed leave out the System.out.println it is very slow.
Also you may want to rewrite to ++i instead of i++, that makes a little difference

If you are really concerned with benchmarks, you would also replace the temporary String object and work directly on an char[] like this

	char[] all = allCharacters.toCharArray();
	char[] word = new char[2];

	for (int i = 0; i &lt; all.length; ++i) {
		for (int j = 0; j &lt; all.length; ++j) {
			word[0] = all[i];
			word[1] = all[j];
			//System.out.println(word);
		}
	}

If you need even more speed, then make a lookup table. That means, write all words into the memory once (again as an array), there they are accessible much faster for the rest of your program's execution than generating them on the fly.
Lookup is of course limited by available memory.

But the real question is: is brute-force really necessary? For that to answer, we would need to understand more about the underlying problem.

huangapple
  • 本文由 发表于 2020年8月2日 04:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63209968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定