Rank roles by weighted skills

Not every skill counts the same for a role. Weighting skills by importance gives a fairer match, and a ranking shows which role to aim for first.

What to build

Rank roles by how well a learner's skill levels match their weighted requirements.

Requirements

  • Line 1: the learner's skills as skill=level pairs separated by spaces, with level 0 to 5 (missing skills are 0).
  • Each remaining line is role: skill=weight skill=weight ..., with whole-number weights.
  • A role's match = sum of (weight × min(level, 3) ÷ 3) ÷ sum of weights × 100, so level 3 or more counts as fully meeting a skill.
  • Print the roles from best match to worst (ties keep input order) as role: M% with M to one decimal place, followed by (gaps: a, b) listing skills with level below 3, in the role's order, if there are any.

Input

As described.

Output

One line per role.

Constraints

Up to 20 roles with up to 15 skills each.

Example

Input:

networking=4 linux=3 python=2 web=1
SOC: networking=3 linux=2 logs=2
AppSec: web=3 python=2 review=1

Output:

SOC: 71.4% (gaps: logs)
AppSec: 38.9% (gaps: web, python, review)

Why it works

Capping each level at 3 treats "good enough" as full credit, and weights let the skills that matter most for a role count most, so the ranking points to the role you are closest to, and the gaps say what to improve.

Examples

Example 1 · Input
networking=4 linux=3 python=2 web=1
SOC: networking=3 linux=2 logs=2
AppSec: web=3 python=2 review=1
Expected output
SOC: 71.4% (gaps: logs)
AppSec: 38.9% (gaps: web, python, review)
Example 2 · Input
a=5
Only: a=1
Expected output
Only: 100.0%

Hints

Hint 1

Parse pairs with split("=").

Hint 2

Sort a list of {role, match, gaps} by match, highest first.

Solution

Show one correct solution (try it yourself first)
const levels = new Map();
for (const pair of prompt().trim().split(/\s+/).filter(Boolean)) {
  const [skill, level] = pair.split("=");
  levels.set(skill, Number(level));
}
const roles = [];
let line = prompt();
while (line !== null) {
  if (line.trim()) {
    const at = line.indexOf(":");
    const role = line.slice(0, at).trim();
    let total = 0;
    let got = 0;
    const gaps = [];
    for (const pair of line.slice(at + 1).trim().split(/\s+/).filter(Boolean)) {
      const [skill, w] = pair.split("=");
      const weight = Number(w);
      const level = levels.get(skill) ?? 0;
      total += weight;
      got += (weight * Math.min(level, 3)) / 3;
      if (level < 3) gaps.push(skill);
    }
    roles.push({ role, match: total ? (got / total) * 100 : 0, gaps });
  }
  line = prompt();
}
roles.sort((a, b) => b.match - a.match);
for (const r of roles) console.log(`${r.role}: ${r.match.toFixed(1)}%${r.gaps.length ? ` (gaps: ${r.gaps.join(", ")})` : ""}`);

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Security careers and a learning roadmap →