From 4972cd27f983d35471f374ba30ac04d772261b60 Mon Sep 17 00:00:00 2001 From: sara-fs Date: Wed, 20 Apr 2022 23:26:38 +0430 Subject: [PATCH 1/4] fixed issue with nested list --- .../src/scratch/blocks/Math/math_on_list.js | 70 ++++++++++++++----- packages/bot-skeleton/src/scratch/dbot.js | 14 +++- .../src/services/tradeEngine/utils/helpers.js | 2 +- 3 files changed, 68 insertions(+), 18 deletions(-) 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..e99b430d9b04 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,10 @@ 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; - } + finalList = []; + recursiveList(myList); - return myList.reduce(function(x, y) { + return finalList.reduce(function(x, y) { return x + y; }) / myList.length; }`, @@ -101,21 +100,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; + 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(){ + quickSort(finalList); + if (finalList.length % 2 == 0) { + return (finalList[finalList.length / 2 - 1] + finalList[finalList.length / 2]) / 2; } else { - return localList[(localList.length - 1) / 2]; + return finalList[(finalList.length - 1) / 2]; } + } + + function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) { + finalList = []; + recursiveList(myList); + return calculateMedian(); }`, ]); diff --git a/packages/bot-skeleton/src/scratch/dbot.js b/packages/bot-skeleton/src/scratch/dbot.js index 450ce0be4259..d3c83598ee71 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,19 @@ class DBot { var BinaryBotPrivateLastTickTime; var BinaryBotPrivateTickAnalysisList = []; var BinaryBotPrivateHasCalledTradeOptions = false; + + var finalList = []; + function recursiveList(list){ + for(var i=0; i < list.length; i++){ + if(typeof(list[i]) === 'object'){ + recursiveList(list[i]); + } + if(typeof(list[i]) == 'number'){ + finalList.push(list[i]); + } + } + } + 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) => { From f36cfacf9f65001bdf25eb606f872cbf7f9eb069 Mon Sep 17 00:00:00 2001 From: sara-fs Date: Sat, 23 Apr 2022 12:35:34 +0430 Subject: [PATCH 2/4] Removed global variable --- .../src/scratch/blocks/Math/math_on_list.js | 29 +++++++++---------- packages/bot-skeleton/src/scratch/dbot.js | 12 +++++--- 2 files changed, 22 insertions(+), 19 deletions(-) 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 e99b430d9b04..39dcd877044e 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,10 +87,7 @@ Blockly.JavaScript.math_on_list = block => { } else if (operation === 'AVERAGE') { const functionName = Blockly.JavaScript.provideFunction_('mathMean', [ `function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(myList) { - finalList = []; - recursiveList(myList); - - return finalList.reduce(function(x, y) { + return recursiveList(myList).reduce(function(x, y) { return x + y; }) / myList.length; }`, @@ -139,21 +136,23 @@ Blockly.JavaScript.math_on_list = block => { stack.push(end); } } + } - function calculateMedian(){ - quickSort(finalList); - if (finalList.length % 2 == 0) { - return (finalList[finalList.length / 2 - 1] + finalList[finalList.length / 2]) / 2; - } else { - return finalList[(finalList.length - 1) / 2]; - } + function calculateMedian(final_list){ + console.log('stat sorting with final list:', final_list); + quickSort(final_list); + console.log('sorted list:', 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) { - finalList = []; - recursiveList(myList); - return calculateMedian(); + 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 d3c83598ee71..febdd21da8f7 100644 --- a/packages/bot-skeleton/src/scratch/dbot.js +++ b/packages/bot-skeleton/src/scratch/dbot.js @@ -127,6 +127,8 @@ class DBot { runBot() { try { const code = this.generateCode(); + + console.log('code:', code); if (this.interpreter !== null) { this.interpreter = null; } @@ -160,16 +162,18 @@ class DBot { var BinaryBotPrivateTickAnalysisList = []; var BinaryBotPrivateHasCalledTradeOptions = false; - var finalList = []; - function recursiveList(list){ + + function recursiveList(list, final_list){ for(var i=0; i < list.length; i++){ if(typeof(list[i]) === 'object'){ - recursiveList(list[i]); + recursiveList(list[i], final_list); } if(typeof(list[i]) == 'number'){ - finalList.push(list[i]); + final_list.push(list[i]); + } } + return final_list; } function BinaryBotPrivateRun(f, arg) { From 6c7cf54ca50331f1a7eb7631894f56cb7b6365af Mon Sep 17 00:00:00 2001 From: sara-fs Date: Sat, 23 Apr 2022 12:37:23 +0430 Subject: [PATCH 3/4] Removed console log --- packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js | 2 -- 1 file changed, 2 deletions(-) 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 39dcd877044e..842da1849084 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 @@ -140,9 +140,7 @@ Blockly.JavaScript.math_on_list = block => { } function calculateMedian(final_list){ - console.log('stat sorting with final list:', final_list); quickSort(final_list); - console.log('sorted list:', final_list); if (final_list.length % 2 == 0) { return (final_list[final_list.length / 2 - 1] + final_list[final_list.length / 2]) / 2; From 74ac9de5464459d2e07e2c748864f6b0dd866cb0 Mon Sep 17 00:00:00 2001 From: sara-fs Date: Sat, 23 Apr 2022 16:15:41 +0430 Subject: [PATCH 4/4] Applied changes to average method --- packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js | 3 ++- packages/bot-skeleton/src/scratch/dbot.js | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) 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 842da1849084..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,7 +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) { - return recursiveList(myList).reduce(function(x, y) { + var final_list = []; + return recursiveList(myList, final_list).reduce(function(x, y) { return x + y; }) / myList.length; }`, diff --git a/packages/bot-skeleton/src/scratch/dbot.js b/packages/bot-skeleton/src/scratch/dbot.js index febdd21da8f7..cbe225ed00f8 100644 --- a/packages/bot-skeleton/src/scratch/dbot.js +++ b/packages/bot-skeleton/src/scratch/dbot.js @@ -127,8 +127,6 @@ class DBot { runBot() { try { const code = this.generateCode(); - - console.log('code:', code); if (this.interpreter !== null) { this.interpreter = null; }