Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

122
LINES

< > BotCompany Repo | #1001685 // Clock times (works!)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (949L/7K/21K).

1  
!747
2  
!multi-line strings
3  
4  
m {
5  
  static S input = [[
6  
    // Dogmas
7  
    
8  
    A "clock time" consists of "minutes" and "seconds".
9  
    "Minutes" is a number between 0 and 99.
10  
    "Seconds" is a number between 0 and 59.
11  
    A "clock time" is usually written in the format "Minutes:Seconds".
12  
    
13  
    // Questions
14  
    
15  
    What are the minutes in the "clock time" "12:35"?
16  
    What are the seconds in the "clock time" "05:12"?
17  
  ]];
18  
  
19  
  !include #2000515 // unquote
20  
  !include #1000709 // formatSnippetID
21  
  
22  
  static class Struct {
23  
    new L<S> components;
24  
    S format;
25  
    
26  
    Map<S, O> parse(S s) {
27  
      L<S> tok = nlTok(s);
28  
      L<S> form = nlTok(format);
29  
      if (l(tok) != l(form)) ret null;
30  
      new Map<S, O> map;
31  
      for (int i = 1; i < l(form); i += 2) {
32  
        String f = form.get(i), t = tok.get(i);
33  
        if (components.contains(simplifyName(f))) {
34  
          map.put(simplifyName(f), t);
35  
        } else
36  
          if (!eq(f, t))
37  
            ret null;
38  
      }
39  
      ret map;
40  
    }
41  
  }
42  
  
43  
  static class Number {
44  
    int min, max;
45  
    
46  
    boolean checkString(S s) {
47  
      int i = parseInt(s);
48  
      ret i >= min && i <= max;
49  
    }
50  
  }
51  
  
52  
  static new Map<S, O> concepts;
53  
54  
  p {
55  
    for (S line : toLinesFullTrim(input))
56  
      print(answer(line));
57  
  }
58  
  
59  
  static S answer(S line) {
60  
    L<S> tok = nlTok(line);
61  
    if (l(tok) == 1) ret "...";
62  
    simplify(tok);
63  
    S s = join(tok);
64  
    print("  " + s);
65  
    
66  
    new Matches m;
67  
    
68  
    // Dogmas
69  
    
70  
    if (match3("a * consists of * and *", s, m)) {
71  
      S name = simplifyName(m.unq(0));
72  
      S a = simplifyName(m.unq(1));
73  
      S b = simplifyName(m.unq(2));
74  
      new Struct st;
75  
      st.components.addAll(litlist(a, b));
76  
      concepts.put(name, st);
77  
      ret "OK. " + name;
78  
    }
79  
    
80  
    if (match3("* is a number between * and *", s, m)) {
81  
      new Number n;
82  
      n.min = parseInt(m.get(1));
83  
      n.max = parseInt(m.get(2));
84  
      S name = simplifyName(m.unq(0));
85  
      concepts.put(name, n);
86  
      ret "OK. " + name;
87  
    }
88  
    
89  
    if (match3("A * is written in the format *", s, m)) {
90  
      S name = simplifyName(m.unq(0));
91  
      S format = m.unq(1);
92  
      O concept = concepts.get(name);
93  
      setOpt(concept, "format", format);
94  
      ret "OK.";
95  
    }
96  
    
97  
    // Questions
98  
    
99  
    if (match3("What are the * in the * *?", s, m)) {
100  
      S component = simplifyName(m.unq(0));
101  
      S name = simplifyName(m.unq(1));
102  
      S pattern = m.unq(2);
103  
      O concept = concepts.get(name);
104  
      Map<S, O> instance = (Map) call(concept, "parse", pattern);
105  
      ret structureOrText(instance.get(component));
106  
    }
107  
    
108  
    ret "huh?";
109  
  }
110  
  
111  
  static S simplifyName(S s) {
112  
    ret join(" ", codeTokensOnly(tokensToLowerCase(nlTok(s))));
113  
  }
114  
  
115  
  static void simplify(L<S> tok) {
116  
    for (int i = 1; i < l(tok); )
117  
      if (equalsIgnoreCase(tok.get(i), "usually")) {
118  
        removeToken(tok, i);
119  
      } else
120  
        i += 2;
121  
  }
122  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001685
Snippet name: Clock times (works!)
Eternal ID of this version: #1001685/1
Text MD5: 65c3b5df6bb2662741c3cd081d5736ab
Transpilation MD5: af715bef68534727f447cf97056b9b5a
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-11-04 21:05:24
Source code size: 3024 bytes / 122 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 673 / 668
Referenced in: [show references]