Public
Edited
Apr 1, 2024
Paused
1 star
Insert cell
Insert cell
Insert cell
decompressedLength("ADVENT") == 6
Insert cell
decompressedLength("A(1x5)BC") == 7
Insert cell
decompressedLength("(3x3)XYZ") == 9
Insert cell
decompressedLength("A(2x2)BCD(2x2)EFG") == 11
Insert cell
decompressedLength("(6x1)(1x3)A") == 6
Insert cell
Insert cell
Insert cell
decompressedLength(input)
Insert cell
function decompressedLength(string) {
let i = 0;
let totalLength = 0;

const parseUntil = (test) => {
let buf = "";
while (true) {
const c = string[i];

if (c == undefined) {
return buf;
}

if (test(c)) {
i++;
return buf;
}

buf += c;
i++;
}
};

while (i < string.length) {
const chars = parseUntil((c) => c == "(");
totalLength += chars.length;
const countToTake = Number(parseUntil((c) => c == "x"));
const repeats = Number(parseUntil((c) => c == ")"));
totalLength += countToTake * repeats;
i += countToTake;
}

return totalLength;
}
Insert cell
Insert cell
decompressedLength2("(3x3)XYZ") == 9
Insert cell
decompressedLength2("X(8x2)(3x3)ABCY") == "XABCABCABCABCABCABCY".length
Insert cell
decompressedLength2("(27x12)(20x12)(13x14)(7x10)(1x12)A") == 241920
Insert cell
decompressedLength2(
"(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN"
) == 445
Insert cell
Insert cell
decompressedLength2(input)
Insert cell
function decompressedLength2(string) {
let i = 0;
let totalLength = 0;

const parseUntil = (test) => {
let buf = "";
while (true) {
const c = string[i];

if (c == undefined) {
return buf;
}

if (test(c)) {
i++;
return buf;
}

buf += c;
i++;
}
};

const takeCount = (count) => {
let buf = "";
let j = 0;

while (j < count) {
const c = string[i + j];
buf += c;
j++;
}

return buf;
};

while (i < string.length) {
const chars = parseUntil((c) => c == "(");
totalLength += chars.length;
const countToTake = Number(parseUntil((c) => c == "x"));
const repeats = Number(parseUntil((c) => c == ")"));
const substring = takeCount(countToTake);
totalLength += decompressedLength2(substring) * repeats;
i += countToTake;
}

return totalLength;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more