Advertisement
dimipan80

Exams - Listy

Dec 28th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* One day @john55 decided to make his own programming language called Listy which will do Excel’s calculations
  2. with a list of numbers. His conception for now is very simple. He can assign lists to a variables,
  3. get the min or max value from a list, get average or sum the elements of the list. Each of the functions
  4. gets as parameters list of numbers or variables in square brackets.”def” is a keyword used to define a variable.
  5. Everything looks great? Right? But @john55 has some problem with his dog “Sharo” and he doesn’t have the time
  6. to make an interpreter for Listy. Help him by writing Listy interpreter in JavaScript, because tomorrow morning
  7. he has meeting with new investors, who wants to use his project for calculation in Boza’s production.
  8. There could be more than one or no whitespace between the characters.
  9. Also you can use old functions in the definitions of the new one.
  10. There will be only a sequence of numbers and variables in the definition of a new variable.
  11. There will be no nested commands in the given command.
  12. Example: Command can be “def var sum[1,2,3]” but it won’t be “def var sum[1,2,3, min[var0, 3,-5,2]]”.
  13. If you meet a variable in a command it’ll be always defined in some of the lines before!
  14. If there is no operation on the last line, command will looks like “[var1]”. Otherwise if there is a final command
  15. it’ll be in format: “sum[var1,var2]” (or other operation).
  16. You are given an array of strings (commands).
  17. Execute all the commands and print the result only from the last line!*/
  18.  
  19. "use strict";
  20.  
  21. function solve(args) {
  22.     var defFunctions = [];
  23.     for (var i = 0; i < args.length; i += 1) {
  24.         var currentRow = args[i].trim();
  25.         currentRow = currentRow.replace(/\s+/g, ' ');
  26.         var j = 0;
  27.         var funcObject = { 'name': undefined, 'command': '', 'value': undefined };
  28.         var indexEnd;
  29.         if (currentRow.indexOf('def ') === 0) {
  30.             j += 4;
  31.             if (currentRow[j] != '[' && currentRow[j] != ',') {
  32.                 indexEnd = currentRow.indexOf('[', j);
  33.                 var funcStr = (currentRow.substring(j, indexEnd)).trim();
  34.                 funcStr = funcStr.split(/\s+/).filter(Boolean);
  35.                 funcObject.name = funcStr[0];
  36.                 if (funcStr.length == 2) {
  37.                     funcObject.command = funcStr[1];
  38.                 }
  39.                 j = indexEnd;
  40.             }
  41.         } else if (i === args.length - 1) {
  42.             if (currentRow[j] != '[' && currentRow[j] != ',') {
  43.                 indexEnd = currentRow.indexOf('[', j);
  44.                 funcStr = (currentRow.substring(j, indexEnd)).trim();
  45.                 funcObject.command = funcStr;
  46.                 j = indexEnd;
  47.             }
  48.         }
  49.  
  50.         if (currentRow[j] == '[' || currentRow[j + 1] == '[') {
  51.             j += 1;
  52.             indexEnd = currentRow.indexOf(']', j);
  53.             var params = (currentRow.substring(j, indexEnd)).trim();
  54.             params = params.split(/[, ]+/).filter(Boolean);
  55.             var paramsList = [];
  56.             for (var k = 0; k < params.length; k += 1) {
  57.                 if (isFinite(params[k])) {
  58.                     paramsList.push(Number(params[k]));
  59.                 } else {
  60.                     var temp = getOldDefinedFuncValue(defFunctions, params[k]);
  61.                     if (temp instanceof Array) {
  62.                         Array.prototype.push.apply(paramsList, temp);
  63.                     } else {
  64.                         paramsList.push(temp);
  65.                     }
  66.                 }
  67.             }
  68.  
  69.             if (paramsList.length > 1) {
  70.                 funcObject.value = calculateFuncCommandValue(paramsList, funcObject.command);
  71.             } else {
  72.                 funcObject.value = paramsList[0];
  73.             }
  74.  
  75.             if (i === args.length - 1) {
  76.                 return funcObject.value;
  77.             }
  78.         }
  79.  
  80.         defFunctions.push(funcObject);
  81.     }
  82.  
  83.     function getOldDefinedFuncValue(objArray, funcNameStr) {
  84.         for (var i = 0; i < objArray.length; i += 1) {
  85.             var object = objArray[i];
  86.             if (object.name === funcNameStr) {
  87.                 return object.value;
  88.             }
  89.         }
  90.     }
  91.  
  92.     function calculateFuncCommandValue(paramsList, command) {
  93.         switch (command) {
  94.             case 'min':
  95.                 return Math.min.apply(null, paramsList);
  96.             case 'max':
  97.                 return Math.max.apply(null, paramsList);
  98.             case 'sum':
  99.                 var sum = 0;
  100.                 for (var i = 0; i < paramsList.length; i += 1) {
  101.                     sum += paramsList[i];
  102.                 }
  103.                 return sum;
  104.             case 'avg':
  105.                 sum = 0;
  106.                 for (i = 0; i < paramsList.length; i += 1) {
  107.                     sum += paramsList[i];
  108.                 }
  109.                 return parseInt((sum / paramsList.length));
  110.             default:
  111.                 return paramsList;
  112.         }
  113.     }
  114. }
  115.  
  116. console.log(solve([
  117.     'def   var1 [ 1 , 2 , 6 , 8 ]',
  118.     'def var2 sum[1, 5, -10, 20]',
  119.     'def var3 max[5, 2, 4, var2, 2]',
  120.     'def var4 min[var1, 6, 50]',
  121.     'def var5 avg[var1] ',
  122.     'def var6 sum[var1, var1, 1]'
  123. ]));
  124.  
  125. console.log(solve([
  126.     'def var1[1, 2, 3 ,4]',
  127.     'def var2 sum[var1, 20, 70]',
  128.     'def var3 min[var1]',
  129.     'avg[var2, var3]'
  130. ]));
  131.  
  132. console.log(solve([
  133.     'def func sum[5, 3, 7, 2, 6, 3]',
  134.     'def func2 [5, 3, 7, 2, 6, 3]',
  135.     'def func3 min[func2]',
  136.     'def func4 max[5, 3, 7, 2, 6, 3]',
  137.     'def func5 avg[5, 3, 7, 2, 6, 3]',
  138.     'def func6 sum[func2, func3, func4 ]',
  139.     'sum[func6, func4]'
  140. ]));
  141.  
  142. console.log(solve([
  143.     'def func sum[1, 2, 3, -6]',
  144.     'def newList [func, 10, 1]',
  145.     'def newFunc sum[func, 100, newList]',
  146.     '[newFunc]'
  147. ]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement