问:
Take Column username in each row and split each username into player_1 Player_2
I have a table called users in that I have id auto inc primary key , username , password , email , admin_level

I want to loop each username and insert them into new table called matchups

table matchups has tournament_id , player_1 , player_2. I need to split each username from previous table and have each username be assigned player_1 and player_2

end result to have a player vs player matchup as they signup

答:
OK... Normally transferring data from one SQL table to another is better done within the SQL language itself, so let's look at how we can do that (I have never dealt with a problem like this before, so I am learning as I go).

My Test Table players, for reference, was:

```<code>`player_id` `name`
1           Robert Doe
2           John Smith
</code>```

Make All Possible Permutations of Players

First, trying to find the potential permutations of two players.

I found a SO Question which was similar, but dealt with two tables interacting. MySQL - Show All Permutations?. MySQL can use one table twice in the same query, we just need to set aliases for each of them so they don't clash.

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
</code>```

This produced the following:

```<code>`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name`
1                Robert Doe  1                Robert Doe
1                Robert Doe  2                John Smith
2                John Smith  1                Robert Doe
2                John Smith  2                John Smith
</code>```

OK... Unless people are playing against themselves, this will not do, so we better eliminate any rows where both sides are the same.

NOTE: Skip ahead to "Eliminate Self-Matches & Duplicate, but Reversed, Matches - Amended Solution" for a more efficient version of the following steps. (Older solution kept here to show my own learning through this problem.)

Eliminate Self-Matches

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`!=`p2`.`player_id`
</code>```

This produced the following:

```<code>`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name`
1                Robert Doe  2                John Smith
2                John Smith  1                Robert Doe
</code>```

Much better. Now, this is good, and is perfect if, for instance, every player/team plays each other twice (for instance, if they have a home and an away game against each other), but if these are one-off matchups, we need to figure out how to filter out the duplicate matches.

Eliminate Duplicate, but Reversed, Matches

Each member has an id number, and numbers, being, well, numbers, can be easily compared against each other and sorted. To do that compare and sort, on a per row basis, we need to use the CASE conditional. (SQL MAX of multiple columns?)

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name` ,
       CASE WHEN `p1`.`player_id`<`p2`.`player_id`
         THEN CONCAT( `p1`.`player_id` , '|' , `p2`.`player_id` )
         ELSE CONCAT( `p2`.`player_id` , '|' , `p1`.`player_id` )
       END AS `uniqueMatch`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`!=`p2`.`player_id`
</code>```

Which returns

```<code>`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name` `uniqueMatch`
2                John Smith  1                Robert Doe 1|2
1                Robert Doe  2                John Smith 1|2
</code>```

Now all we have to do group the rows by those matches

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name` ,
       CASE WHEN `p1`.`player_id`<`p2`.`player_id`
         THEN CONCAT( `p1`.`player_id` , '|' , `p2`.`player_id` )
         ELSE CONCAT( `p2`.`player_id` , '|' , `p1`.`player_id` )
       END AS `uniqueMatch`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`!=`p2`.`player_id`
GROUP BY `uniqueMatch`
</code>```

Which returns:

```<code>`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name` `uniqueMatch`
2                John Smith  1                Robert Doe 1|2
</code>```

And adding another row to the table to check it behaves itself:

```<code>`p1`.`player_id` `p1`.`name`  `p2`.`player_id` `p2`.`name` `uniqueMatch`
2                Robert Doe   1                John Smith  1|2
3                Matt Citizen 1                John Smith  1|3
3                Matt Citizen 2                Robert Doe  2|3
</code>```

Eliminate Self-Matches & Duplicate, but Reversed, Matches - Amended Solution

With many thanks to David Winant, the solution could be shorted somewhat. Rather than using the CASE THEN CONCAT( ... ) ELSE ... we can apply a simple additional check to the WHERE clause to ensure that we only get one set of matched players. Again, as we know that the player's IDs will be diferent, and as such one will be greater than the other, we can check that is the case to eliminate half the returned rows.

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
</code>```0

Which returns

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
</code>```1

Again, thank you to David Winant for identifying this amendment and sharing it with me - now I have learnt even more in helping out with this problem.

Excellent! Now, we can use these results to automatically INSERT into another table:

Inserting the Results into Another Table

If we were doing it manually, we'd use something like:

```<code>SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
</code>```2

But, we can use the above SELECT query to take care of all the VALUES bit:

```<code>INSERT INTO `matches`
  ( `id_player1` , `id_player2` )
  SELECT `p1`.`player_id` , `p2`.`player_id`
  FROM `players` AS `p1`
  CROSS JOIN `players` AS `p2`
  WHERE `p1`.`player_id`<`p2`.`player_id`
</code>```

Which produces the table:

```<code>`id_match` `id_player1` `id_player2`
1          2            1
2          3            1
3          3            2
</code>```

And, voila, you have it!

Incremental Addition for New Player

The above works from an established table of players (which is fine in instances where everyone registers before a deadline and the matchups are then calculated), but for instances where new users/players are added afterwards, the following code can be used to add the new matches.

```<code>INSERT INTO `matches` ( `id_player1` , `id_player2` )
  SELECT 123 , `player_id`
  FROM `players`
  WHERE `player_id`!=123
</code>```

Where 123 is the player_id of the newly added player.

Body:
OK... Normally transferring data from one SQL table to another is better done within the SQL language itself, so let's look at how we can do that (I have never dealt with a problem like this before, so I am learning as I go).

My Test Table players, for reference, was:

`player_id` `name`
1           Robert Doe
2           John Smith

Make All Possible Permutations of Players

First, trying to find the potential permutations of two players.

I found a SO Question which was similar, but dealt with two tables interacting. MySQL - Show All Permutations?. MySQL can use one table twice in the same query, we just need to set aliases for each of them so they don't clash.

SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`

This produced the following:

`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name`
1                Robert Doe  1                Robert Doe
1                Robert Doe  2                John Smith
2                John Smith  1                Robert Doe
2                John Smith  2                John Smith

OK... Unless people are playing against themselves, this will not do, so we better eliminate any rows where both sides are the same.

NOTE: Skip ahead to "Eliminate Self-Matches & Duplicate, but Reversed, Matches - Amended Solution" for a more efficient version of the following steps. (Older solution kept here to show my own learning through this problem.)

Eliminate Self-Matches

SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`!=`p2`.`player_id`

This produced the following:

`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name`
1                Robert Doe  2                John Smith
2                John Smith  1                Robert Doe

Much better. Now, this is good, and is perfect if, for instance, every player/team plays each other twice (for instance, if they have a home and an away game against each other), but if these are one-off matchups, we need to figure out how to filter out the duplicate matches.

Eliminate Duplicate, but Reversed, Matches

Each member has an id number, and numbers, being, well, numbers, can be easily compared against each other and sorted. To do that compare and sort, on a per row basis, we need to use the CASE conditional. (SQL MAX of multiple columns?)

SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name` ,
       CASE WHEN `p1`.`player_id`<`p2`.`player_id`
         THEN CONCAT( `p1`.`player_id` , '|' , `p2`.`player_id` )
         ELSE CONCAT( `p2`.`player_id` , '|' , `p1`.`player_id` )
       END AS `uniqueMatch`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`!=`p2`.`player_id`

Which returns

`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name` `uniqueMatch`
2                John Smith  1                Robert Doe 1|2
1                Robert Doe  2                John Smith 1|2

Now all we have to do group the rows by those matches

SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name` ,
       CASE WHEN `p1`.`player_id`<`p2`.`player_id`
         THEN CONCAT( `p1`.`player_id` , '|' , `p2`.`player_id` )
         ELSE CONCAT( `p2`.`player_id` , '|' , `p1`.`player_id` )
       END AS `uniqueMatch`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`!=`p2`.`player_id`
GROUP BY `uniqueMatch`

Which returns:

`p1`.`player_id` `p1`.`name` `p2`.`player_id` `p2`.`name` `uniqueMatch`
2                John Smith  1                Robert Doe 1|2

And adding another row to the table to check it behaves itself:

`p1`.`player_id` `p1`.`name`  `p2`.`player_id` `p2`.`name` `uniqueMatch`
2                Robert Doe   1                John Smith  1|2
3                Matt Citizen 1                John Smith  1|3
3                Matt Citizen 2                Robert Doe  2|3

Eliminate Self-Matches & Duplicate, but Reversed, Matches - Amended Solution

With many thanks to David Winant, the solution could be shorted somewhat. Rather than using the CASE THEN CONCAT( ... ) ELSE ... we can apply a simple additional check to the WHERE clause to ensure that we only get one set of matched players. Again, as we know that the player's IDs will be diferent, and as such one will be greater than the other, we can check that is the case to eliminate half the returned rows.

SELECT `p1`.`player_id` , `p1`.`name` , `p2`.`player_id` , `p2`.`name`
FROM `players` AS `p1`
CROSS JOIN `players` AS `p2`
WHERE `p1`.`player_id`<`p2`.`player_id`

Which returns

`p1`.`player_id` `p1`.`name`  `p2`.`player_id` `p2`.`name`
1                John Smith   2                Robert Doe
1                John Smith   3                Matt Citizen
2                Robert Doe   3                Matt Citizen

Again, thank you to David Winant for identifying this amendment and sharing it with me - now I have learnt even more in helping out with this problem.

Excellent! Now, we can use these results to automatically INSERT into another table:

Inserting the Results into Another Table

If we were doing it manually, we'd use something like:

INSERT INTO `matches` ( `id_player1` , `id_player2` ) VALUES ( 2 , 1 ) , ( 3 , 1 ) ...

But, we can use the above SELECT query to take care of all the VALUES bit:

INSERT INTO `matches`
  ( `id_player1` , `id_player2` )
  SELECT `p1`.`player_id` , `p2`.`player_id`
  FROM `players` AS `p1`
  CROSS JOIN `players` AS `p2`
  WHERE `p1`.`player_id`<`p2`.`player_id`

Which produces the table:

`id_match` `id_player1` `id_player2`
1          2            1
2          3            1
3          3            2

And, voila, you have it!

Incremental Addition for New Player

The above works from an established table of players (which is fine in instances where everyone registers before a deadline and the matchups are then calculated), but for instances where new users/players are added afterwards, the following code can be used to add the new matches.

INSERT INTO `matches` ( `id_player1` , `id_player2` )
  SELECT 123 , `player_id`
  FROM `players`
  WHERE `player_id`!=123

Where 123 is the player_id of the newly added player.


来源: stackexchange

问:
Efficiently getting most and least significant bit in javascript
In a 64 bit integer. I am trying to get the index of the leftmost non zero bit. I used the naive method of iterating over all the 64 bits to calculate this.

```<code>function getLowestBitPosition(bitstring) {
    for (a = 0; a < bitstring.length; a++) if (bitstring[a] === "1") return a;
}
</code>```

Similarly, I iterated backwards to get the index of the rightmost non zero bit.

```<code>function getHighestBitPosition(bitstring) {
    for (a = bitstring.length - 1; a >= 0; a--) if (bitstring[a] === "1") return a;
}
</code>```

I'm pretty sure that there is a more efficient way to implement this. I have seen methods in C requiring little iteration. However, all those examples are using C library functions. Is there a better way to get the index in javascript?

答:
Realize the answer has already been selected for this question, but found it an interesting problem likely best solved with webassembly (WASM).  Unfortunately, it appears that WASM does not support i64 parameters, so had to settle for sending the BigInt as lo / hi ( i32 / i32 ), and stitching together as an i64 within WASM before looking for the MSB.

EDIT Jan 2021:  Per https://webassembly.org/roadmap/, the latest browsers are now supporting BigInt i64 parameters between javascript and WASM.  This answer has been updated to now include the following WASM code lz64.wat compiled into lz64.wasm:

```<code>(module
  (func $lz64 (param $val i64) (result i64)
    get_local $val
    i64.clz
  )
  (export "lz64" (func $lz64))
)
</code>```

Compared with the WASM code below which needed to stitch together two i32 parameters, the code above is greatly simplified (and much faster)!

The WASM i32 code is fairly straightforward, leveraging https://pengowray.github.io/wasm-ops/ to determine the proper OpCodes.  Additionally, used https://webassembly.github.io/wabt/demo/wat2wasm/ to create, debug, and compile the Web Assembly Text (WAT) to WASM.  The i64.clz op is where the actual magic happens.  All the code before that is to stitch the two i32 numbers together to form the actual i64 number to check.  Note too that WASM acts a bit like an RPN calculator...

lz64v32.wat just below is compiled into lz64v32.wasm

```<code>(module
  (func $lz (param $lo i32) (param $hi i32) (result i32)
    get_local $hi
    i64.extend_i32_u
    i64.const 32
    i64.shl

    get_local $lo
    i64.extend_i32_u
    i64.add

    i64.clz
    i32.wrap_i64
  )
  (export "lz" (func $lz))
)
</code>```

The listing below includes the answer for the De Bruijn solution, in order to test the relative performance.

EDIT Also stumbled into What's the most efficient way of getting position of least significant bit of a number in javascript? which points out the Math.clz32 function(!).  Put together a few help functions to support finding the MSB for a 64 bit BigInt, and ran tests with this third option also.

BEWARE! Running the code below will create 10M BigInts to run through the Math.clz32, WASM, and De Bruijn solutions.  On my Acer laptop, on a handful of runs, the results are...

Math.clz32 =>  1.70s

WASM i32   =>  2.06s

WASM i64   =>  0.43s

De Bruijn  => 13.70s

The WASM i64 solution is significantly faster!

var v232 = 2n ** 32n;

function clz32(n) {
    return Math.clz32(n | 0);
}

// https://stackoverflow.com/questions/61442006/whats-the-most-efficient-way-of-getting-position-of-least-significant-bit-of-a
function ctz32(n) {
    n |= 0;
    return n ? 31 - Math.clz32(n & -n) : 0;
}

function clz64(bn) {
    let result = clz32(Number(bn / v232) | 0);
    if (result === 32) {
        result += clz32(Number(bn % v232) | 0);
    }
    return result;
}

function arrayBufferToBase64(arrayBuffer) {
    return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}

function base64ToArrayBuffer(base64) {
    let s = atob(base64);
    let arrayBuffer = new ArrayBuffer(s.length);
    var bufferView = new Uint8Array(arrayBuffer);
    for (let i = 0; i < s.length; i++) {
        bufferView[i] = s.charCodeAt(i);
    }
    return arrayBuffer;
}

async function compile(fn, preCompiled) {
    let wasmBuffer;
    if (preCompiled) {
        wasmBuffer = base64ToArrayBuffer(preCompiled);
    } else {
        let response = await fetch(fn);
        wasmBuffer = await response.arrayBuffer();
        console.log(arrayBufferToBase64(wasmBuffer));
    }

    return await WebAssembly.instantiate(wasmBuffer);
}

async function runTest() {

    let wasm64v32 = await compile('./lz64v32.wasm', 'AGFzbQEAAAABBwFgAn9/AX8DAgEABwYBAmx6AAAKEAEOACABrUIghiAArXx5pwsAGQRuYW1lAQUBAAJsegILAQACAAJsbwECaGk=');
    let wasm64 = await compile('./lz64.wasm', 'AGFzbQEAAAABBgFgAX4BfgMCAQAHCAEEbHo2NAAACgcBBQAgAHkLABgEbmFtZQEHAQAEbHo2NAIIAQABAAN2YWw=');
    let v232 = 2n ** 32n;
    let randomBigInts = new Array(10000000);
    console.log(`Building ${randomBigInts.length.toLocaleString()} BigInts...\n\n`);
    for (let i = 0; i < randomBigInts.length; i++) {
        randomBigInts[i] = 2n ** BigInt(Math.round(Math.random() * 64));
    }

    console.log(`Math.clz32 MSB start check on ${randomBigInts.length.toLocaleString()} BigInts.`);
    randomBigInts.forEach(v=>{
        64 - clz64(v);
    });
    console.log('Done');

    let v = randomBigInts[0];
    console.log(`Math.clz32 test of msb ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${64 - clz64(v)}\n\n`);

    console.log(`WASM MSB start check on ${randomBigInts.length.toLocaleString()} BigInts via WASM, splitting 64 bit BigInt into 2 x i32 parameters.`);
    let lzf = wasm64v32.instance.exports.lz
    randomBigInts.forEach(v=>{
        64 - lzf(Number(v % v232), Number(v / v232));
    });
    console.log('Done');

    console.log(`WASM test of leading zeros ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${64 - lzf(Number(v % v232), Number(v / v232))}\n\n`);

    console.log(`WASM MSB start check on ${randomBigInts.length.toLocaleString()} BigInts via WASM using i64 parameter.`);
    let lzf64 = wasm64.instance.exports.lz64
    randomBigInts.forEach(v=>{
        64n - lzf64(v);
    });
    console.log('Done');

    console.log(`WASM test of leading zeros ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${64n - lzf64(v)}\n\n`);

    console.log(`DeBruijn MSB start check on ${randomBigInts.length.toLocaleString()} BigInts via deBruijn.`);
    randomBigInts.forEach(v=>{
        msb(v);
    });
    console.log('Done');
    console.log(`DeBruijn test of leading zeros ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${msb(v)}\n\n`);

}

const deBruijn = [0, 48, -1, -1, 31, -1, 15, 51, -1, 63, 5, -1, -1, -1, 19, -1, 23, 28, -1, -1, -1, 40, 36, 46, -1, 13, -1, -1, -1, 34, -1, 58, -1, 60, 2, 43, 55, -1, -1, -1, 50, 62, 4, -1, 18, 27, -1, 39, 45, -1, -1, 33, 57, -1, 1, 54, -1, 49, -1, 17, -1, -1, 32, -1, 53, -1, 16, -1, -1, 52, -1, -1, -1, 64, 6, 7, 8, -1, 9, -1, -1, -1, 20, 10, -1, -1, 24, -1, 29, -1, -1, 21, -1, 11, -1, -1, 41, -1, 25, 37, -1, 47, -1, 30, 14, -1, -1, -1, -1, 22, -1, -1, 35, 12, -1, -1, -1, 59, 42, -1, -1, 61, 3, 26, 38, 44, -1, 56];
const multiplicator = BigInt("0x6c04f118e9966f6b");

const b1 = BigInt(1)
  , b2 = BigInt(2)
  , b4 = BigInt(4)
  , b8 = BigInt(8)
  , b16 = BigInt(16)
  , b32 = BigInt(32)
  , b57 = BigInt(57);

function msb(v) {
    v |= v >> b1;
    v |= v >> b2;
    v |= v >> b4;
    v |= v >> b8;
    v |= v >> b16;
    v |= v >> b32;
    return deBruijn[BigInt.asUintN(64, (BigInt.asUintN(64, (v * multiplicator))) >> b57)];
}

function lsb(v) {
    v = -v | v;
    return deBruijn[BigInt.asUintN(64, (BigInt.asUintN(64, (~(v) * multiplicator))) >> b57)];
}

runTest();```

BEWARE! Running this code will perform 40M tests, and there will be a pause before you see any results in the console log.

ContentLicense: CC BY-SA 4.0

Body:
In a 64 bit integer. I am trying to get the index of the leftmost non zero bit. I used the naive method of iterating over all the 64 bits to calculate this.

function getLowestBitPosition(bitstring) {
    for (a = 0; a < bitstring.length; a++) if (bitstring[a] === "1") return a;
}

Similarly, I iterated backwards to get the index of the rightmost non zero bit.

function getHighestBitPosition(bitstring) {
    for (a = bitstring.length - 1; a >= 0; a--) if (bitstring[a] === "1") return a;
}

I'm pretty sure that there is a more efficient way to implement this. I have seen methods in C requiring little iteration. However, all those examples are using C library functions. Is there a better way to get the index in javascript?

Body:
Realize the answer has already been selected for this question, but found it an interesting problem likely best solved with webassembly (WASM).  Unfortunately, it appears that WASM does not support i64 parameters, so had to settle for sending the BigInt as lo / hi ( i32 / i32 ), and stitching together as an i64 within WASM before looking for the MSB.

EDIT Jan 2021:  Per https://webassembly.org/roadmap/, the latest browsers are now supporting BigInt i64 parameters between javascript and WASM.  This answer has been updated to now include the following WASM code lz64.wat compiled into lz64.wasm:

(module
  (func $lz64 (param $val i64) (result i64)
    get_local $val
    i64.clz
  )
  (export "lz64" (func $lz64))
)

Compared with the WASM code below which needed to stitch together two i32 parameters, the code above is greatly simplified (and much faster)!

The WASM i32 code is fairly straightforward, leveraging https://pengowray.github.io/wasm-ops/ to determine the proper OpCodes.  Additionally, used https://webassembly.github.io/wabt/demo/wat2wasm/ to create, debug, and compile the Web Assembly Text (WAT) to WASM.  The i64.clz op is where the actual magic happens.  All the code before that is to stitch the two i32 numbers together to form the actual i64 number to check.  Note too that WASM acts a bit like an RPN calculator...

lz64v32.wat just below is compiled into lz64v32.wasm

(module
  (func $lz (param $lo i32) (param $hi i32) (result i32)
    get_local $hi
    i64.extend_i32_u
    i64.const 32
    i64.shl

    get_local $lo
    i64.extend_i32_u
    i64.add

    i64.clz
    i32.wrap_i64
  )
  (export "lz" (func $lz))
)

The listing below includes the answer for the De Bruijn solution, in order to test the relative performance.

EDIT Also stumbled into What's the most efficient way of getting position of least significant bit of a number in javascript? which points out the Math.clz32 function(!).  Put together a few help functions to support finding the MSB for a 64 bit BigInt, and ran tests with this third option also.

BEWARE! Running the code below will create 10M BigInts to run through the Math.clz32, WASM, and De Bruijn solutions.  On my Acer laptop, on a handful of runs, the results are...

Math.clz32 =>  1.70s

WASM i32   =>  2.06s

WASM i64   =>  0.43s

De Bruijn  => 13.70s

The WASM i64 solution is significantly faster!

var v232 = 2n ** 32n;

function clz32(n) {
    return Math.clz32(n | 0);
}

// https://stackoverflow.com/questions/61442006/whats-the-most-efficient-way-of-getting-position-of-least-significant-bit-of-a
function ctz32(n) {
    n |= 0;
    return n ? 31 - Math.clz32(n & -n) : 0;
}

function clz64(bn) {
    let result = clz32(Number(bn / v232) | 0);
    if (result === 32) {
        result += clz32(Number(bn % v232) | 0);
    }
    return result;
}

function arrayBufferToBase64(arrayBuffer) {
    return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}

function base64ToArrayBuffer(base64) {
    let s = atob(base64);
    let arrayBuffer = new ArrayBuffer(s.length);
    var bufferView = new Uint8Array(arrayBuffer);
    for (let i = 0; i < s.length; i++) {
        bufferView[i] = s.charCodeAt(i);
    }
    return arrayBuffer;
}

async function compile(fn, preCompiled) {
    let wasmBuffer;
    if (preCompiled) {
        wasmBuffer = base64ToArrayBuffer(preCompiled);
    } else {
        let response = await fetch(fn);
        wasmBuffer = await response.arrayBuffer();
        console.log(arrayBufferToBase64(wasmBuffer));
    }

    return await WebAssembly.instantiate(wasmBuffer);
}

async function runTest() {

    let wasm64v32 = await compile('./lz64v32.wasm', 'AGFzbQEAAAABBwFgAn9/AX8DAgEABwYBAmx6AAAKEAEOACABrUIghiAArXx5pwsAGQRuYW1lAQUBAAJsegILAQACAAJsbwECaGk=');
    let wasm64 = await compile('./lz64.wasm', 'AGFzbQEAAAABBgFgAX4BfgMCAQAHCAEEbHo2NAAACgcBBQAgAHkLABgEbmFtZQEHAQAEbHo2NAIIAQABAAN2YWw=');
    let v232 = 2n ** 32n;
    let randomBigInts = new Array(10000000);
    console.log(`Building ${randomBigInts.length.toLocaleString()} BigInts...\n\n`);
    for (let i = 0; i < randomBigInts.length; i++) {
        randomBigInts[i] = 2n ** BigInt(Math.round(Math.random() * 64));
    }

    console.log(`Math.clz32 MSB start check on ${randomBigInts.length.toLocaleString()} BigInts.`);
    randomBigInts.forEach(v=>{
        64 - clz64(v);
    });
    console.log('Done');

    let v = randomBigInts[0];
    console.log(`Math.clz32 test of msb ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${64 - clz64(v)}\n\n`);

    console.log(`WASM MSB start check on ${randomBigInts.length.toLocaleString()} BigInts via WASM, splitting 64 bit BigInt into 2 x i32 parameters.`);
    let lzf = wasm64v32.instance.exports.lz
    randomBigInts.forEach(v=>{
        64 - lzf(Number(v % v232), Number(v / v232));
    });
    console.log('Done');

    console.log(`WASM test of leading zeros ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${64 - lzf(Number(v % v232), Number(v / v232))}\n\n`);

    console.log(`WASM MSB start check on ${randomBigInts.length.toLocaleString()} BigInts via WASM using i64 parameter.`);
    let lzf64 = wasm64.instance.exports.lz64
    randomBigInts.forEach(v=>{
        64n - lzf64(v);
    });
    console.log('Done');

    console.log(`WASM test of leading zeros ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${64n - lzf64(v)}\n\n`);

    console.log(`DeBruijn MSB start check on ${randomBigInts.length.toLocaleString()} BigInts via deBruijn.`);
    randomBigInts.forEach(v=>{
        msb(v);
    });
    console.log('Done');
    console.log(`DeBruijn test of leading zeros ${('0'.repeat(64) + v.toString(2)).slice(-64)} is ${msb(v)}\n\n`);

}

const deBruijn = [0, 48, -1, -1, 31, -1, 15, 51, -1, 63, 5, -1, -1, -1, 19, -1, 23, 28, -1, -1, -1, 40, 36, 46, -1, 13, -1, -1, -1, 34, -1, 58, -1, 60, 2, 43, 55, -1, -1, -1, 50, 62, 4, -1, 18, 27, -1, 39, 45, -1, -1, 33, 57, -1, 1, 54, -1, 49, -1, 17, -1, -1, 32, -1, 53, -1, 16, -1, -1, 52, -1, -1, -1, 64, 6, 7, 8, -1, 9, -1, -1, -1, 20, 10, -1, -1, 24, -1, 29, -1, -1, 21, -1, 11, -1, -1, 41, -1, 25, 37, -1, 47, -1, 30, 14, -1, -1, -1, -1, 22, -1, -1, 35, 12, -1, -1, -1, 59, 42, -1, -1, 61, 3, 26, 38, 44, -1, 56];
const multiplicator = BigInt("0x6c04f118e9966f6b");

const b1 = BigInt(1)
  , b2 = BigInt(2)
  , b4 = BigInt(4)
  , b8 = BigInt(8)
  , b16 = BigInt(16)
  , b32 = BigInt(32)
  , b57 = BigInt(57);

function msb(v) {
    v |= v >> b1;
    v |= v >> b2;
    v |= v >> b4;
    v |= v >> b8;
    v |= v >> b16;
    v |= v >> b32;
    return deBruijn[BigInt.asUintN(64, (BigInt.asUintN(64, (v * multiplicator))) >> b57)];
}

function lsb(v) {
    v = -v | v;
    return deBruijn[BigInt.asUintN(64, (BigInt.asUintN(64, (~(v) * multiplicator))) >> b57)];
}

runTest();

BEWARE! Running this code will perform 40M tests, and there will be a pause before you see any results in the console log.


问:
android security crypto: java.lang.NoSuchMethodError: No static method aes256SivTemplate()
When using

androidx.security:security-crypto:1.0.0-rc01

The app runs fine, when using the new

androidx.security:security-crypto:1.0.0-rc02

I get the following

```<code>E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.appm, PID: 12145
    java.lang.NoSuchMethodError: No static method aes256SivTemplate()Lcom/google/crypto/tink/KeyTemplate; in class Lcom/google/crypto/tink/daead/AesSivKeyManager; or its super classes (declaration of 'com.google.crypto.tink.daead.AesSivKeyManager' appears in /data/app/com.app.appm-mak-CE5Ycx65IADcRe9FQw==/base.apk)
        at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:146)
        at com.app.appm.Utils.getPrefs(Utils.java:455)
        at com.app.appm.MainActivity.onCreate(MainActivity.java:184)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 12145 SIG: 9
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
</code>```

The command that fails is

```<code>return EncryptedSharedPreferences.create(
                "app",
                masterKeyAlias,
                context,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
</code>```

Is that mean I should use a different schema? What about current sharedPreferences file that is in use? It will fail because of that, no? Any idea what can be done? Thanks

答:
This version of the library fixes the problem that was caused by the previous RC version. I can't remember exactly what it was, but it was something to do with the tinc library and dependancy clash with other libraries like Firebase.

To fix the previous issue I had to add the following to my build.gradle:

```<code>allprojects {
    configurations.all {
        resolutionStrategy {
            force("com.google.crypto.tink:tink-android:1.3.0-rc1")
        }
    }
}
</code>```

Removing it again fixed the same crash as you had here.

Body:
When using

androidx.security:security-crypto:1.0.0-rc01

The app runs fine, when using the new

androidx.security:security-crypto:1.0.0-rc02

I get the following

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.appm, PID: 12145
    java.lang.NoSuchMethodError: No static method aes256SivTemplate()Lcom/google/crypto/tink/KeyTemplate; in class Lcom/google/crypto/tink/daead/AesSivKeyManager; or its super classes (declaration of 'com.google.crypto.tink.daead.AesSivKeyManager' appears in /data/app/com.app.appm-mak-CE5Ycx65IADcRe9FQw==/base.apk)
        at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:146)
        at com.app.appm.Utils.getPrefs(Utils.java:455)
        at com.app.appm.MainActivity.onCreate(MainActivity.java:184)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 12145 SIG: 9
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

The command that fails is

return EncryptedSharedPreferences.create(
                "app",
                masterKeyAlias,
                context,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);

Is that mean I should use a different schema? What about current sharedPreferences file that is in use? It will fail because of that, no? Any idea what can be done? Thanks

Body:
This version of the library fixes the problem that was caused by the previous RC version. I can't remember exactly what it was, but it was something to do with the tinc library and dependancy clash with other libraries like Firebase.

To fix the previous issue I had to add the following to my build.gradle:

allprojects {
    configurations.all {
        resolutionStrategy {
            force("com.google.crypto.tink:tink-android:1.3.0-rc1")
        }
    }
}

Removing it again fixed the same crash as you had here.


问:
Regex matching everything except what is between [ ... ]
I'm looking for two Regex which given a string like the following one:

I want [I]  cAptUre this [don't] number [want to] 9 and [be captured] this Word

(A)  return only the string inside each [] (brackets included). I've solved this with \[(.*?)\]

(B) return everything except what is inside [] (brackets excluded from the end result). So the opposite of the previous one.

Expected result:

(A) ["[I]", "[don't]", "[want to]", "[be captured]"]

(B) "I want CaptUre this number 9 and this Word"

How can I do it?

答:
Like this for the first requirement :

```<code>(\[[^]]+\])
</code>```

Check https://regex101.com/r/NdmSRF/1

For the second requirement, not sure a regex alone can do this, but sed can do it:

```<code>$ sed -E 's/\[[^]]+\]//g' file
I want   cAptUre this  number  9 and  this Word
</code>```

Body:
Like this for the first requirement :

(\[[^]]+\])

Check https://regex101.com/r/NdmSRF/1

For the second requirement, not sure a regex alone can do this, but sed can do it:

$ sed -E 's/\[[^]]+\]//g' file
I want   cAptUre this  number  9 and  this Word


问:
Androidx.security with google protobuf
I wanted to implement androidx.security in my app in order to encrypt my sharedpreferences.

I had few problems with building the dependencies of this but eventually the app ran.

Then, when I come to the line where I EncryptedSharedPreferences.create, I get the following error:

```<code>java.lang.NoSuchMethodError: No static method registerDefaultInstance(Ljava/lang/Class;Lcom/google/protobuf/GeneratedMessageLite;)V in class Lcom/google/protobuf/GeneratedMessageLite; or its super classes (declaration of 'com.google.protobuf.GeneratedMessageLite' appears in /data/app/com.example.myapp-1/base.apk:classes3.dex)
    at com.google.crypto.tink.proto.AesSivKeyFormat.<clinit>(AesSivKeyFormat.java:248)
    at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:129)
    at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.createAesSivKeyTemplate(DeterministicAeadKeyTemplates.java:45)
    at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.<clinit>(DeterministicAeadKeyTemplates.java:38)
    at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:146)
    at com.example.myapp.SignInActivity.lambda$null$1$SignInActivity(SignInActivity.java:129)
    at com.example.myapp.-$$Lambda$SignInActivity$j5WyCkLUVlBZT6WQVBcMkDLDozQ.onComplete(lambda)
    at com.google.android.gms.tasks.zzj.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
</code>```

Any idea how to solve this?

I'm pretty sure it is because I used

```<code>exclude group: "com.google.protobuf", module: "protobuf-javalite"
</code>```

In my build.gradle to make androidx.security work but I can't find any other solution.

Thank you

答:
This issue was resolved in version 1.0.0-rc02.

See: https://developer.android.com/jetpack/androidx/releases/security

Title: Androidx.security with google protobuf

Body:
I wanted to implement androidx.security in my app in order to encrypt my sharedpreferences.

I had few problems with building the dependencies of this but eventually the app ran.

Then, when I come to the line where I EncryptedSharedPreferences.create, I get the following error:

java.lang.NoSuchMethodError: No static method registerDefaultInstance(Ljava/lang/Class;Lcom/google/protobuf/GeneratedMessageLite;)V in class Lcom/google/protobuf/GeneratedMessageLite; or its super classes (declaration of 'com.google.protobuf.GeneratedMessageLite' appears in /data/app/com.example.myapp-1/base.apk:classes3.dex)
    at com.google.crypto.tink.proto.AesSivKeyFormat.<clinit>(AesSivKeyFormat.java:248)
    at com.google.crypto.tink.proto.AesSivKeyFormat.newBuilder(AesSivKeyFormat.java:129)
    at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.createAesSivKeyTemplate(DeterministicAeadKeyTemplates.java:45)
    at com.google.crypto.tink.daead.DeterministicAeadKeyTemplates.<clinit>(DeterministicAeadKeyTemplates.java:38)
    at androidx.security.crypto.EncryptedSharedPreferences$PrefKeyEncryptionScheme.<clinit>(EncryptedSharedPreferences.java:146)
    at com.example.myapp.SignInActivity.lambda$null$1$SignInActivity(SignInActivity.java:129)
    at com.example.myapp.-$$Lambda$SignInActivity$j5WyCkLUVlBZT6WQVBcMkDLDozQ.onComplete(lambda)
    at com.google.android.gms.tasks.zzj.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Any idea how to solve this?

I'm pretty sure it is because I used

exclude group: "com.google.protobuf", module: "protobuf-javalite"

In my build.gradle to make androidx.security work but I can't find any other solution.

Thank you


问:
Making a POST request in Selenium without filling a form?
I have an application A that should handle a form submit made with POST method. The actual form, that initiates the request, is in totally separate application B. I am testing application A using Selenium, and I like to write a test case for form submit handling.

How to do this? Can this be done in Selenium at all? Application A does not have a form that can initiate this request.

Note, that the request must use POST, otherwise I could just use WebDriver.get(url) method.

答:
The easiest way I found is making an intermediary page solely for the purposes of submitting a POST request. Have selenium open the page, submit the form, and then get the source of the final page.</p>

```<code>from selenium import webdriver
html='<html><head><title>test</title></head><body><form action="yoursite.com/postlocation" method="post" id="formid"><input type="hidden" name="firstName" id="firstName" value="Bob"><input type="hidden" name="lastName" id="lastName" value="Boberson"><input type="submit" id="inputbox"></form></body></html>'
htmlfile='/tmp/temp.html'
    try:
        with open(htmlfile, "w") as text_file:
            text_file.write(html)
    except:
        print('Unable to create temporary HTML file')
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.get('file://'+htmlfile)
driver.find_element_by_id('inputbox').click();
#wait for form to submit and finish loading page
wait = WebDriverWait(driver, 30)
response=driver.page_source
</code>```

ContentLicense: CC BY-SA 3.0

Body:
The easiest way I found is making an intermediary page solely for the purposes of submitting a POST request. Have selenium open the page, submit the form, and then get the source of the final page.

from selenium import webdriver
html='<html><head><title>test</title></head><body><form action="yoursite.com/postlocation" method="post" id="formid"><input type="hidden" name="firstName" id="firstName" value="Bob"><input type="hidden" name="lastName" id="lastName" value="Boberson"><input type="submit" id="inputbox"></form></body></html>'
htmlfile='/tmp/temp.html'
    try:
        with open(htmlfile, "w") as text_file:
            text_file.write(html)
    except:
        print('Unable to create temporary HTML file')
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.get('file://'+htmlfile)
driver.find_element_by_id('inputbox').click();
#wait for form to submit and finish loading page
wait = WebDriverWait(driver, 30)
response=driver.page_source


问:
flutter doesn't show images in the web
Hello i have build a flutter web project which includes some images but all without one aren't shown in the web but all are shown in the emulator. Could you tell me the solution for this?
Tell me if you need more insides.

pubspec.yaml:

```<code>name: nanostifel
description: A new Flutter application.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev
CFBundleVersion.
https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
flutter:
sdk: flutter

cupertino_icons: ^0.1.3

dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:[enter image description here][1]
- images/
</code>```

答:
If you check the migration guide for Flutter Web you will see that: If the application has assets, they need to be in the web/assets directory. You may also have to update paths within your source code that reference these assets.

But for the latest versions of Flutter you can have the assets directory in the root of your project too.

So check this steps:

Put your assets in the in the folder web/assets of your project.

You don't need to specify the name of the assets in your pubspec.yaml

To use it in a a widget, you don't need to use all the path, use only the file name, for example if you have the image web/assets/myImage.png just use it as follows:

Image.asset('myImage.png')

And if you are working with the latest version of Flutter, and you have your image in a folder rootProject/assets/images/myImage.png use it as follows:

```<code>Image.asset('images/myImage.png')
</code>```

In both cases is it necessary to have the images in a folder named assets

Title: flutter doesn't show images in the web

Body:
Hello i have build a flutter web project which includes some images but all without one aren't shown in the web but all are shown in the emulator. Could you tell me the solution for this?
Tell me if you need more insides.

pubspec.yaml:

name: nanostifel
description: A new Flutter application.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev
CFBundleVersion.
https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
flutter:
sdk: flutter

cupertino_icons: ^0.1.3

dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:[enter image description here][1]
- images/

Body:
If you check the migration guide for Flutter Web you will see that: If the application has assets, they need to be in the web/assets directory. You may also have to update paths within your source code that reference these assets.

But for the latest versions of Flutter you can have the assets directory in the root of your project too.

So check this steps:

Put your assets in the in the folder web/assets of your project.

You don't need to specify the name of the assets in your pubspec.yaml

To use it in a a widget, you don't need to use all the path, use only the file name, for example if you have the image web/assets/myImage.png just use it as follows:

Image.asset('myImage.png')

And if you are working with the latest version of Flutter, and you have your image in a folder rootProject/assets/images/myImage.png use it as follows:

Image.asset('images/myImage.png')

In both cases is it necessary to have the images in a folder named assets


问:
Query between two relative dates, MySQL
I've got this function but I'd like the user to be able to search between 1 and 2 months back, or 6 and 19 months, or whatever.

```<code>$query =
"SELECT * FROM reports
WHERE date > DATE_SUB(NOW(), INTERVAL ".$dateMax." MONTH)"
</code>```

I cannot find any documentation about changing the NOW function into a relative date. Any help would be appreciated.

Something like:

```<code>$query =
"SELECT * FROM reports
WHERE date > DATE_SUB(NOW() - ".$dateMin." MONTH, INTERVAL ".$dateMax." MONTH)"
</code>```

would be really great, but it just doesn't work.

答:
```<code>$query = "
SELECT
  *
FROM
  reports
WHERE
  date BETWEEN (CURRENT_DATE - INTERVAL $dateMin MONTH) AND (CURRENT_DATE - INTERVAL $dateMax MONTH);
";
</code>```

Another option would be a date instead of CURRENT_DATE:

```<code>  date BETWEEN ('2009-08-01' - INTERVAL $dateMin MONTH) AND ('2008-08-01' - INTERVAL $dateMax MONTH);
</code>```

Title: Query between two relative dates, MySQL

Body:
I've got this function but I'd like the user to be able to search between 1 and 2 months back, or 6 and 19 months, or whatever.

$query =
"SELECT * FROM reports
WHERE date > DATE_SUB(NOW(), INTERVAL ".$dateMax." MONTH)"

I cannot find any documentation about changing the NOW function into a relative date. Any help would be appreciated.

Something like:

$query =
"SELECT * FROM reports
WHERE date > DATE_SUB(NOW() - ".$dateMin." MONTH, INTERVAL ".$dateMax." MONTH)"

would be really great, but it just doesn't work.

Body:
$query = "
SELECT
  *
FROM
  reports
WHERE
  date BETWEEN (CURRENT_DATE - INTERVAL $dateMin MONTH) AND (CURRENT_DATE - INTERVAL $dateMax MONTH);
";

Another option would be a date instead of CURRENT_DATE:

  date BETWEEN ('2009-08-01' - INTERVAL $dateMin MONTH) AND ('2008-08-01' - INTERVAL $dateMax MONTH);


问:
How to use Python XML findall to find '<v:imagedata r:id="rId7" o:title="1-REN"/>'
I'm trying to do a find all from a Word document for <v:imagedata r:id="rId7" o:title="1-REN"/> with namespace xmlns:v="urn:schemas-microsoft-com:vml" and I cannot figure out what on earth the syntax is.

The docs only cover the very straight forward case and with the URN and VML combo thrown in I can't seem to get any of the examples I've seen online to work. Does anyone happen to know what it is?

I'm trying to do something like this:

```<code>namespace = {'v': "urn:schemas-microsoft-com:vml"}

results = ET.fromstring(xml).findall("imagedata", namespace)
for image_id in results:
    print(image_id)
</code>```

Edit: What @aneroid wrote is 1000% the right answer and super helpful. You should upvote it. That said, after understanding all that - I went with the BS4 answer because it does the entire job in two lines exactly how I need it to . If you don't actually care about the namespaces it seems waaaaaaay easier.

答:
I'm going to leave the question open, but the workaround I'm currently using is to use BeautifulSoup which happily accepts the v: syntax.

```<code>soup = BeautifulSoup(xml, "lxml")

results = soup.find_all("v:imagedata")
</code>```

Body:
I'm trying to do a find all from a Word document for <v:imagedata r:id="rId7" o:title="1-REN"/> with namespace xmlns:v="urn:schemas-microsoft-com:vml" and I cannot figure out what on earth the syntax is.

The docs only cover the very straight forward case and with the URN and VML combo thrown in I can't seem to get any of the examples I've seen online to work. Does anyone happen to know what it is?

I'm trying to do something like this:

namespace = {'v': "urn:schemas-microsoft-com:vml"}

results = ET.fromstring(xml).findall("imagedata", namespace)
for image_id in results:
    print(image_id)

Edit: What @aneroid wrote is 1000% the right answer and super helpful. You should upvote it. That said, after understanding all that - I went with the BS4 answer because it does the entire job in two lines exactly how I need it to . If you don't actually care about the namespaces it seems waaaaaaay easier.

Body:
I'm going to leave the question open, but the workaround I'm currently using is to use BeautifulSoup which happily accepts the v: syntax.

soup = BeautifulSoup(xml, "lxml")

results = soup.find_all("v:imagedata")


问:
ConnectionRequest pause() and resume()
Are ConnectionRequest methods named pause() and resume() really implemented?

Looking at the sources, I imagined that they are "fake", in the sense they actually do nothing, except setting an internal flag:
https://github.com/codenameone/CodenameOne/blob/master/CodenameOne/src/com/codename1/io/ConnectionRequest.java

So, my question is if Codename One offers any way to pause and then resume a large download (for example 10MB or 100MB, that could be the size of a video). My idea is to pause the download when the app goes to background and then resume it when the app returns to foreground, without re-downloading the already downloaded data: this could circumvent the issue described in my previous question: How to improve the behavior of ConnectionRequest?

Thank you

答:
It looks like they are implemented but notice you need to override isPausable() to return true which currently no ConnectionRequest does.

It might make sense to add a setDefaultPausable(boolean) method to ConnectionRequest to make this usage easier and more universal e.g. with the Rest API.

Title: ConnectionRequest pause() and resume()


问:
Flutter how to handle Image.network error (like 404 or wrong url)
How to handle Image.network when the url is wrong or the destination leads to 404.

for example try

```<code>Image.network('https://image.tmdb.org/t/p/w92')
</code>```

答:
It's weird but you can't easily handle errors correctly with Image.Provider.
Use this handy package: https://pub.dev/packages/flutter_advanced_networkimage

Then in you code:

```<code>backgroundImage: AdvancedNetworkImage(
                    "YOUR IMAGE URL",
                    fallbackAssetImage: "YOUR DEAULT ASSERT IMAGE eg:assets/default.png"
                  )
</code>```

Body:
How to handle Image.network when the url is wrong or the destination leads to 404.

for example try

Image.network('https://image.tmdb.org/t/p/w92')

Body:
It's weird but you can't easily handle errors correctly with Image.Provider.
Use this handy package: https://pub.dev/packages/flutter_advanced_networkimage

Then in you code:

backgroundImage: AdvancedNetworkImage(
                    "YOUR IMAGE URL",
                    fallbackAssetImage: "YOUR DEAULT ASSERT IMAGE eg:assets/default.png"
                  )


问:
How do you convert a string into a char in c++?
I'm trying to figure out how I could convert a string into a char, I keep seeing posts online to use strcpy but I can't figure out how to use it. Can someone show an example?</p>

答:
How about?:</p>

```<code>std::string foo = "Your string";
const char* as_char = foo.c_str();
</code>```

Body:
How about?:

std::string foo = "Your string";
const char* as_char = foo.c_str();


问:
Comparator and PriorityQueue
```<code>        int points[][] = { { 0, 1 }, { 1, 0 }, {3,3},{5,-1},{-2,4}}, k = 2;
        Queue<int[]> pq = new PriorityQueue<>((a, b) -> ((b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1])));
        for (int[] e : points) {
            pq.add(e);`
             if (pq.size() > k)
                pq.remove();
        }
</code>```

Can anybody please explain to me the working of this code?
Thanks for your help

答:
The code using the PriorityQueue to order int[], only pick the top2.
the comparator function is:

(a, b) -> ((b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1])) //smaller item will be sort first
```

<p>for example: { 0, 1 }, { 1, 0 }, they'll be equal, {3,3},{5,-1}, {5,-1} will be bigger.</p>

<pre class="lang-java prettyprint-override"><code>for (int[] e : points) {
            pq.add(e); // put the int[] into the queue, queue will sort all items
             if (pq.size() > k)
                pq.remove(); // if the size of the queue exceed, remove the last item in the queue (the more big item)
        }
</code>```

The result of the code will be the top 2 smallest. {1,0} and {0,1}

Title: Comparator and PriorityQueue

Body:
        int points[][] = { { 0, 1 }, { 1, 0 }, {3,3},{5,-1},{-2,4}}, k = 2;
        Queue<int[]> pq = new PriorityQueue<>((a, b) -> ((b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1])));
        for (int[] e : points) {
            pq.add(e);`
             if (pq.size() > k)
                pq.remove();
        }

Can anybody please explain to me the working of this code?
Thanks for your help

Body:
The code using the PriorityQueue to order int[], only pick the top2.
the comparator function is:

(a, b) -> ((b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1])) //smaller item will be sort first

for example: { 0, 1 }, { 1, 0 }, they'll be equal, {3,3},{5,-1}, {5,-1} will be bigger.

for (int[] e : points) {
            pq.add(e); // put the int[] into the queue, queue will sort all items
             if (pq.size() > k)
                pq.remove(); // if the size of the queue exceed, remove the last item in the queue (the more big item)
        }

The result of the code will be the top 2 smallest. {1,0} and {0,1}


问:
Windows7/32bits. Can't run MongoDB after shutdown. I've to delete all files in db directory. Then it works. But I lose all prev files
I use Windows 7 32 bits. I can run MongoDB with this command

```<code>"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --storageEngine=mmapv1 --dbpath="C:\data\db".
</code>```

But after shutdown (I mean when I restart my PC after a shutdown), I can't use it again. I have to delete all files and folders in db directory (which are previously created because I ran MongoDB with that command). Then it works. I mean I can run MongoDB again with that command only after deleting all files and folders inside db directory (C / data / db ). But this way I lose all my previous database contents (the things I saved in my database).

How can I rerun MongoDB when I am working again after a shutdown without having to delete all the files and folders?

IMAGES HERE:

Not Working:

Deleted all files in db:

Works now but lost all prev files:

答: Every time you run the command it creates a "mongod.lock" file in your db directory. So when you re-run after a shutdown, just delete this "mongod.lock" file only. You will not have to delete all the db files. So you will not lose any database contents (things that you saved in the database). </p>

Body:
I use Windows 7 32 bits. I can run MongoDB with this command

"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --storageEngine=mmapv1 --dbpath="C:\data\db".

But after shutdown (I mean when I restart my PC after a shutdown), I can't use it again. I have to delete all files and folders in db directory (which are previously created because I ran MongoDB with that command). Then it works. I mean I can run MongoDB again with that command only after deleting all files and folders inside db directory (C / data / db ). But this way I lose all my previous database contents (the things I saved in my database).

How can I rerun MongoDB when I am working again after a shutdown without having to delete all the files and folders?

IMAGES HERE:

Not Working:

Deleted all files in db:

Works now but lost all prev files:


问:
How to determine number Saturdays and Sundays comes between two dates in java script
I have requirement as follows
I have two dates i need to find how may saturdays and sundays will come in between

Date1: 02/06/2011

Date2: 02/07/2011

10 days are weekends

Thanks
Srini

答:
Brute force: http://jsfiddle.net/mplungjan/vwNfU/</p>

```<code><script>
var aDay = 24*60*60*1000;
function getWeekend(dString1,dString2) {
  var d1 = new Date(Date.parse(dString1)); //"MM/DD/YYYY"
  var d2 = new Date(Date.parse(dString2));
  var weekend = {
    Sat:0,
    Sun:0
  }
  for (var d,i=d1.getTime(), n=d2.getTime();i<=n;i+=aDay) {
    d=new Date(i).getDay();
    document.write("<br>"+new Date(i)+":"+d);
    if (d===6) weekend.Sat++;
    if (d===0) weekend.Sun++;
  }
  return weekend;
}
var satsun = getWeekend("06/02/2011","07/02/2011")
document.write("<br>Sat:"+satsun.Sat+"\nSun:"+satsun.Sun)
</script>
</code>```

Body:
Brute force: http://jsfiddle.net/mplungjan/vwNfU/

<script>
var aDay = 24*60*60*1000;
function getWeekend(dString1,dString2) {
  var d1 = new Date(Date.parse(dString1)); //"MM/DD/YYYY"
  var d2 = new Date(Date.parse(dString2));
  var weekend = {
    Sat:0,
    Sun:0
  }
  for (var d,i=d1.getTime(), n=d2.getTime();i<=n;i+=aDay) {
    d=new Date(i).getDay();
    document.write("<br>"+new Date(i)+":"+d);
    if (d===6) weekend.Sat++;
    if (d===0) weekend.Sun++;
  }
  return weekend;
}
var satsun = getWeekend("06/02/2011","07/02/2011")
document.write("<br>Sat:"+satsun.Sat+"\nSun:"+satsun.Sun)
</script>


问:
galera puppet code creating two clusters instead of one cluster
I have a puppet code that supposed to create one galera cluster that contains two nodes but instead, it is creating two clusters with one node each.

the name of the two nodes are testbox1 and testbox2

the following is my ./hiera/role/testbox.yaml

```<code>---
classes:
  - '::galera'

selinux::mode: 'permissive'

yum::repos::enabled:
  - percona-x86_64
yum::repos:
  contrail-3.2.1-mitaka:
    enabled: 0

packages:
  - 'Percona-XtraDB-Cluster-shared-compat-57'
  - 'Percona-Server-selinux-56'

galera::configure_repo: false
galera::package_ensure: 'present'
galera::galera_package_ensure: 'absent'
galera::galera_package_name: 'Percona-XtraDB-Cluster-galera-3'
galera::client_package_name: 'Percona-XtraDB-Cluster-client-57'
galera::mysql_package_name: 'Percona-XtraDB-Cluster-server-57'
galera::bootstrap_command: 'systemctl start mysql@bootstrap.service'

galera::mysql_service_name: 'mysql'
mysql::server_service_name: 'mysql'
galera::service_enabled: true
galera::mysql_restart: true

galera::configure_firewall: false
mysql::server::purge_conf_dir: true
galera::purge_conf_dir: true

galera::grep_binary: '/bin/grep'
galera::mysql_binary: '/usr/bin/mysql'
galera::rundir: '/var/run/mysqld'
galera::socket: '/var/lib/mysql/mysql.sock'

galera::create_root_user: true
galera::create_root_my_cnf: true
galera::create_status_user: true
galera::status_check: true

galera::galera_servers: ['testbox-1', 'testbox-2']
galera::galera_master: 'testbox-1'
galera::status_password: 'bla'
galera::bind_address: '0.0.0.0'
galera::override_options:
  mysqld:
    pxc_strict_mode: 'ENFORCING'
    wsrep_provider: '/usr/lib64/galera3/libgalera_smm.so'
    wsrep_slave_threads: 8
    wsrep_sst_method: 'rsync'
    wsrep_cluster_name: 'grafana-galera-cluster'
    wsrep_node_address: "%{ipaddress}"
    wsrep_node_name: "%{hostname}"
    wsrep_sst_auth: "sstuser:%{hiera('galera::sstuser_password')}"
    binlog_format: 'ROW'
    default_storage_engine: 'InnoDB'
    innodb_locks_unsafe_for_binlog: 1
    innodb_autoinc_lock_mode: 2
    innodb_buffer_pool_size: '40000M'
    innodb_log_file_size: '100M'
    query_cache_size: 0
    query_cache_type: 0
    datadir: '/var/lib/mysql'
    socket: '/var/lib/mysql/mysql.sock'
    log-error: '/var/log/mysqld.log'
    pid-file: '/var/run/mysql/mysql.pid'
    max_connections: '10000'
    max_connect_errors: '10000000'
  mysqld_safe:
    log-error: '/var/log/mysqld.log'

galera::status_user: 'clustercheck'
galera::status_allow: '%'
galera::status_available_when_donor: 0
galera::status_available_when_readonly: -1
galera::status_host: 'localhost'
galera::status_log_on_success: ''
galera::status_log_on_success_operator: '='
galera::status_port: 9200

galera::validate::action: 'select count(1);'
galera::validate::catch: 'ERROR'
galera::validate::delay: 3
galera::validate::inv_catch: undef
galera::validate::retries: 20
</code>```

and I am using fraenki/galera module

The thing with this code, I end up with testbox1 in one cluster and testbox2 in another cluster instead of having both of them in the same cluster, After troubleshooting my issue is related to jira.percona.com/browse/PXC-2258, I found out the puppet code will create wsrep.cnf which has no value for wsrep_cluster_address and this will overwrite /etc/my.cnf.d/server.cnf which has the right value. I know how to fix this manually by deleting wsrep.cnf but I would like to have Puppet to do this without me fixing things manually but I do not know how.

puppet version 3.8.7 (opensource) (I can not upgrade it)

答:
mysql@bootstrap needs to be executed on only one node. The other node do a normal start and then it will SST off the first node.

With two nodes you will have trouble getting a quorum and its unworkable as a HA system.

Body:
I have a puppet code that supposed to create one galera cluster that contains two nodes but instead, it is creating two clusters with one node each.

the name of the two nodes are testbox1 and testbox2

the following is my ./hiera/role/testbox.yaml

---
classes:
  - '::galera'

selinux::mode: 'permissive'

yum::repos::enabled:
  - percona-x86_64
yum::repos:
  contrail-3.2.1-mitaka:
    enabled: 0

packages:
  - 'Percona-XtraDB-Cluster-shared-compat-57'
  - 'Percona-Server-selinux-56'

galera::configure_repo: false
galera::package_ensure: 'present'
galera::galera_package_ensure: 'absent'
galera::galera_package_name: 'Percona-XtraDB-Cluster-galera-3'
galera::client_package_name: 'Percona-XtraDB-Cluster-client-57'
galera::mysql_package_name: 'Percona-XtraDB-Cluster-server-57'
galera::bootstrap_command: 'systemctl start mysql@bootstrap.service'

galera::mysql_service_name: 'mysql'
mysql::server_service_name: 'mysql'
galera::service_enabled: true
galera::mysql_restart: true

galera::configure_firewall: false
mysql::server::purge_conf_dir: true
galera::purge_conf_dir: true

galera::grep_binary: '/bin/grep'
galera::mysql_binary: '/usr/bin/mysql'
galera::rundir: '/var/run/mysqld'
galera::socket: '/var/lib/mysql/mysql.sock'

galera::create_root_user: true
galera::create_root_my_cnf: true
galera::create_status_user: true
galera::status_check: true

galera::galera_servers: ['testbox-1', 'testbox-2']
galera::galera_master: 'testbox-1'
galera::status_password: 'bla'
galera::bind_address: '0.0.0.0'
galera::override_options:
  mysqld:
    pxc_strict_mode: 'ENFORCING'
    wsrep_provider: '/usr/lib64/galera3/libgalera_smm.so'
    wsrep_slave_threads: 8
    wsrep_sst_method: 'rsync'
    wsrep_cluster_name: 'grafana-galera-cluster'
    wsrep_node_address: "%{ipaddress}"
    wsrep_node_name: "%{hostname}"
    wsrep_sst_auth: "sstuser:%{hiera('galera::sstuser_password')}"
    binlog_format: 'ROW'
    default_storage_engine: 'InnoDB'
    innodb_locks_unsafe_for_binlog: 1
    innodb_autoinc_lock_mode: 2
    innodb_buffer_pool_size: '40000M'
    innodb_log_file_size: '100M'
    query_cache_size: 0
    query_cache_type: 0
    datadir: '/var/lib/mysql'
    socket: '/var/lib/mysql/mysql.sock'
    log-error: '/var/log/mysqld.log'
    pid-file: '/var/run/mysql/mysql.pid'
    max_connections: '10000'
    max_connect_errors: '10000000'
  mysqld_safe:
    log-error: '/var/log/mysqld.log'

galera::status_user: 'clustercheck'
galera::status_allow: '%'
galera::status_available_when_donor: 0
galera::status_available_when_readonly: -1
galera::status_host: 'localhost'
galera::status_log_on_success: ''
galera::status_log_on_success_operator: '='
galera::status_port: 9200

galera::validate::action: 'select count(1);'
galera::validate::catch: 'ERROR'
galera::validate::delay: 3
galera::validate::inv_catch: undef
galera::validate::retries: 20

and I am using fraenki/galera module

The thing with this code, I end up with testbox1 in one cluster and testbox2 in another cluster instead of having both of them in the same cluster, After troubleshooting my issue is related to jira.percona.com/browse/PXC-2258, I found out the puppet code will create wsrep.cnf which has no value for wsrep_cluster_address and this will overwrite /etc/my.cnf.d/server.cnf which has the right value. I know how to fix this manually by deleting wsrep.cnf but I would like to have Puppet to do this without me fixing things manually but I do not know how.

puppet version 3.8.7 (opensource) (I can not upgrade it)


问:
Connect to Azure SQL with Azure AD and MFA with Dbeaver client 7.0.5 (latest version)
Is there a way to AUTHENTICATE to Azure Sql DB with DBeaver with MFA enabled? Our organization use Azure Active Directory Universal - Universal with MFA to authenticate user to the DBs. Is there a way to use Dbeaver instead of MSSMS?</p>

答: The tool uses the jdbc driver to connect azure SQL. Now, the driver does not support Azure AD with MFA. So we have no way to do these. For more details, please refer to here and here</p>


问:
Changing domain name from localhost to custom in meteor.js
I tried to change domain name of my project with the following command

```<code>$ set ROOT_URL=http://mixer.example.com:3000
$ meteor
</code>```

And that seems to work according to output

```<code>=> Started your app.

=> App running at: http://mixer.example.com:3000
   Type Control-C twice to stop.
</code>```

But I can't open page at this address, it still opens only on localhost. What should I do to fix this?

答:
This worked for me without doing anything else:

```<code>ROOT_URL=http://mydomainname.test:3000/ meteor --port 3000 --settings settings.local.json
</code>```

Body:
I tried to change domain name of my project with the following command

$ set ROOT_URL=http://mixer.example.com:3000
$ meteor

And that seems to work according to output

=> Started your app.

=> App running at: http://mixer.example.com:3000
   Type Control-C twice to stop.

But I can't open page at this address, it still opens only on localhost. What should I do to fix this?

Body:
This worked for me without doing anything else:

ROOT_URL=http://mydomainname.test:3000/ meteor --port 3000 --settings settings.local.json


问:
Is there a faster way to count non-overlapping occurrences in a string than count()?
Given a minimum length N and a string S of 1's and 0's (e.g. "01000100"), I am trying to return the number of non-overlapping occurrences of a sub-string of length n containing all '0's. For example, given n=2 and the string "01000100", the number of non-overlapping "00"s is 2.

This is what I have done:

```<code>def myfunc(S,N):
  return S.count('0'*N)
</code>```

My question: is there a faster way of performing this for very long strings? This is from an online coding practice site and my code passes all but one of the test cases, which fails due to not being able to finish within a time limit. Doing some research it seems I can only find that count() is the fastest method for this.

答:
lenik posted a very good response that worked well. I also found another method faster than count() that I will post here as well. It uses the findall() method from the regex library:</p>

```<code>import re
def my_count(a, n):
  return len(re.findall('0'*n, a))
</code>```

Body:
Given a minimum length N and a string S of 1's and 0's (e.g. "01000100"), I am trying to return the number of non-overlapping occurrences of a sub-string of length n containing all '0's. For example, given n=2 and the string "01000100", the number of non-overlapping "00"s is 2.

This is what I have done:

def myfunc(S,N):
  return S.count('0'*N)

My question: is there a faster way of performing this for very long strings? This is from an online coding practice site and my code passes all but one of the test cases, which fails due to not being able to finish within a time limit. Doing some research it seems I can only find that count() is the fastest method for this.

Body:
lenik posted a very good response that worked well. I also found another method faster than count() that I will post here as well. It uses the findall() method from the regex library:

import re
def my_count(a, n):
  return len(re.findall('0'*n, a))


问:
Heatmap using lists of different size
I would like to compare two list of different size using JaroWinkler similarity. List_1 has 5 elements and it comes from a column dataframe, e.g.

```<code>List_1=df['Movements'].tolist()
</code>```

i.e. List_1=['surrealism', 'futurism', 'impressionism', 'realism', 'neorealism']

while list_2 has 7 elements:

```<code>list_2=['romantisme', 'surrealisme', 'realisme','creativité', 'liberté', 'poete','cubism','metaphysique']
</code>```

To calculate the JaroWinkler distance, I am using the following function:

```<code>jarowinkler = JaroWinkler()
</code>```

that I apply to the column df.Movements as follows:

```<code>for m in all_mov:
     df[m] = df.Movements.apply(lambda x: jarowinkler.similarity(x, m))
    np.where(df[m] > 0.1, df[m], np.nan)
</code>```

I would like to plot using seaborn heat map  only the results where jarowinkler.similarity is greater than 0.1.

```<code>mask = np.zeros_like(df.Movements)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
    f, ax = plt.subplots(figsize=(10, 5))
    ax = sns.heatmap(df['Movements'], mask=mask, square=False)
</code>```

However, I can see only results for a square (i.e. only for the first 5 elements from list_2.
Am I doing something wrong (e.g. considering a heat map not squared)? Is there any other way to visualise similarity between lists of different size?
Advices will be very welcomed.

答:
You can simply do:

```<code>for m in all_mov:
    #compute similarity
    df[m] = df.Movements.apply(lambda x: jarowinkler.similarity(x, m))

    # filter out low similarity scores
    df[m] = np.where(df[m] > 0.1, df[m], np.nan)

sns.heatmap(data=df.set_index('Movements')[all_mov])
</code>```

which gives you something like this:

Title: Heatmap using lists of different size

Body:
I would like to compare two list of different size using JaroWinkler similarity. List_1 has 5 elements and it comes from a column dataframe, e.g.

List_1=df['Movements'].tolist()

i.e. List_1=['surrealism', 'futurism', 'impressionism', 'realism', 'neorealism']

while list_2 has 7 elements:

list_2=['romantisme', 'surrealisme', 'realisme','creativité', 'liberté', 'poete','cubism','metaphysique']

To calculate the JaroWinkler distance, I am using the following function:

jarowinkler = JaroWinkler()

that I apply to the column df.Movements as follows:

for m in all_mov:
     df[m] = df.Movements.apply(lambda x: jarowinkler.similarity(x, m))
    np.where(df[m] > 0.1, df[m], np.nan)

I would like to plot using seaborn heat map  only the results where jarowinkler.similarity is greater than 0.1.

mask = np.zeros_like(df.Movements)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
    f, ax = plt.subplots(figsize=(10, 5))
    ax = sns.heatmap(df['Movements'], mask=mask, square=False)

However, I can see only results for a square (i.e. only for the first 5 elements from list_2.
Am I doing something wrong (e.g. considering a heat map not squared)? Is there any other way to visualise similarity between lists of different size?
Advices will be very welcomed.

Body:
You can simply do:

for m in all_mov:
    #compute similarity
    df[m] = df.Movements.apply(lambda x: jarowinkler.similarity(x, m))

    # filter out low similarity scores
    df[m] = np.where(df[m] > 0.1, df[m], np.nan)

sns.heatmap(data=df.set_index('Movements')[all_mov])

which gives you something like this:


问:
VSCode extension works in development but fails when installed
I created an extension for adding test files in Ruby on Rails - https://github.com/SimonBo/rails-go-to-test. It works when I test it locally (press F5 from VSCode with the extension code open) but when I package the extension and install it i get =>  "command 'extension.goToTest' not found". The problem is probably with runtime dependencies but not sure how to fix this.</p>

答:
It might be that the version of VSCode you have on your computer is older than the version specified in your package.json.

```<code>    "engines": {
        "vscode": "^1.45.0"
    },
</code>```

If you're certain that your extension will work with an older version like "^1.32.0" , update the engines key in your package.json as well as the version of @types/vscode, etc. in your devDependencies.

If this doesn't work, make sure that all the paths in your package.json are specified correctly.

Body:
It might be that the version of VSCode you have on your computer is older than the version specified in your package.json.

    "engines": {
        "vscode": "^1.45.0"
    },

If you're certain that your extension will work with an older version like "^1.32.0" , update the engines key in your package.json as well as the version of @types/vscode, etc. in your devDependencies.

If this doesn't work, make sure that all the paths in your package.json are specified correctly.


问:
Making dynamic filepaths for an exportable JAR file
I want to make a small, runnable program for my friends. As I am on a Mac and they mostly are running Windows, I have to make a dynamic structure for the file paths (only images) I am using in my program. Alas, here is my question:

What is the easiest way to make dynamic file paths, so that my program can find the files no matter what computer (and OS) it is running on?

Seeing as I have to export my program in a JAR file, the images will have to be incorporated into the JAR file.

How is that possible?

I have read about putting the files into the src folder of the program and creating a runnable JAR file, but it doesn't work... (Maybe because of my IDE? I am using Eclipse 4.13.0).

```<code>       try {
        File file = new File(".");
        String path = file.getCanonicalPath() + "/src";     // I put the img in the src folder of the program
        String newFile = path + "someImage.jpeg";
        BufferedImage myPicture = ImageIO.read(new File(newFile));
        JLabel panel = new JLabel(new ImageIcon(myPicture));
        c.add(panel);
    } catch (IOException e) {
    }
</code>```

This is working when running it in Eclipse, but not when running the runnable JAR file.

.
.
.

I am thankful for every answer and in the meantime will continue to search for answers on my own! :)

答:
To me it sounds like you just want the images as resource files for your app. So, I would suggest the convenience of placing them inside the jar, out of sight from your clients (so long as you only just need to read them, a jar is read only.)

I am using Maven in my IDE ... so, the pom edit to tell the build to add these resources into the jar when the IDE builds it, is this:

```<code><!-- AND THIS IS HOW YOU ADD YOUR OWN RESOURCES INTO THE JAR
 -->
        <resources>
          <resource>
            <directory>src/resources/gum</directory>
            <includes>
              <include>**/*.txt</include>
              <include>**/*.jpg</include>
            </includes>
          </resource>
        </resources>
</code>```

See how I directed to be in my own special folder under /src ... yet in the jar, they appear at the root. I have to remember to preface my filenames now with an initial path separator, because I'm looking in a folder relative to the working directory the jvm is run from when working inside the jar.

And then, when you load resources out of a jar, you have to read the files using a little bit different structure than you do out on the normal file system. You load the resource as a stream, or it just throws an exception. Here's an example of how that might look for an image.

```<code>    java.awt.Image frameImage = new javax.swing.ImageIcon(
        getClass().getResource("/LotsaIcon.jpg")
    ).getImage();
    this.setIconImage(frameImage);
</code>```

I hope any of this helps.

Edit: The reason it runs fine from the IDE, is the IDE doesn't actually run the jar, it runs the class files, and from a working directory somewhere in your IDE project, not the root of any executing jar.

Edit2: And did you say even an executable jar won't work? Or was it just the reading files part that failed? Because, as I do with Maven, you also have to tell the IDE you are wanting an executable jar, and I add pom edits for that too, to tell it the main class to add to the jar manifest it creates, or else it won't be an executable jar.

Body:
I want to make a small, runnable program for my friends. As I am on a Mac and they mostly are running Windows, I have to make a dynamic structure for the file paths (only images) I am using in my program. Alas, here is my question:

What is the easiest way to make dynamic file paths, so that my program can find the files no matter what computer (and OS) it is running on?

Seeing as I have to export my program in a JAR file, the images will have to be incorporated into the JAR file.

How is that possible?

I have read about putting the files into the src folder of the program and creating a runnable JAR file, but it doesn't work... (Maybe because of my IDE? I am using Eclipse 4.13.0).

       try {
        File file = new File(".");
        String path = file.getCanonicalPath() + "/src";     // I put the img in the src folder of the program
        String newFile = path + "someImage.jpeg";
        BufferedImage myPicture = ImageIO.read(new File(newFile));
        JLabel panel = new JLabel(new ImageIcon(myPicture));
        c.add(panel);
    } catch (IOException e) {
    }

This is working when running it in Eclipse, but not when running the runnable JAR file.

.
.
.

I am thankful for every answer and in the meantime will continue to search for answers on my own! :)

Body:
To me it sounds like you just want the images as resource files for your app. So, I would suggest the convenience of placing them inside the jar, out of sight from your clients (so long as you only just need to read them, a jar is read only.)

I am using Maven in my IDE ... so, the pom edit to tell the build to add these resources into the jar when the IDE builds it, is this:

<!-- AND THIS IS HOW YOU ADD YOUR OWN RESOURCES INTO THE JAR
 -->
        <resources>
          <resource>
            <directory>src/resources/gum</directory>
            <includes>
              <include>**/*.txt</include>
              <include>**/*.jpg</include>
            </includes>
          </resource>
        </resources>

See how I directed to be in my own special folder under /src ... yet in the jar, they appear at the root. I have to remember to preface my filenames now with an initial path separator, because I'm looking in a folder relative to the working directory the jvm is run from when working inside the jar.

And then, when you load resources out of a jar, you have to read the files using a little bit different structure than you do out on the normal file system. You load the resource as a stream, or it just throws an exception. Here's an example of how that might look for an image.

    java.awt.Image frameImage = new javax.swing.ImageIcon(
        getClass().getResource("/LotsaIcon.jpg")
    ).getImage();
    this.setIconImage(frameImage);

I hope any of this helps.

Edit: The reason it runs fine from the IDE, is the IDE doesn't actually run the jar, it runs the class files, and from a working directory somewhere in your IDE project, not the root of any executing jar.

Edit2: And did you say even an executable jar won't work? Or was it just the reading files part that failed? Because, as I do with Maven, you also have to tell the IDE you are wanting an executable jar, and I add pom edits for that too, to tell it the main class to add to the jar manifest it creates, or else it won't be an executable jar.


问:
SQL query to remove consecutive duplicates in table
I have a table in the format below:

```<code>|name |visited_property|visit_date_time    |
|Marry|Residence_inn   |05-25-2020 15:00:01|
|Marry|Residence_inn   |05-25-2020 15:15:01|
|Marry|Residence_inn   |05-25-2020 15:30:01|
|Marry|Hilton_garden   |05-25-2020 17:10:01|
|Marry|Marriott_hotel  |05-25-2020 18:10:01|

|Harry|Hilton_garden   |05-26-2020 10:10:01|
|Harry|Residence_inn   |05-26-2020 12:10:01|
|Harry|Hilton_garden   |05-26-2020 15:10:01|

</code>```

I want to write a query to get the list of distinct properties (and order of visit) they visit. If they visit the same property consecutively, I'd like to count that as 1 visit. If they visit the same property non-consecutively, I'd like to count the visits separately.

Ideally I would like to replicate the table and get rid of the consecutive duplicates to have something that looks like this (getting rid of Marry's 2nd and 3rd visit to Residence Inn since it's 3 visits to that property in a row, while keeping Harry's both visits to Hilton garden since they are non-consecutive):

```<code>|name |visited_property|visit_date_time    |
|Marry|Residence_inn   |05-25-2020 15:00:01|
|Marry|Hilton_garden   |05-25-2020 17:10:01|
|Marry|Marriott_hotel  |05-25-2020 18:10:01|

|Harry|Hilton_garden   |05-26-2020 10:10:01|
|Harry|Residence_inn   |05-26-2020 12:10:01|
|Harry|Hilton_garden   |05-26-2020 15:10:01|
</code>```

What SQL statement can I use? Appreciate your help.

答:
Use lag():

```<code>select t.*
from (select t.*,
             lag(visited_property) over (partition by name order by visit_date_time) as prev_visited_property
      from t
     ) t
where prev_visited_property is null or prev_visited_property <> visited_property;
</code>```

This returns the first occurrence of each property for each name.

Body:
I have a table in the format below:

|name |visited_property|visit_date_time    |
|Marry|Residence_inn   |05-25-2020 15:00:01|
|Marry|Residence_inn   |05-25-2020 15:15:01|
|Marry|Residence_inn   |05-25-2020 15:30:01|
|Marry|Hilton_garden   |05-25-2020 17:10:01|
|Marry|Marriott_hotel  |05-25-2020 18:10:01|

|Harry|Hilton_garden   |05-26-2020 10:10:01|
|Harry|Residence_inn   |05-26-2020 12:10:01|
|Harry|Hilton_garden   |05-26-2020 15:10:01|

I want to write a query to get the list of distinct properties (and order of visit) they visit. If they visit the same property consecutively, I'd like to count that as 1 visit. If they visit the same property non-consecutively, I'd like to count the visits separately.

Ideally I would like to replicate the table and get rid of the consecutive duplicates to have something that looks like this (getting rid of Marry's 2nd and 3rd visit to Residence Inn since it's 3 visits to that property in a row, while keeping Harry's both visits to Hilton garden since they are non-consecutive):

|name |visited_property|visit_date_time    |
|Marry|Residence_inn   |05-25-2020 15:00:01|
|Marry|Hilton_garden   |05-25-2020 17:10:01|
|Marry|Marriott_hotel  |05-25-2020 18:10:01|

|Harry|Hilton_garden   |05-26-2020 10:10:01|
|Harry|Residence_inn   |05-26-2020 12:10:01|
|Harry|Hilton_garden   |05-26-2020 15:10:01|

What SQL statement can I use? Appreciate your help.

Body:
Use lag():

select t.*
from (select t.*,
             lag(visited_property) over (partition by name order by visit_date_time) as prev_visited_property
      from t
     ) t
where prev_visited_property is null or prev_visited_property <> visited_property;

This returns the first occurrence of each property for each name.


问:
How to use mousePressed for lights in processing?
I was working on an animation on processing. Then, I have a question about the lights. Normally, my code is more long. However, I made a simple code which can usefull also for the beginners.

```<code>void setup()
{
  size(400, 400, P3D);
  noStroke();
}

void draw()
{
  background(0);
  if (mousePressed) {   // lights should work if the mouse pressed on the sphere
    lights();           // It should continue till press again on the sphere
  }                     // If the mouse pressed again on the sphere, lights should close
  translate(200,200,0); // translate the sphere to the middle of window
  sphere(100);          // making a sphere for see ligts
}
</code>```

So, as you can see on the comments. If the mouse pressed on the sphere the lights should work and it should keep working till the mousepressed again on the sphere. Then, if mouse pressed on the sphere it should close the lights.It should keep working again and again. If you know how to make it. You are welcome. Thanks.

答:
You need to have a variable that keeps the state of the light and switch it on if it's off or switch it off if it's on.

After doing that, using mousePressed in the if statement might create some problems since if the click is not quick enough (maybe you press for a bit too long) it will turn on and then off the light so it will look like it was never turned on.
To avoid that I suggest using mouseReleased() function.
Heres the final code:

```<code>boolean isOn = false;    // variable keeping the state of the light

void setup()
{
  size(400, 400, P3D);
  noStroke();
}

void draw()
{
  background(0);
  if (isOn)    // checks the state in which the light should be
    lights();
  translate(200,200,0); // translate the sphere to the middle of window
  sphere(100);          // making a sphere for see ligts
}

void mouseReleased() {    // this function is automatically called in draw method
  if (isOn)    //after a click the state of the light is inverted
      isOn = false;
    else isOn = true;
}
</code>```

Anyway if for some reason you needed to use specifically mousePressed function her's some code also working:

```<code>void draw()
{
  background(0);
  if (mousePressed) {   // lights should work if the mouse pressed on the sphere
    if (isOn)
      isOn = false;
    else isOn = true;
    delay(200);    // delay added to minimize the problem explained above
  }                     // If the mouse pressed again on the sphere, lights should close
  if (isOn)
    lights();
  translate(200,200,0); // translate the sphere to the middle of window
  sphere(100);          // making a sphere for see ligts
}
</code>```

Body:
I was working on an animation on processing. Then, I have a question about the lights. Normally, my code is more long. However, I made a simple code which can usefull also for the beginners.

void setup()
{
  size(400, 400, P3D);
  noStroke();
}

void draw()
{
  background(0);
  if (mousePressed) {   // lights should work if the mouse pressed on the sphere
    lights();           // It should continue till press again on the sphere
  }                     // If the mouse pressed again on the sphere, lights should close
  translate(200,200,0); // translate the sphere to the middle of window
  sphere(100);          // making a sphere for see ligts
}

So, as you can see on the comments. If the mouse pressed on the sphere the lights should work and it should keep working till the mousepressed again on the sphere. Then, if mouse pressed on the sphere it should close the lights.It should keep working again and again. If you know how to make it. You are welcome. Thanks.

Body:
You need to have a variable that keeps the state of the light and switch it on if it's off or switch it off if it's on.

After doing that, using mousePressed in the if statement might create some problems since if the click is not quick enough (maybe you press for a bit too long) it will turn on and then off the light so it will look like it was never turned on.
To avoid that I suggest using mouseReleased() function.
Heres the final code:

boolean isOn = false;    // variable keeping the state of the light

void setup()
{
  size(400, 400, P3D);
  noStroke();
}

void draw()
{
  background(0);
  if (isOn)    // checks the state in which the light should be
    lights();
  translate(200,200,0); // translate the sphere to the middle of window
  sphere(100);          // making a sphere for see ligts
}

void mouseReleased() {    // this function is automatically called in draw method
  if (isOn)    //after a click the state of the light is inverted
      isOn = false;
    else isOn = true;
}

Anyway if for some reason you needed to use specifically mousePressed function her's some code also working:

void draw()
{
  background(0);
  if (mousePressed) {   // lights should work if the mouse pressed on the sphere
    if (isOn)
      isOn = false;
    else isOn = true;
    delay(200);    // delay added to minimize the problem explained above
  }                     // If the mouse pressed again on the sphere, lights should close
  if (isOn)
    lights();
  translate(200,200,0); // translate the sphere to the middle of window
  sphere(100);          // making a sphere for see ligts
}


问:
I can't install tensorflow on win10
I can't install tensorflow
I commanded pip3 install tensorflow --user and result is

ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\\Users\\kosh9\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python38\\site-packages\\tensorboard_plugin_wit\\_vendor\\tensorflow_serving\\sources\\storage_path\\__pycache__\\file_system_storage_path_source_pb2.cpython-38.pyc'

Namely, I could not install packages due to an Environment Error.

How could I solve this error?

答:
Try installing directly form PyPi.

Your on a Windows system so try one of these:

pip install tensorflow-gpu
or if on an Intel processor
pip install intel-tensorflow

If that dons't work try to grab the .whl file. For Python 3.8 on windows that's:
pip install https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.2.0-cp38-cp38-win_amd64.whl

Note: You don't normally need to use pip3 or python3 on Windows

Title: I can't install tensorflow on win10


问:
JQplot barRenderer "y-axis" values start from negative values
Does anyone know how to make "y-axis" values start from 0 in Jqplot....by default it starts with negative values for eg: -500, 0, 500, 1000 and so on....Please help</p>

答:
Refer to http://www.jqplot.com/docs/files/jqPlotOptions-txt.html

set   yaxis: {min: 0, max: 500, numberTicks:5}


问:
RuntimeWarning: overflow encountered in double_scalars in gradient descent
I'm trying to implement gradient descent algorithm to linear regression. I think figured out the math part, but it doesn't work in Python.

```<code>from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
import random

data = load_boston()
df = pd.DataFrame(data['data'], columns=data['feature_names'])
y = data['target']
X = df.TAX

def RMSE(y, y_hat):
    return np.sqrt(sum((y - y_hat) ** 2) / len(y))

def partial_k(x, y, y_hat):
    n = len(y)
    gradient = 0
    for x_i, y_i, y_hat_i in zip(list(x), list(y), list(y_hat)):
        gradient += (y_i - y_hat_i) * x_i
    return -2 / n * gradient

def partial_b(y, y_hat):
    n = len(y)
    gradient = 0
    for y_i, y_hat_i in zip(list(y), list(y_hat)):
        gradient += (y_i - y_hat_i)
    return -2 / n * gradient

def gradient(X, y, n, alpha=0.01, loss=RMSE):
    loss_min = float('inf')

    k = random.random() * 200 - 100
    b = random.random() * 200 - 100

    for i in range(n):
        y_hat = k * X + b
        loss_new = loss(y, y_hat)
        if loss_new < loss_min:
            loss_min = loss_new
            print(f"round: {i}, k: {k}, b: {b}, {loss}: {loss_min}")
        k_gradient = partial_k(X, y, y_hat)
        b_gradient = partial_b(y, y_hat)
        k += -k_gradient * alpha
        b += -b_gradient * alpha
    return (k, b)
gradient(X, y, 200)
</code>```

The script works only for the first iteration, then throw out warning;

```<code>/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:5: RuntimeWarning: overflow encountered in double_scalars
  """
/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: RuntimeWarning: overflow encountered in double_scalars
  if sys.path[0] == '':
/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:29: RuntimeWarning: invalid value encountered in double_scalars
/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:30: RuntimeWarning: invalid value encountered in double_scalars
</code>```

答:
The main feature of gradient descent is to reduce the value of theta till optimum but when the alpha(learning rate) is large gradient descent may overshoot the minimum and increases in an oscillatory fashion over the graph.This is the main reason of why overflow occurs here.

Try reducing alpha(learning rate) OR apply feature normalization.

Body:
I'm trying to implement gradient descent algorithm to linear regression. I think figured out the math part, but it doesn't work in Python.

from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
import random

data = load_boston()
df = pd.DataFrame(data['data'], columns=data['feature_names'])
y = data['target']
X = df.TAX

def RMSE(y, y_hat):
    return np.sqrt(sum((y - y_hat) ** 2) / len(y))

def partial_k(x, y, y_hat):
    n = len(y)
    gradient = 0
    for x_i, y_i, y_hat_i in zip(list(x), list(y), list(y_hat)):
        gradient += (y_i - y_hat_i) * x_i
    return -2 / n * gradient

def partial_b(y, y_hat):
    n = len(y)
    gradient = 0
    for y_i, y_hat_i in zip(list(y), list(y_hat)):
        gradient += (y_i - y_hat_i)
    return -2 / n * gradient

def gradient(X, y, n, alpha=0.01, loss=RMSE):
    loss_min = float('inf')

    k = random.random() * 200 - 100
    b = random.random() * 200 - 100

    for i in range(n):
        y_hat = k * X + b
        loss_new = loss(y, y_hat)
        if loss_new < loss_min:
            loss_min = loss_new
            print(f"round: {i}, k: {k}, b: {b}, {loss}: {loss_min}")
        k_gradient = partial_k(X, y, y_hat)
        b_gradient = partial_b(y, y_hat)
        k += -k_gradient * alpha
        b += -b_gradient * alpha
    return (k, b)
gradient(X, y, 200)

The script works only for the first iteration, then throw out warning;

/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:5: RuntimeWarning: overflow encountered in double_scalars
  """
/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: RuntimeWarning: overflow encountered in double_scalars
  if sys.path[0] == '':
/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:29: RuntimeWarning: invalid value encountered in double_scalars
/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:30: RuntimeWarning: invalid value encountered in double_scalars


问:
Generate XML file with document constructor from xquery
I have problem to generate .xml file from xquery.
Here is my code:

```<code>document {
<?xml version="1.0" encoding=UTF-8" href="C:\Users\User\Desktop\UDBXML-lab\artist.xml"?>,
<ARTISTS>
{
for $a in doc("Artists.xml")//ARTIST
return <ARTIST ID="{$a/@ID}">
{$a/*}
{
for $b in doc("Groups.xml")//GROUP
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("DJ.xml")//DJ
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("Singers.xml")//SINGER
where $a/@ID = $b/@ID
return $b
}
</ARTIST>
}
</ARTISTS>
}
</code>```

I get error:

A processing instruction must not be named 'xml' in any combination of upper or lower case.

But I don't know which processing instruction to set to save this query in document.

答:
You need to leverage XQuery prolog declarations. Here is how to do it.

base-uri declaration specifies a directory for all XML files. The other two declarations will create an XML prolog with desired settings.

XQuery

```<code>xquery version "3.0";
declare base-uri 'C:\Users\User\Desktop\UDBXML-lab\';
declare option output:omit-xml-declaration "no";
declare option output:encoding "UTF-8";

<ARTISTS>
{
for $a in doc("Artists.xml")//ARTIST
return <ARTIST ID="{$a/@ID}">
{$a/*}
{
for $b in doc("Groups.xml")//GROUP
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("DJ.xml")//DJ
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("Singers.xml")//SINGER
where $a/@ID = $b/@ID
return $b
}
</ARTIST>
}
</ARTISTS>
</code>```

Body:
I have problem to generate .xml file from xquery.
Here is my code:

document {
<?xml version="1.0" encoding=UTF-8" href="C:\Users\User\Desktop\UDBXML-lab\artist.xml"?>,
<ARTISTS>
{
for $a in doc("Artists.xml")//ARTIST
return <ARTIST ID="{$a/@ID}">
{$a/*}
{
for $b in doc("Groups.xml")//GROUP
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("DJ.xml")//DJ
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("Singers.xml")//SINGER
where $a/@ID = $b/@ID
return $b
}
</ARTIST>
}
</ARTISTS>
}

I get error:

A processing instruction must not be named 'xml' in any combination of upper or lower case.

But I don't know which processing instruction to set to save this query in document.

Body:
You need to leverage XQuery prolog declarations. Here is how to do it.

base-uri declaration specifies a directory for all XML files. The other two declarations will create an XML prolog with desired settings.

XQuery

xquery version "3.0";
declare base-uri 'C:\Users\User\Desktop\UDBXML-lab\';
declare option output:omit-xml-declaration "no";
declare option output:encoding "UTF-8";

<ARTISTS>
{
for $a in doc("Artists.xml")//ARTIST
return <ARTIST ID="{$a/@ID}">
{$a/*}
{
for $b in doc("Groups.xml")//GROUP
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("DJ.xml")//DJ
where $a/@ID = $b/@ID
return $b
}
{
for $b in doc("Singers.xml")//SINGER
where $a/@ID = $b/@ID
return $b
}
</ARTIST>
}
</ARTISTS>