Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. function invertChar(char) {
  2. return char === "a" ? "b" : "a";
  3. }
  4.  
  5. function last2EqualsChar(str, char) {
  6. return (
  7. str.length >= 2 &&
  8. str[str.length - 1] === char &&
  9. str[str.length - 2] === char
  10. );
  11. }
  12.  
  13. function gen(A, B, acc = "") {
  14. if (A <= 0 && B <= 0) {
  15. return acc;
  16. }
  17.  
  18. let char = A > B ? "a" : "b";
  19.  
  20. char = last2EqualsChar(acc, char) ? invertChar(char) : char;
  21.  
  22. const nextA = char === "a" ? A - 1 : A;
  23. const nextB = char === "b" ? B - 1 : B;
  24.  
  25. return gen(nextA, nextB, acc + char);
  26. };
  27.  
  28.  
  29. describe("Uncle bob approach", () => {
  30. it("should return empty string with empty params", () => {
  31. expect(gen(0, 0)).toBe("");
  32. });
  33.  
  34. it("should be `a` for A = 1 & B = 0", () => {
  35. expect(gen(1, 0)).toBe("a");
  36. });
  37.  
  38. it("should be `b` for A = 0 & B = 1", () => {
  39. expect(gen(0, 1)).toBe("b");
  40. });
  41.  
  42. it("result should have proper length", () => {
  43. expect(gen(1, 1)).toHaveLength(2);
  44. expect(gen(3, 5)).toHaveLength(8);
  45. });
  46.  
  47. it("should use maximum available amount of symbol in a row", () => {
  48. expect(gen(1, 3)).toBe("bbab");
  49. expect(gen(4, 1)).toBe("aabaa");
  50. });
  51. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement