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).

!7

sS testCases;

p {
  testCases = loadSnippet(#1013119);
  L<S> lines = lines(testCases);
  L<Int> l = indexesOfLinesBeginningWithDoubleArrow(lines);
  int i = 0, cases = 0;
  for (int line : l) {
    S question = lines(subList(lines, i, line));
    S answer = trimDropPrefixTrim("=>", lines.get(line));
    i = line+1;
    
    assertContainsCodeTokensIC(answer, callAnswerFunctionVerbose(question));
    ++cases;
  }
  
  print("OK (" + n2(cases, 'case) + ")");
}

answer {
  // parsing
  
  new L<S> parts;
  L<S> l = tok_splitAtSpacedDashes_old(s);
  parts.addAll(tok_splitAtAndFromMiddle(l.get(0)));
  parts.add(l.get(1));
  
  // loop
  
  pnl(parts);
  new L<S> output;
  new Executor e;
  for (S part : parts) {
    print(part);
    e.process(part);
    L<S> out = cloneAndClearList(e.output);
    printLinesWithIndent(" => ", out);
    output.addAll(out);
  }
  try answer lines(output);
}

sclass Executor {
  int length = -1; // unknown
  new L<S> output;
  
  void say(S s) { output.add(s); }
    
  void process(S s) {
    L<S> tok = javaTok(s);
    
    // interpretation

    int i = findCodeTokensIC(tok, "<int>"/*, "elements"*/);
    if (i >= 0) {
      int n = parseInt(tok.get(i));
      S before = get(tok, i-2);
      if (eqic(before, "add")) ret with length += n;
      if (eqic(before, "remove")) ret with length -= n;
      ret with length = n;
    }

    if (hasCodeTokensIC(tok, "empty")) ret with length = 0;
    if (hasCodeTokensIC(tok, "add", "one")) ret with ++length;
    if (hasCodeTokensIC(tok, "remove", "one")) ret with --length;
    
    if (hasCodeTokensIC(tok, "how", "long"))
      say("The list is " + n2(length, "element") + " long");
  }
}

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: 421 / 1278
Version history: 17 change(s)
Referenced in: [show references]