PBT 2021 の到来 - 20 日目 - 解決策


私たちのアルゴリズムは: drawTree でした.

付属している可能性のある一連のプロパティを含む CodeSandbox: https://codesandbox.io/s/advent-of-pbt-day-20-solution-21h7b?file=/src/index.spec.ts&previewwindow=tests

プロパティ 1: 線形トランクを構築する必要があります



for any requested size of tree n
it should render a tree having a linear trunk



高速チェックで書かれています:

it("should build a linear trunc", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      // Remove all the leaves from the tree to only keep the trunk
      const treeWithoutLeaves = tree
        .split("\n")
        .map((level) => level.replace(/[()]/g, " ").trimRight());
      for (const level of treeWithoutLeaves) {
        expect(level.trimLeft()).toEqual("^");
        expect(level).toEqual(treeWithoutLeaves[0]);
      }
    })
  );
});



プロパティ 2: ますます大きなレベルを作成する必要があります



for any requested size of tree n
it should render a tree having levels being larger and larger as we move down in the tree



高速チェックで書かれています:

it("should create larger and larger levels", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      const treeLevels = tree.split("\n").map((level) => level.trim());
      for (let index = 1; index < n; ++index) {
        expect(treeLevels[index]).toContain(treeLevels[index - 1]);
      }
    })
  );
});



プロパティ 3: リーフをあるレベルから次のレベルにオフセットする必要があります



for any requested size of tree n
it should render a tree with leaves being offset from one level to the next one



高速チェックで書かれています:

it("should offset leaves from one level to the next one", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      const treeLevels = tree.split("\n").map((level) => level.trim());
      for (let index = 1; index < n; ++index) {
        expect(treeLevels[index]).toEqual("(" + treeLevels[index - 1] + ")");
      }
    })
  );
});



プロパティ 4: 上部と同じレベルでサイズ 2 のベースを作成する必要があります



for any requested size of tree n
it should render a tree having a base of size 2



高速チェックで書かれています:

it("should create a base of size two with levels identical to the top", () => {
  fc.assert(
    fc.property(fc.integer({ min: 1, max: 1000 }), (n) => {
      // Arrange / Act
      const tree = drawTree(n);

      // Assert
      const treeLevels = tree.split("\n");
      expect(treeLevels).toHaveLength(n + 2);
      expect(treeLevels[n]).toEqual(treeLevels[0]);
      expect(treeLevels[n + 1]).toEqual(treeLevels[0]);
    })
  );
});



他の日に取り上げられたトピックとその解決策を確認するには.

このシリーズの詳細については、ハッシュタグ .