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

72
LINES

< > BotCompany Repo | #1013113 // How long is the list [OK]

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

Download Jar. Libraryless. Click here for Pure Java version (23169L/163K).

1  
!7
2  
3  
sS testCases;
4  
5  
p {
6  
  testCases = loadSnippet(#1013119);
7  
  L<S> lines = lines(testCases);
8  
  L<Int> l = indexesOfLinesBeginningWithDoubleArrow(lines);
9  
  int i = 0, cases = 0;
10  
  for (int line : l) {
11  
    S question = lines(subList(lines, i, line));
12  
    S answer = trimDropPrefixTrim("=>", lines.get(line));
13  
    i = line+1;
14  
    
15  
    assertContainsCodeTokensIC(answer, callAnswerFunctionVerbose(question));
16  
    ++cases;
17  
  }
18  
  
19  
  print("OK (" + n2(cases, 'case) + ")");
20  
}
21  
22  
answer {
23  
  // parsing
24  
  
25  
  new L<S> parts;
26  
  L<S> l = tok_splitAtSpacedDashes_old(s);
27  
  parts.addAll(tok_splitAtAndFromMiddle(l.get(0)));
28  
  parts.add(l.get(1));
29  
  
30  
  // loop
31  
  
32  
  pnl(parts);
33  
  new L<S> output;
34  
  new Executor e;
35  
  for (S part : parts) {
36  
    print(part);
37  
    e.process(part);
38  
    L<S> out = cloneAndClearList(e.output);
39  
    printLinesWithIndent(" => ", out);
40  
    output.addAll(out);
41  
  }
42  
  try answer lines(output);
43  
}
44  
45  
sclass Executor {
46  
  int length = -1; // unknown
47  
  new L<S> output;
48  
  
49  
  void say(S s) { output.add(s); }
50  
    
51  
  void process(S s) {
52  
    L<S> tok = javaTok(s);
53  
    
54  
    // interpretation
55  
56  
    int i = findCodeTokensIC(tok, "<int>"/*, "elements"*/);
57  
    if (i >= 0) {
58  
      int n = parseInt(tok.get(i));
59  
      S before = get(tok, i-2);
60  
      if (eqic(before, "add")) ret with length += n;
61  
      if (eqic(before, "remove")) ret with length -= n;
62  
      ret with length = n;
63  
    }
64  
65  
    if (hasCodeTokensIC(tok, "empty")) ret with length = 0;
66  
    if (hasCodeTokensIC(tok, "add", "one")) ret with ++length;
67  
    if (hasCodeTokensIC(tok, "remove", "one")) ret with --length;
68  
    
69  
    if (hasCodeTokensIC(tok, "how", "long"))
70  
      say("The list is " + n2(length, "element") + " long");
71  
  }
72  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1013113
Snippet name: How long is the list [OK]
Eternal ID of this version: #1013113/18
Text MD5: 8709e90cb926e48e5b160940d4da15c8
Transpilation MD5: 4f33591f7b900a35851ed593bede0737
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-11 17:09:19
Source code size: 1755 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 432 / 1297
Version history: 17 change(s)
Referenced in: [show references]