public boolean isSymmetric(TreeNode A) {
if(A==null)
return true;
/* if(isSymmetric(A.left) && isSymmetric(A.right))
return 1;
else return 0;*/
ArrayList<Integer> al1= preorder(A);
System.out.println(al1);
al.clear();
//System.out.println("common "+al);
p=invertTree(A);
ArrayList<Integer> al2= preorder(p);
System.out.println(al2);
int i=0,j=0;
while(i<al1.size() && i<al2.size())
{
System.out.println(al1.get(i)+" "+al2.get(i));
if(al1.get(i)!=al2.get(i))
return false;
i++;
}
return true;
}
i am trying to compare whether 2 arraylist are having same elements in same sequence or not
Your input
[1,2,2,null,3,null,3]
stdout
[1, 2, -1, 3, -1, -1, 2, -1, 3, -1, -1] //printed al1
[1, 2, 3, -1, -1, -1, 2, 3, -1, -1, -1] //printed al2
1 1 2 2 3 3
-1 -1
-1 -1
-1 -1 2 2 3 3
-1 -1
-1 -1
-1 -1
//don't know how they are showing same elements
Output
true
Expected
false
Let's suppose that you have al1 and al2 ArrayLists. This is how you can check whether they are equal (same size and same values in all positions)
boolean same = true;
if ((al1 == null) != (al2 == null)) {
same = false;
}
if (al1.size() != al2.size()) {
same = false;
} else {
for (int i = 0; same && (same = al1.get(i).equals(al2.get(i)); i++);
//The result is in same
}
Related
Here is the updated AFL for "Filtered Connors RSI" -
_SECTION_BEGIN("Filtered Connors RSI Final");
ma_length = Param("MA Length", 14, 3, 100);
c_length = Param("C Length", 3, 1, 100);
ud_length = Param("UD Length", 2, 1, 100);
roc_length = Param("ROC Length", 100, 1, 100);
function updown(input)
{
s = Close;
isEqual = s == Ref(s, -1);
isGrowing = s > Ref(s, -1);
ud = StaticVarGet("UD Self-Referencing", align=True);
ud = IIf(isEqual == 0, isGrowing, IIf(Nz(Ref(ud, -1)) <= 0, 1, Nz(Ref(ud, -1) + 1), IIf(Nz(Ref(ud, -1)) >= 0, -1, Nz(Ref(ud, -1) - 1)), Null);
return ud;
}
function RSIt(ARRAY, range)
{
return RSIa(Close, c_length);
}
function CRSI(Close, c_length, ud_length, roc_length)
{
updn = RSIa(updown(Close), c_length);
tRSI = RSIa(Close, c_length);
updownrsi = RSIt(updn, ud_length);
ptrk = PercentRank(ROC(Ref(Close, -1)), roc_length);
return (tRSI + updownrsi + ptrk)/3;
}
rsi_open = crsi( Open, c_length, ud_length, roc_length);
rsi_high = crsi( High, c_length, ud_length, roc_length);
rsi_low = crsi( Low, c_length, ud_length, roc_length);
rsi_close = crsi( Close, c_length, ud_length, roc_length);
function HaClose(O, H, L, C)
{
return (O + H + L + C) / 4;
}
source = HaClose(rsi_open, rsi_high, rsi_low, rsi_close);
fcrsi = TEMA(source, ma_length);
PlotGrid(70, colorGreen, pattern=10, width=1, label=True);
PlotGrid(60, colorPlum, pattern=9, width=1, label=True);
PlotGrid(50, colorBrown, pattern=9, width=1, label=True);
PlotGrid(40, colorPlum, pattern=9, width=1, label=True);
PlotGrid(30, colorRed, pattern=10, width=1, label=True);
Plot(fcrsi, "Filtered Connors RSI", colorBlue, styleLine|styleThick);
//Rescaling
Plot(100, "", colorWhite,styleLine|styleNoDraw|styleNoLabel);
Plot(0, "", colorWhite,styleLine|styleNoDraw|styleNoLabel);
_SECTION_END();
Here is the error -
Error 31. Syntax error, unexpected ';', expecting ')' or','
The line of pine script code based on the "Ternary Operator" I am trying to code with the "IIf" function in AFL is as follows -
ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) :
(nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
The pine script code snippet consists the whole function is as follows -
updown(float s) =>
isEqual = s == s[1]
isGrowing = s > s[1]
ud = 0.0
ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
ud
Please help me to fix this. Regards.
Here is a simple module containing a down counter:
import chisel3.util.{Valid, DeqIO}
class Input(WIDTH : Int) extends Bundle {
val x = UInt(WIDTH.W)
}
class Output(WIDTH : Int) extends Bundle {
val y = UInt(WIDTH.W)
}
class DownCount(WIDTH : Int) extends Module {
val io = IO(new Bundle {
val in = DeqIO(new Input(WIDTH))
val out = Output(Valid(new Output(WIDTH)))
})
val i = Reg(UInt(WIDTH.W))
val p = RegInit(false.B)
printf("i = %d, p = %b, out.valid = %b\n",
i, p, io.out.valid)
io.in.ready := !p
io.out.valid := false.B
io.out.bits.y := 10.U
when (p) {
i := i - 1.U
when (i === 0.U) {
printf("Before setting out.valid = %b\n", io.out.valid)
p := false.B
// Here the register is set to 1.
io.out.valid := true.B
}
}.otherwise {
when (io.in.valid) {
printf("Initializing\n")
i := WIDTH.U - 1.U
p := true.B
}
}
}
Here is the output after applying the clock.step() function on the module a few times:
i = 0, p = 0, out.valid = 0
Initializing
i = 7, p = 1, out.valid = 0
i = 6, p = 1, out.valid = 0
i = 5, p = 1, out.valid = 0
i = 4, p = 1, out.valid = 0
i = 3, p = 1, out.valid = 0
i = 2, p = 1, out.valid = 0
i = 1, p = 1, out.valid = 0
i = 0, p = 1, out.valid = 1
Before setting out.valid = 1 <-- why is it 1 here??
i = 255, p = 0, out.valid = 0
How on earth can io.out.valid be 1 (true) before it is assigned the true.B value? Is Chisel reordering printf statement in some weird way?
It seems to be normal behavior. With the := operator, the left-value will be written with right value at the end of clock cycle.
out.valid is still true.B just after false.B assignation.
// i = 0, p = 1, out.valid = 1
printf("i = %d, p = %b, out.valid = %b\n",i, p, io.out.valid)
io.in.ready := !p
io.out.valid := false.B
// Here, io.out.valid is still true.B if printf was true.B
// false.BÂ will be written at the end of cycle,
// except if another connection write true.B
io.out.bits.y := 10.U
when (p) {
i := i - 1.U
when (i === 0.U) {
printf("Before setting out.valid = %b\n", io.out.valid)
p := false.B
// Here the register is set to 1.
io.out.valid := true.B // Will be set to true.B (but was already true.B)
}
}.otherwise {
when (io.in.valid) {
printf("Initializing\n")
i := WIDTH.U - 1.U
p := true.B
}
}
}
In my naive implementation of edit-distance finder, I have to check whether the last characters of two strings match:
ulong editDistance(const string a, const string b) {
if (a.length == 0)
return b.length;
if (b.length == 0)
return a.length;
const auto delt = a[$ - 1] == b[$ - 1] ? 0 : 1;
import std.algorithm : min;
return min(
editDistance(a[0 .. $ - 1], b[0 .. $ - 1]) + delt,
editDistance(a, b[0 .. $ - 1]) + 1,
editDistance(a[0 .. $ - 1], b) + 1
);
}
This yields the expected results but if I replace delt with its definition it always returns 1 on non-empty strings:
ulong editDistance(const string a, const string b) {
if (a.length == 0)
return b.length;
if (b.length == 0)
return a.length;
//const auto delt = a[$ - 1] == b[$ - 1] ? 0 : 1;
import std.algorithm : min;
return min(
editDistance(a[0 .. $ - 1], b[0 .. $ - 1]) + a[$ - 1] == b[$ - 1] ? 0 : 1, //delt,
editDistance(a, b[0 .. $ - 1]) + 1,
editDistance(a[0 .. $ - 1], b) + 1
);
}
Why does this result change?
The operators have different precedence from what you expect. In const auto delt = a[$ - 1] == b[$ - 1] ? 0 : 1; there is no ambiguity, but in editDistance(a[0 .. $ - 1], b[0 .. $ - 1]) + a[$ - 1] == b[$ - 1] ? 0 : 1, there is (seemingly).
Simplifying:
auto tmp = editDistance2(a[0..$-1], b[0..$-1]);
return min(tmp + a[$-1] == b[$-1] ? 0 : 1),
//...
);
The interesting part here is parsed as (tmp + a[$-1]) == b[$-1] ? 0 : 1, and tmp + a[$-1] is not equal to b[$-1]. The solution is to wrap things in parentheses:
editDistance(a[0 .. $ - 1], b[0 .. $ - 1]) + (a[$ - 1] == b[$ - 1] ? 0 : 1)
I am the following UDF code that calculate jaro_winkle_distance
It seems to work when test it with json test data but when I try to call it in the google-bigquery UI, it keeps consistently gives me a score of zero.
even with self join e.g.
input:
[
{a: "Liu",b:"Lau"},
{a: "John",b:"Jone"}]
Output:
[
{
"scr": 80
},
{
"scr": 87
}
]
SQL:
CREATE TEMP FUNCTION
jwd(a STRING,
b STRING)
RETURNS INT64
LANGUAGE js AS """
// Assumes 'doInterestingStuff' is defined in one of the library files.
//return doInterestingStuff(a, b);
return jaro_winkler_distance(a,b);
""" OPTIONS ( library="gs://kayama808/javascript/jaro_winkler_google_UDF.js" );
SELECT
x.name name1,
jwd(x.name,
x.name) scr
FROM
babynames.usa_1910_2013_copy x
WHERE
x.gender = 'F' and x.number >= 1000 and x.state = 'CA'
ORDER BY
scr DESC;
http://storage.googleapis.com/bigquery-udf-test-tool/testtool.html
https://storage.cloud.google.com/kayama808/javascript/jaro_winkler_google_UDF.js?_ga=1.184402278.1320598031.1475534357
Try below. It works as expected with result as
name1 name2 scr
Liu Liu 100
John Jone 87
Liu Lau 80
Hopefully, you will be able to put it back to your external lib file :o)
CREATE TEMP FUNCTION jwd(a STRING, b STRING)
RETURNS INT64
LANGUAGE js AS """
/* JS implementation of the strcmp95 C function written by
Bill Winkler, George McLaughlin, Matt Jaro and Maureen Lynch,
released in 1994 (http://web.archive.org/web/20100227020019/http://www.census.gov/geo/msb/stand/strcmp.c).
a and b should be strings. Always performs case-insensitive comparisons
and always adjusts for long strings. */
var jaro_winkler_adjustments = {
'A': 'E',
'A': 'I',
'A': 'O',
'A': 'U',
'B': 'V',
'E': 'I',
'E': 'O',
'E': 'U',
'I': 'O',
'I': 'U',
'O': 'U',
'I': 'Y',
'E': 'Y',
'C': 'G',
'E': 'F',
'W': 'U',
'W': 'V',
'X': 'K',
'S': 'Z',
'X': 'S',
'Q': 'C',
'U': 'V',
'M': 'N',
'L': 'I',
'Q': 'O',
'P': 'R',
'I': 'J',
'2': 'Z',
'5': 'S',
'8': 'B',
'1': 'I',
'1': 'L',
'0': 'O',
'0': 'Q',
'C': 'K',
'G': 'J',
'E': ' ',
'Y': ' ',
'S': ' '
};
if (!a || !b) { return 0.0; }
a = a.trim().toUpperCase();
b = b.trim().toUpperCase();
var a_len = a.length;
var b_len = b.length;
var a_flag = []; var b_flag = [];
var search_range = Math.floor(Math.max(a_len, b_len) / 2) - 1;
var minv = Math.min(a_len, b_len);
// Looking only within the search range, count and flag the matched pairs.
var Num_com = 0;
var yl1 = b_len - 1;
for (var i = 0; i < a_len; i++) {
var lowlim = (i >= search_range) ? i - search_range : 0;
var hilim = ((i + search_range) <= yl1) ? (i + search_range) : yl1;
for (var j = lowlim; j <= hilim; j++) {
if (b_flag[j] !== 1 && a[j] === b[i]) {
a_flag[j] = 1;
b_flag[i] = 1;
Num_com++;
break;
}
}
}
// Return if no characters in common
if (Num_com === 0) { return 0.0; }
// Count the number of transpositions
var k = 0; var N_trans = 0;
for (var i = 0; i < a_len; i++) {
if (a_flag[i] === 1) {
var j;
for (j = k; j < b_len; j++) {
if (b_flag[j] === 1) {
k = j + 1;
break;
}
}
if (a[i] !== b[j]) { N_trans++; }
}
}
N_trans = Math.floor(N_trans / 2);
// Adjust for similarities in nonmatched characters
var N_simi = 0; var adjwt = jaro_winkler_adjustments;
if (minv > Num_com) {
for (var i = 0; i < a_len; i++) {
if (!a_flag[i]) {
for (var j = 0; j < b_len; j++) {
if (!b_flag[j]) {
if (adjwt[a[i]] === b[j]) {
N_simi += 3;
b_flag[j] = 2;
break;
}
}
}
}
}
}
var Num_sim = (N_simi / 10.0) + Num_com;
// Main weight computation
var weight = Num_sim / a_len + Num_sim / b_len + (Num_com - N_trans) / Num_com;
weight = weight / 3;
// Continue to boost the weight if the strings are similar
if (weight > 0.7) {
// Adjust for having up to the first 4 characters in common
var j = (minv >= 4) ? 4 : minv;
var i;
for (i = 0; (i < j) && a[i] === b[i]; i++) { }
if (i) { weight += i * 0.1 * (1.0 - weight) };
// Adjust for long strings.
// After agreeing beginning chars, at least two more must agree
// and the agreeing characters must be more than half of the
// remaining characters.
if (minv > 4 && Num_com > i + 1 && 2 * Num_com >= minv + i) {
weight += (1 - weight) * ((Num_com - i - 1) / (a_len * b_len - i*2 + 2));
}
}
return Math.round(weight*100);
""";
SELECT
name1, name2,
jwd(name1, name2) scr
FROM -- babynames.usa_1910_2013_copy x
(
select "Liu" as name1, "Lau" as name2 union all
select "Liu" as name1, "Liu" as name2 union all
select "John" as name1, "Jone" as name2
) x
ORDER BY scr DESC
In addition: I've just double checked your jaro_winkler_google_UDF2.js file and clearly see that issue is in this file.
Fix this file using code in my answer
Or, just remove below lines in it
var a = r.a;
var b = r.b;
and uncomment
//jaro_winkler.distance = function(a, b) {
//return Math.round(weight*100)
and comment all with emit in it
jaro_winkler_distance=function(r, emit) {
emit(weight);
You should be ok then!
Anyone have any ideas on how to create a generic function to do this in a much shorter fashion?
if (($item['Quantity'] > 9) && ($item['Quantity'] < 20)) { $bogo_item = 1; }
else if (($item['Quantity'] > 19) && ($item['Quantity'] < 30)) { $bogo_item = 2; } else if (($item['Quantity'] > 29) && ($item['Quantity'] < 40)) { $bogo_item = 3; } else if (($item['Quantity'] > 39) && ($item['Quantity'] < 50)) { $bogo_item = 4; }
...
And so forth... Basically, I would like to create a function that will check and compare a variable $item['Quantity'] and see if it fits in one of the cases.
This is a typical lookup problem...
You could create an array of triples:
(I'm not a PHP programmer, but here's some JavaScript)
// position 0=lower limit, position 1=upper limit, position 2=bogo item value
var values = [ [9, 20, 1], [19, 30, 2], [29, 40, 3], [39, 50, 4] ];
Then you can write a generic loop:
for (var i= 0; i < values.length; ++i) {
if ((item.quantity > values[i][0] && (item.quantity < values[i][1]) {
return values[i][2];
}
}
HTH