Replace values in an ArraySlice<Float> - arrays

I tried to filter an ArraySlice<Float>to replace the values below -5000 and higher 5000 to -5000 and 5000.
I can apply one condition but how I can apply both condition in a same time?
I want to add that if the value is higher than 5000, replace it with 5000, how I can do that?
filteredData.append(contentsOf: data.map({ return $0 < -5000 ? -5000 : $0}))

You can just use an if-else statement to check both conditions
let filteredData = floatData.map { value -> Float in
if value > 5000 {
return 5000
} else if value < -5000 {
return -5000
} else {
return value
}
}
Or you can use the combination of min and max to limit your values to a specified range.
let filteredData = floatData.map { value -> Float in max(min(value, 5000), -5000) }

According to the task conditions, let's do it:
filteredData.append(contentsOf: data.map({ $0 < -5000 ? -5000 : $0 > 5000 ? 5000 : $0}))

Related

How can I use my plotted data in further calculations?

So I have this code that draws zigzag-alike line
indicator("Custom zigzag", overlay=true)
bullish(at) => open[at] < close[at]
bearish(at) => open[at] > close[at]
break_up() =>
if close != open
at = 1
while close[at] == open[at]
at += 1
if bearish(at) and bullish(0)
true
else
false
else
false
break_down() =>
if close != open
at = 1
while close[at] == open[at]
at += 1
if bearish(0) and bullish(at)
true
else
false
else
false
u = break_up()
d = break_down()
plot(u?open[0] : d?open[0] : na, color = color.fuchsia, linewidth = 1, style = plot.style_line, offset=0)
I want to use the result in some further calculations, but I dont get it, how can I put the whole u?open[0] : d?open[0] : na in an array?
Roughly pushing values to an array variable leads to recalculation on each bar, IMO.
Or can I access somehow my own previous plot, since custom script call is not possible?
There are two ways to do it depending on do you want na values as it is or previous calculated values in place of na. You can save the whole thing in a variable and then use that to calculate anything like sma etc. First way
//Keeping na value
val=u?open[0] : d?open[0] : na
s=ta.sma(val,10)
plot(s)
Second way
//Replacing na values with previous values
var val=open[0]
val:=u?open[0] : d?open[0] : val[1]
s=ta.sma(val,10)
plot(s)

Sort Numbers after Letters in Angular

I want to sort Letters first and followed by numbers like below:
[Austria , France , Germany , 101110 , 124563]
This is what i have tried:
obj.sort((a,b) => a.text > b.text ? 1 : -1)
But it is sorting numbers first and then letters.
Any help?
If you are looking for a solution like you want to sort string at the first portion of array and numbers at the last of array, just make sure that you are following this procedure.
Never compare a string with a number.
If both a and b are string or both are numbers just compare both with a > b and return 1 or -1 depending on the result of comparision.
The last else block corresponds to the comparision between a number and a string. The return value determines the location of the numbers in the array. If either one of them is not a number, return -1 or 1 depending on the requirement. Since you want the numbers at the end of your array, return -1 if the value is not a number. If you send 1 instead, the numbers will take the first posion in he output array.
const data = [101110 , 124563 , 'France' , 'Austria', 'Germany'];
const output = data.sort((a,b) => {
if (
(isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b))
) {
// Both are strings
// OR
// Both are numbers
return a > b ? 1 : -1;
}
else {
// One of them is a number
return isNaN(a) ? -1 : 1;
}
});
console.log(output);
Much Simplified version
const data = [101110, 'France', 'Austria', 124563, 'Germany'];
const checkOfSamePattern = (a, b) => (isNaN(a) && isNaN(b)) || (!isNaN(a) && !isNaN(b));
const output = data.sort((a, b) => checkOfSamePattern(a, b) ? a > b ? 1 : -1 : isNaN(a) ? -1 : 1);
console.log(output);

Merge random elements of array/split into chunks

How can I split array into chunks with some special algorithm? E.g. I need to shorten array to the size of 10 elements. If I have array of 11 elements, I want two next standing elements get merged. If I have array of 13 elements, I want three elements merged. And so on. Is there any solution?
Sample #1
var test = ['1','2','3','4','5','6','7','8','9','10','11'];
Need result = [['1'],['2'],['3'],['4'],['5|6'],['7'],['8'],['9'],['10'],['11']]
Sample #2
var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13'];
Need result = [['1|2'],['3'],['4'],['5'],['6'],['7|8'],['9'],['10'],['11'],['12|13']]
Thank you in advance.
The following code most probably does what you want.
function condense(a){
var source = a.slice(),
len = a.length,
excessCount = (len - 10) % 10,
step = excessCount - 1 ? Math.floor(10/(excessCount-1)) : 0,
groupSize = Math.floor(len / 10),
template = Array(10).fill()
.map((_,i) => step ? i%step === 0 ? groupSize + 1
: i === 9 ? groupSize + 1
: groupSize
: i === 4 ? groupSize + 1
: groupSize);
return template.map(e => source.splice(0,e)
.reduce((p,c) => p + "|" + c));
}
var test1 = ['1','2','3','4','5','6','7','8','9','10','11'],
test2 = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21'];
console.log(condense(test1));
console.log(condense(test2));
A - Find the difference and create thus many random numbers for merge and put in array
B - loop through initial numbers array.
B1 - if iterator number is in the merge number array (with indexOf), you merge it with the next one and increase iterator (to skip next one as it is merged and already in results array)
B1 example:
int mergers[] = [2, 7, 10]
//in loop when i=2
if (mergers.indexOf(i)>-1) { //true
String newVal = array[i]+"|"+array[i+1]; //will merge 2 and 3 to "2|3"
i++; //adds 1, so i=3. next loop is with i=4
}
C - put new value in results array
You can try this code
jQuery(document).ready(function(){
var test = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'];
var arrays = [];
var checkLength = test.length;
var getFirstSet = test.slice(0,10);
var getOthers = test.slice(10,checkLength);
$.each( getFirstSet, function( key,value ) {
if(key in getOthers){
values = value +'|'+ getOthers[key];
arrays.push(values);
}else{
arrays.push(value);
}
});
console.log(arrays);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

JavaScript function between two numbers in a list?

I would like to create a function which will retrieve the result from following list
var Fedsy = [
{min : 0, max : 2300 , must:0, percent:0 , excess:0},
{min : 2300, max : 11525 , must:0, percent:10 , excess:2300},
{min : 11525, max : 39750 , must:922.50, percent:15 , excess:11525},
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750},
{min : 93050, max : 191600 , must:18481.25, percent:28 , excess:93050},
{min : 191600, max : 413800 , must:46075.25, percent:33 , excess:191600},
{min : 413800, max : 415500 , must:119401.25, percent:35 , excess:413800},
{min : 415500, max : 1000000000 , must:119996.25, percent:39.6 , excess:415500}
];
Now I want to pull the row from above list if "inputNum" is between "min" & "max"
var result= function(inputNum){
///..???
}
Ex:if "inputNum"= 40000 the function should return
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750}
Since you tagged underscore.js, I understand that you want to solve the problem using that library.
As such, you can use the function _.find to find the first value that matches a condition. Think of it like a filter but just looking into the first one:
function findRange(inputNum) {
return _.find(Fedsy, function(f) {
return f.min <= inputNum && f.max >= inputNum;
});
}
The first item from Fedsy (f) to return true in the iterator function will be the value that underscore returns.
You may see it more in detail and with more reference functions at underscore.js site: http://underscorejs.org/
What I did:
I created the function you mentioned, it uses pure javascript without any libraries. It loops through each item in the Fedsy array, and looks to see if the value is in the range. If so it returns the row in question.
It also returns as soon as it finds the result not continuing to search the object.
Data
var Fedsy = [
{min : 0, max : 2300 , must:0, percent:0 , excess:0},
{min : 2300, max : 11525 , must:0, percent:10 , excess:2300},
{min : 11525, max : 39750 , must:922.50, percent:15 , excess:11525},
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750},
{min : 93050, max : 191600 , must:18481.25, percent:28 , excess:93050},
{min : 191600, max : 413800 , must:46075.25, percent:33 , excess:191600},
{min : 413800, max : 415500 , must:119401.25, percent:35 , excess:413800},
{min : 415500, max : 1000000000 , must:119996.25, percent:39.6 , excess:415500}
];
Function
function filterArray(inputNum){
var row;
// Foreach item in the array
for(rownum in Fedsy){
var row = Fedsy[rownum];
// If "inputNum" is greater than or equal to "row.min" & less than "row.max"
if(inputNum >= row.min && inputNum < row.max){
return row;
}
}
}
Usage
var result = filterArray(3); // returns first row
console.log(result.must); //0
If you just want the first matching row, then and application of underscore/lodash's _.find https://lodash.com/docs#find might be what you want:
var result = (function(inputNum){
return _.find(Fedsy, function(item){
return inputNum>item.min && inputNum<item.max;
});
})();
That will immediately run an anonymous function that accepts the inputNum and uses it in another anonymous function that _.find uses to check each item. The advantage of _.find is that it will stop looking as soon as it hits a result.
Ideally though, you'd write things in a more generalized way, so that you could specify your filter and what you're filtering separately, so maybe by doing something like this:
var minmaxFilterFunc = function(inputNum){
return function(item){
return inputNum>item.min && inputNum<item.max;
};
};
Now, using minmaxFilterFunc, you can create a function that will return true for a particular inputNum. Now you can use find directly, creating the proper filter function in one line. Easier to read, and also easier to reuse for filtering other collections on different numbers later.
var result = _.find(Fedsy, minMaxFilterFunc(inputNum));

Evaluating equality in perl using elements taken from an array ref

I have a small perl script that needs to evaluate the equality of two parameters and a small return from the database.
my ($firstId, $secondId, $firstReturnedId, $secondReturnedId, $picCount);
my $pics = $dbh->prepare(qq[select id from pictures limit 10]);
$firstId = q->param('firstId');
$secondId = q->param('secondId');
$pics->execute or die;
my $picids = $pics->fetchall_arrayref;
$picCount = scalar(#{$picids});
$firstReturnedId = $picCount > 0 ? shift(#{$picids}) : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids}) : $firstReturnedId;
Here, a quick look at my debugger shows that $picCount = 1 and $firstReturnedId = 9020 and $secondReturnedId = 9020. However, they are both denoted as
ARRAY(0x9e79184)
0 9020
in the debugger so when I perform the final check
my $result = (($firstId == $firstReturnedId) && ($secondId == $secondReturnedId)) ? 1 : 0;
I get $result = 0, which is not what I want.
What am I doing wrong?
DBI::fetchall_arrayref returns a reference to a list of "row results". But since there could be more than one value in a row result (e.g., your query could have been select id,other_field from pictures), each row result is also a reference to a list. This means you have one more dereferencing to do in order to get the result you want. Try:
$picCount = scalar(#{$picids});
if ($picCount > 0) {
my $result = shift #{$picids};
$firstReturnedId = $result->[0];
} else {
$firstReturnedId = 0;
}
if ($picCount > 1) {
my $result = pop #{$picids};
$secondReturnedId = $result->[0];
} else {
$secondReturnedId = $firstReturnedId;
}
or if you still want to use a concise style:
$firstReturnedId = $picCount > 0 ? shift(#{$picids})->[0] : 0;
$secondReturnedId = $picCount > 1 ? pop(#{$picids})->[0] : $firstReturnedId;

Resources