Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* One day @john55 decided to make his own programming language called Listy which will do Excel’s calculations
- with a list of numbers. His conception for now is very simple. He can assign lists to a variables,
- get the min or max value from a list, get average or sum the elements of the list. Each of the functions
- gets as parameters list of numbers or variables in square brackets.”def” is a keyword used to define a variable.
- Everything looks great? Right? But @john55 has some problem with his dog “Sharo” and he doesn’t have the time
- to make an interpreter for Listy. Help him by writing Listy interpreter in JavaScript, because tomorrow morning
- he has meeting with new investors, who wants to use his project for calculation in Boza’s production.
- There could be more than one or no whitespace between the characters.
- Also you can use old functions in the definitions of the new one.
- There will be only a sequence of numbers and variables in the definition of a new variable.
- There will be no nested commands in the given command.
- 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]]”.
- If you meet a variable in a command it’ll be always defined in some of the lines before!
- If there is no operation on the last line, command will looks like “[var1]”. Otherwise if there is a final command
- it’ll be in format: “sum[var1,var2]” (or other operation).
- You are given an array of strings (commands).
- Execute all the commands and print the result only from the last line!*/
- "use strict";
- function solve(args) {
- var defFunctions = [];
- for (var i = 0; i < args.length; i += 1) {
- var currentRow = args[i].trim();
- currentRow = currentRow.replace(/\s+/g, ' ');
- var j = 0;
- var funcObject = { 'name': undefined, 'command': '', 'value': undefined };
- var indexEnd;
- if (currentRow.indexOf('def ') === 0) {
- j += 4;
- if (currentRow[j] != '[' && currentRow[j] != ',') {
- indexEnd = currentRow.indexOf('[', j);
- var funcStr = (currentRow.substring(j, indexEnd)).trim();
- funcStr = funcStr.split(/\s+/).filter(Boolean);
- funcObject.name = funcStr[0];
- if (funcStr.length == 2) {
- funcObject.command = funcStr[1];
- }
- j = indexEnd;
- }
- } else if (i === args.length - 1) {
- if (currentRow[j] != '[' && currentRow[j] != ',') {
- indexEnd = currentRow.indexOf('[', j);
- funcStr = (currentRow.substring(j, indexEnd)).trim();
- funcObject.command = funcStr;
- j = indexEnd;
- }
- }
- if (currentRow[j] == '[' || currentRow[j + 1] == '[') {
- j += 1;
- indexEnd = currentRow.indexOf(']', j);
- var params = (currentRow.substring(j, indexEnd)).trim();
- params = params.split(/[, ]+/).filter(Boolean);
- var paramsList = [];
- for (var k = 0; k < params.length; k += 1) {
- if (isFinite(params[k])) {
- paramsList.push(Number(params[k]));
- } else {
- var temp = getOldDefinedFuncValue(defFunctions, params[k]);
- if (temp instanceof Array) {
- Array.prototype.push.apply(paramsList, temp);
- } else {
- paramsList.push(temp);
- }
- }
- }
- if (paramsList.length > 1) {
- funcObject.value = calculateFuncCommandValue(paramsList, funcObject.command);
- } else {
- funcObject.value = paramsList[0];
- }
- if (i === args.length - 1) {
- return funcObject.value;
- }
- }
- defFunctions.push(funcObject);
- }
- function getOldDefinedFuncValue(objArray, funcNameStr) {
- for (var i = 0; i < objArray.length; i += 1) {
- var object = objArray[i];
- if (object.name === funcNameStr) {
- return object.value;
- }
- }
- }
- function calculateFuncCommandValue(paramsList, command) {
- switch (command) {
- case 'min':
- return Math.min.apply(null, paramsList);
- case 'max':
- return Math.max.apply(null, paramsList);
- case 'sum':
- var sum = 0;
- for (var i = 0; i < paramsList.length; i += 1) {
- sum += paramsList[i];
- }
- return sum;
- case 'avg':
- sum = 0;
- for (i = 0; i < paramsList.length; i += 1) {
- sum += paramsList[i];
- }
- return parseInt((sum / paramsList.length));
- default:
- return paramsList;
- }
- }
- }
- console.log(solve([
- 'def var1 [ 1 , 2 , 6 , 8 ]',
- 'def var2 sum[1, 5, -10, 20]',
- 'def var3 max[5, 2, 4, var2, 2]',
- 'def var4 min[var1, 6, 50]',
- 'def var5 avg[var1] ',
- 'def var6 sum[var1, var1, 1]'
- ]));
- console.log(solve([
- 'def var1[1, 2, 3 ,4]',
- 'def var2 sum[var1, 20, 70]',
- 'def var3 min[var1]',
- 'avg[var2, var3]'
- ]));
- console.log(solve([
- 'def func sum[5, 3, 7, 2, 6, 3]',
- 'def func2 [5, 3, 7, 2, 6, 3]',
- 'def func3 min[func2]',
- 'def func4 max[5, 3, 7, 2, 6, 3]',
- 'def func5 avg[5, 3, 7, 2, 6, 3]',
- 'def func6 sum[func2, func3, func4 ]',
- 'sum[func6, func4]'
- ]));
- console.log(solve([
- 'def func sum[1, 2, 3, -6]',
- 'def newList [func, 10, 1]',
- 'def newFunc sum[func, 100, newList]',
- '[newFunc]'
- ]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement