For a randomly chosen password, entropy = length × log₂(pool size), where the pool is every kind of character the password uses.
What to build
Print the pool size and the entropy of each password.
Requirements
- Pools: lower-case letters 26, upper-case letters 26, digits 10, any other character 33. Add the size of each pool the password uses at least once.
- Print
pool P bits Bwith B to one decimal place.
Input
One password per line (it may contain spaces).
Output
One line per password.
Constraints
Passwords have 1 to 64 printable ASCII characters.
Example
Input:
password
Rahim2004
Output:
pool 26 bits 37.6
pool 62 bits 53.6
Why it works
A random password of length L from a pool of P characters has P^L possibilities, and log₂(P^L) = L × log₂(P) bits.
Examples
Example 1 · Input
password Rahim2004
Expected output
pool 26 bits 37.6 pool 62 bits 53.6
Example 2 · Input
P@ssw0rd!
Expected output
pool 95 bits 59.1
Hints
Hint 1
/[a-z]/.test(password) checks for a lower-case letter; /[^A-Za-z0-9]/ finds any other character.
Hint 2
Math.log2(p) gives log₂.
Solution
Show one correct solution (try it yourself first)
let line = prompt();
while (line !== null) {
let pool = 0;
if (/[a-z]/.test(line)) pool += 26;
if (/[A-Z]/.test(line)) pool += 26;
if (/[0-9]/.test(line)) pool += 10;
if (/[^A-Za-z0-9]/.test(line)) pool += 33;
console.log(`pool ${pool} bits ${(line.length * Math.log2(pool)).toFixed(1)}`);
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step