August 2020
Intermediate to advanced
508 pages
11h 53m
English
You’re a hacker (an ethical one, of course) who’s trying to figure out someone’s password. You decide on a brute-force approach, and write some code that produces every possible string of a given length. Here’s the code you whipped up:
| | def every_password(n) |
| | (("a" * n)..("z" * n)).each do |str| |
| | puts str |
| | end |
| | end |
The way this works is you pass in a number to the function, which becomes the variable n.
If n is 3, for example, the code "a" * n produces the string "aaa". The code then sets a loop between all possible strings within the range of "aaa" and "zzz". Running this code will print:
| | aaa |
| | aab |
| | aac |
| | aad |
| | aae |
| | |
| | ... |
| | |
| | zzx |
| | zzy |
| | zzz |
If n is 4, your code will print all possible strings of length 4:
| |
Read now
Unlock full access