Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function invertChar(char) {
- return char === "a" ? "b" : "a";
- }
- function last2EqualsChar(str, char) {
- return (
- str.length >= 2 &&
- str[str.length - 1] === char &&
- str[str.length - 2] === char
- );
- }
- function gen(A, B, acc = "") {
- if (A <= 0 && B <= 0) {
- return acc;
- }
- let char = A > B ? "a" : "b";
- char = last2EqualsChar(acc, char) ? invertChar(char) : char;
- const nextA = char === "a" ? A - 1 : A;
- const nextB = char === "b" ? B - 1 : B;
- return gen(nextA, nextB, acc + char);
- };
- describe("Uncle bob approach", () => {
- it("should return empty string with empty params", () => {
- expect(gen(0, 0)).toBe("");
- });
- it("should be `a` for A = 1 & B = 0", () => {
- expect(gen(1, 0)).toBe("a");
- });
- it("should be `b` for A = 0 & B = 1", () => {
- expect(gen(0, 1)).toBe("b");
- });
- it("result should have proper length", () => {
- expect(gen(1, 1)).toHaveLength(2);
- expect(gen(3, 5)).toHaveLength(8);
- });
- it("should use maximum available amount of symbol in a row", () => {
- expect(gen(1, 3)).toBe("bbab");
- expect(gen(4, 1)).toBe("aabaa");
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement