Skip to content

Instantly share code, notes, and snippets.

@fersilva16
Created May 28, 2022 19:07
Show Gist options
  • Save fersilva16/4c408f0e72f7f820bffd76ddb128a4a5 to your computer and use it in GitHub Desktop.
Save fersilva16/4c408f0e72f7f820bffd76ddb128a4a5 to your computer and use it in GitHub Desktop.
a*b parser
const assert = require('assert');
/**
* @param {string} str
*/
const asb = ([head, ...tail]) => {
if (head === 'a') return asb(tail);
if (head === 'b' && !tail.length) return true;
return false;
};
assert(asb('b') === true);
assert(asb('ab') === true);
assert(asb('aab') === true);
assert(asb('aaab') === true);
assert(asb('aaaab') === true);
assert(asb('aaaaab') === true);
assert(asb('a') === false);
assert(asb('c') === false);
assert(asb('ba') === false);
assert(asb('bb') === false);
assert(asb('bc') === false);
assert(asb('cb') === false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment