Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sara / fixed issue with nested list #5288

Merged
merged 10 commits into from
Apr 26, 2022
72 changes: 54 additions & 18 deletions packages/bot-skeleton/src/scratch/blocks/Math/math_on_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}`,
Expand All @@ -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));
}`,
]);

Expand Down
16 changes: 15 additions & 1 deletion packages/bot-skeleton/src/scratch/dbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class DBot {
runBot() {
try {
const code = this.generateCode();

if (this.interpreter !== null) {
this.interpreter = null;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down