Skip to content

Commit

Permalink
Sara / fixed issue with nested list (#5288)
Browse files Browse the repository at this point in the history
* fixed issue with nested list

* Removed global variable

* Removed console log

* Applied changes to average method

Co-authored-by: Carol Sachdeva <58209918+carol-binary@users.noreply.github.com>
  • Loading branch information
sara-fs and carolsachdeva authored Apr 26, 2022
1 parent 97e797b commit 867944b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 20 deletions.
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

1 comment on commit 867944b

@vercel
Copy link

@vercel vercel bot commented on 867944b Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

deriv-app – ./

binary.sx
deriv-app-git-master.binary.sx
deriv-app.vercel.app
deriv-app.binary.sx

Please sign in to comment.