Chapter 18 of 22 Part IV: Information Processing

Finding a Needle in a Digital Universe

4 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

Binary Find (Ch. 18 Example 1)

File: block-01.js (byte-identical) · 406 bytes · SHA-256: 4d33dc8ea6b4…
binary search Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

function binaryFind(items, key) {
  let lo = 0, hi = items.length - 1;
  let checks = 0;
  while (lo <= hi) {
    const mid = Math.floor((lo + hi) / 2);
    checks++;
    if (items[mid] === key)
      return {index: mid, checks};
    if (items[mid] < key) lo = mid + 1;
    else hi = mid - 1;
  }
  return {index: -1, checks};
}
const books = ["A", "B", "C", "D", "E"];
console.log(binaryFind(books, "D"));
Download .js

Words (Ch. 18 Example 2)

File: block-02.js (byte-identical) · 388 bytes · SHA-256: 63d90befac31…
binary search Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).

function words(text) {
  return text.toLowerCase()
    .match(/[a-z0-9]+/g) || [];
}
function makeIndex(docs) {
  const index = new Map();
  for (const doc of docs) {
    const terms = new Set(
      words(doc.title + " " + doc.text));
    for (const word of terms) {
      if (!index.has(word))
        index.set(word, []);
      index.get(word).push(doc.id);
    }
  }
  return index;
}
Download .js

Find All (Ch. 18 Example 3)

File: block-03.js (byte-identical) · 557 bytes · SHA-256: a3d3fa2aa065…
binary search Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

function findAll(index, query) {
  const terms = [...new Set(words(query))];
  if (!terms.length) return [];
  const lists = terms.map(
    t => index.get(t) || []);
  lists.sort((a,b) => a.length-b.length);
  const rest = lists.slice(1)
    .map(a => new Set(a));
  return lists[0].filter(
    id => rest.every(s => s.has(id)));
}
const docs = [
  {id:"A", title:"Saturn rings",
   text:"ice particles orbit Saturn"},
  {id:"C", title:"Ice worlds",
   text:"Saturn moons have ice"}
];
console.log(findAll(makeIndex(docs),
                    "ice rings"));
Download .js

Rank Links (Ch. 18 Example 4)

File: block-04.js (byte-identical) · 559 bytes · SHA-256: db3392da2658…
binary search Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

function rankLinks(graph, steps = 20) {
  const n = graph.length, d = 0.85;
  if (!n) return [];
  let score = Array(n).fill(1/n);
  for (let step=0; step<steps; step++) {
    const next = Array(n).fill((1-d)/n);
    for (let i=0; i<n; i++) {
      const links = graph[i];
      if (links.length) {
        for (const j of links)
          next[j] += d*score[i]/links.length;
      } else {
        for (let j=0; j<n; j++)
          next[j] += d*score[i]/n;
      }
    }
    score = next;
  }
  return score;
}
console.log(rankLinks([[2],[2],[0,3],[2],[]]));
Download .js