포스트

GPT 정말 똑똑함

내가 만든 규칙을 간파해버림

코드도 만드는 걸 도와줌.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
enum Relationship { Right, Down }

class PGElement {
  static int _idCounter = 1;  // Global ID counter to assign unique IDs to elements
  final int id;
  String composition; // Describes how this element was composed from other elements
  PGElement? right;
  PGElement? down;

  PGElement({String? composition})
      : this.id = _idCounter++,
        this.composition = composition ?? 'E';

  PGElement addRight(PGElement element) {
    right = element;
    // 두 element를 각각 encapsulate() 함수로 괄호로 묶기.
    String leftC = encapsulate(composition);
    String rightC = encapsulate(element.composition);
    composition = '${leftC}R$rightC';
    return this; // Return this element for chaining
  }

  PGElement addDown(PGElement element) {
    down = element;
    // 두 element를 각각 encapsulate() 함수로 괄호로 묶기.
    String upC = encapsulate(composition);
    String downC = encapsulate(element.composition);
    composition = '${upC}D$downC';
    return this; // Return this element for chaining
  }

  String encapsulate(String str) {
    // 'E'가 아니면 괄호로 묶어서 반환
    return str != 'E' ? '($str)' : str;
  }  

  // Print the composition of this element
  void printComposition() {
    print(composition);
  }
}

void main() {
  // Example usage:
  var e1 = PGElement();
  var e2 = PGElement();
  var e3 = PGElement();
  var e4 = PGElement();
  var e5 = PGElement();

  var layout = e1.addRight(e2).addDown(e3).addRight(e4);

  layout = e5.addDown(layout);

  // Print the composition of the layout
  layout.printComposition();  // Output: ED(((ERE)DE)RE)

}

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.