diff --git a/packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js b/packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js index d01d942f1909..83ce9ca9bf4c 100755 --- a/packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js +++ b/packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js @@ -87,11 +87,8 @@ Blockly.JavaScript.math_on_list = block => { } else if (operation === 'AVERAGE') { const functionName = Blockly.JavaScript.provideFunction_('mathMean', [ `function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) { - if (!myList || !myList.length) { - return null; - } - - return myList.reduce(function(x, y) { + var final_list = []; + return recursiveList(myList, final_list).reduce(function(x, y) { return x + y; }) / myList.length; }`, @@ -101,21 +98,60 @@ Blockly.JavaScript.math_on_list = block => { code = `${functionName}((${list} || [0]))`; } else if (operation === 'MEDIAN') { const functionName = Blockly.JavaScript.provideFunction_('mathMedian', [ - `function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) { - var localList = myList.filter(function(x) { - return typeof x == 'number'; - }); - if (!localList.length) { - return null; + ` + Array.prototype.swap = function (x,y) { + var b = this[x]; + this[x] = this[y]; + this[y] = b; + return this; + } + + function partition(arr, start, end){ + var pivotValue = arr[end]; + var pivotIndex = start; + for (var i = start; i < end; i++) { + if (arr[i] < pivotValue) { + arr.swap(pivotIndex, i); + pivotIndex++; + } } - localList.sort(function(a, b) { - return b - a; - }); - if (localList.length % 2 == 0) { - return (localList[localList.length / 2 - 1] + localList[localList.length / 2]) / 2; - } else { - return localList[(localList.length - 1) / 2]; + arr.swap(end, pivotIndex); + return pivotIndex; + }; + + function quickSort(arr) { + var stack = []; + stack.push(0); + stack.push(arr.length - 1); + + while(stack[stack.length - 1] >= 0){ + end = stack.pop(); + start = stack.pop(); + pivotIndex = partition(arr, start, end); + if (pivotIndex - 1 > start){ + stack.push(start); + stack.push(pivotIndex - 1); + } + if (pivotIndex + 1 < end){ + stack.push(pivotIndex + 1); + stack.push(end); + } } + + } + + function calculateMedian(final_list){ + quickSort(final_list); + + if (final_list.length % 2 == 0) { + return (final_list[final_list.length / 2 - 1] + final_list[final_list.length / 2]) / 2; + } + return final_list[(final_list.length - 1) / 2]; + } + + function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) { + var final_list = []; + return calculateMedian(recursiveList(myList, final_list)); }`, ]); diff --git a/packages/bot-skeleton/src/scratch/dbot.js b/packages/bot-skeleton/src/scratch/dbot.js index 450ce0be4259..cbe225ed00f8 100644 --- a/packages/bot-skeleton/src/scratch/dbot.js +++ b/packages/bot-skeleton/src/scratch/dbot.js @@ -127,7 +127,6 @@ class DBot { runBot() { try { const code = this.generateCode(); - if (this.interpreter !== null) { this.interpreter = null; } @@ -160,6 +159,21 @@ class DBot { var BinaryBotPrivateLastTickTime; var BinaryBotPrivateTickAnalysisList = []; var BinaryBotPrivateHasCalledTradeOptions = false; + + + function recursiveList(list, final_list){ + for(var i=0; i < list.length; i++){ + if(typeof(list[i]) === 'object'){ + recursiveList(list[i], final_list); + } + if(typeof(list[i]) == 'number'){ + final_list.push(list[i]); + + } + } + return final_list; + } + function BinaryBotPrivateRun(f, arg) { if (f) return f(arg); return false; diff --git a/packages/bot-skeleton/src/services/tradeEngine/utils/helpers.js b/packages/bot-skeleton/src/services/tradeEngine/utils/helpers.js index fa0596090503..8877545652c2 100644 --- a/packages/bot-skeleton/src/services/tradeEngine/utils/helpers.js +++ b/packages/bot-skeleton/src/services/tradeEngine/utils/helpers.js @@ -59,7 +59,7 @@ export const getLastDigit = tick => { if (typeof number_string === 'number') { number_string = String(number_string); } - return number_string[number_string.length - 1]; + return Number(number_string[number_string.length - 1]); }; const getBackoffDelayInMs = (error, delay_index) => {