I want to merge 2 columns on which I joined 2 tables.
Here is my code:
let Table1 = datatable(ver: string, number:int)
[
1.0, 5,
2.0, 5,
2.1, 3
];
//
let Table2 = datatable(ver: string, number:int)
[
2.0, 3,
2.1, 2,
3.0, 1
];
//
Table2
| join kind = fullouter Table1 on ver
| extend Delta = number1 - number
This is what I get:
And this is what I need:
you could use the coalesce() function: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/coalescefunction
for example:
let Table1 = datatable(ver:string, number:int)
[
'1.0', 5,
'2.0', 5,
'2.1', 3
];
let Table2 = datatable(ver:string, number:int)
[
'2.0', 3,
'2.1', 2,
'3.0', 1
];
Table2
| join kind = fullouter Table1 on ver
| project ver = coalesce(ver, ver1),
Delta = coalesce(number1, 0) - coalesce(number, 0)
ver
Delta
1.0
5
2.0
2
2.1
1
3.0
-1
Related
My code is represented in Dart, but this is more general to the Binary Tree data structure and Register-based VM implementation. I have commented the code for you to understand if you do not know Dart as well.
So, here are my nodes:
enum NodeType {
numberNode,
addNode,
subtractNode,
multiplyNode,
divideNode,
plusNode,
minusNode,
}
NumberNode has a number value in it.
AddNode, SubtractNode, MultiplyNode, DivideNode, they are really just Binary Op Nodes .
PlusNode, MinusNode, are Unary Operator nodes.
The tree is generated based off Order of Operations. Unary Operation first, then multiplication and division, and then addition and subtraction. E.g. "1 + 2 * -3" becomes "(1 + (2 * (-3)))"
Here is my code to trying to walk over the AST:
/// Converts tree to Register-based VM code
List<Opcode> convertNodeToCode(Node node) {
List<Opcode> result = [const Opcode(OpcodeKind.loadn, 2, -1)];
bool counterHasBeenZero = false;
bool binOpDebounce = false;
int counter = 0;
List<Opcode> convert(Node node) {
switch (node.nodeType) {
case NodeType.numberNode:
counter = counter == 0 ? 1 : 0;
if (counter == 0 && !counterHasBeenZero) {
counterHasBeenZero = true;
} else {
counter = 1;
}
return [Opcode(OpcodeKind.loadn, counter, (node as NumberNode).value)];
case NodeType.addNode:
var aNode = node as AddNode;
return convert(aNode.nodeA) +
convert(aNode.nodeB) +
[
const Opcode(
OpcodeKind.addn,
0,
1,
)
];
case NodeType.subtractNode:
var sNode = node as SubtractNode;
var result = convert(sNode.nodeA) +
convert(sNode.nodeB) +
(binOpDebounce
? [
const Opcode(
OpcodeKind.subn,
0,
0,
1,
)
]
: [
const Opcode(
OpcodeKind.subn,
0,
1,
)
]);
if (!binOpDebounce) binOpDebounce = true;
return result;
case NodeType.multiplyNode:
var mNode = node as MultiplyNode;
var result = convert(mNode.nodeA) +
convert(mNode.nodeB) +
(binOpDebounce
? [
const Opcode(
OpcodeKind.muln,
0,
0,
1,
)
]
: [
const Opcode(
OpcodeKind.muln,
0,
1,
)
]);
if (!binOpDebounce) binOpDebounce = true;
return result;
case NodeType.divideNode:
var dNode = node as DivideNode;
var result = convert(dNode.nodeA) +
convert(dNode.nodeB) +
(binOpDebounce
? [
const Opcode(
OpcodeKind.divn,
0,
0,
1,
)
]
: [
const Opcode(
OpcodeKind.divn,
0,
1,
)
]);
if (!binOpDebounce) binOpDebounce = true;
return result;
case NodeType.plusNode:
return convert((node as PlusNode).node);
case NodeType.minusNode:
return convert((node as MinusNode).node) +
[Opcode(OpcodeKind.muln, 1, 2)];
default:
throw Exception('Non-existent node type');
}
}
return result + convert(node) + [const Opcode(OpcodeKind.exit)];
}
I tried a method to just use 2-3 registers and using a counter to track where I loaded the number in the register, but the code gets ugly real quick and when I'm trying to do Order of Operations, it gets really hard to track where the numbers are with the counter. Basically, how I tried to make this code work is just store the number in register 1 or 0 and load the number if needed to and add the registers together to equal to register 0. Example, 1 + 2 + 3 + 4 becomes [r2 = -1.0, r1 = 1.0, r0 = 2.0, r0 = r1 + r0, r1 = 3.0, r0 = r1 + r0, r1 = 4.0, r0 = r1 + r0, exit]. When I tried this with multiplication though, this became very hard as it incorrectly multiplied the wrong number which is possibly because of the order of operations.
I tried to see if this way could be done as well:
// (1 + (2 * ((-2) + 3) * 5))
const code = [
// (-2)
Opcode(OpcodeKind.loadn, 1, -2), // r1 = -2;
// (2 + 3)
Opcode(OpcodeKind.loadn, 1, 2), // r1 = 2;
Opcode(OpcodeKind.loadn, 2, 3), // r2 = 3;
Opcode(OpcodeKind.addn, 2, 1, 2), // r2 = r1 + r2;
// (2 * (result) * 5)
Opcode(OpcodeKind.loadn, 1, 2), // r1 = 2;
Opcode(OpcodeKind.loadn, 3, 5), // r3 = 5;
Opcode(OpcodeKind.muln, 2, 1, 2), // r2 = r1 * r2;
Opcode(OpcodeKind.muln, 2, 2, 3), // r2 = r2 * r3;
// (1 + (result))
Opcode(OpcodeKind.loadn, 1, 1), // r1 = 1;
Opcode(OpcodeKind.addn, 1, 1, 2), // r1 = r1 + r2;
Opcode(OpcodeKind.exit), // exit halt
];
I knew this method would not work because if I'm going to iterate through the nodes I need to know the position of the numbers and registers beforehand, so I'd have to use another method or way to find the number/register.
You don't need to read all of above; those were just my attempts to try to produce register-based virtual machine code.
I want to see how you guys would do it or how you would make it.
I am receiving the following response from API, I need to sort this dictionary of dictionary based on the displayOrder key. So as per the below code you can see currently the order is [SALE, OFFER, DISCOUNT, SOLD] which I want to be in order [SALE, SOLD, OFFER, DISCOUNT].
["SALE": {
displayOrder = 1;
segId = 2;
chg = 2;
}, "OFFER": {
displayOrder = 3;
segId = 4;
chg = 2;
}, "DISCOUNT": {
displayOrder = 4;
segId = 1;
chg = 1;
}, "SOLD": {
displayOrder = 2;
segId = 2;
chg = 1;
}]
Simple sort it with sorted function like:
let dic = ["SALE": [
"displayOrder" : 1,
"segId" : 2,
"chg" : 2,
], "OFFER": [
"displayOrder" : 3,
"segId" : 4,
"chg" : 2,
], "DISCOUNT": [
"displayOrder" : 4,
"segId" : 1,
"chg" : 1,
], "SOLD": [
"displayOrder" : 2,
"segId" : 2,
"chg" : 1,
]]
dic.sorted { (lhs, rhs) -> Bool in
lhs.value["displayOrder"]! < rhs.value["displayOrder"]!
}
The net promoter score can have the values 0-10. It is divided in three groups:
Promoters = respondents giving a 9 or 10 score
Passives = respondents giving a 7 or 8 score
Detractors = respondents giving a 0 to 6 score
The score is calculated as the difference between the percentage of Promoters and Detractors.
Let's say we have the scores [10, 9, 10, 6, 2, 5, 10].
This would give the score +14 (57% - 43%).
I wish I could count occurrences of a range in an array, if that would be possible I would do
total_count = array.size
promoters = array.count(9..10)
passives = array.count(7..8)
detractors = array.count(0..6)
promoters_perc = promoters.to_f / total_count * 100
detractors_perc = detractors.to_f / total_count * 100
score = promoters_perc - detractors_perc
How can I do this calculation?
You can count all your metrics in hash:
arr = [10, 9, 10, 6, 2, 5, 10]
count = arr.each_with_object(Hash.new(0)) do |e, memo|
case e
when 0..6 then memo[:detractors] += 1
when 7..8 then memo[:passives] += 1
when 9..10 then memo[:promoters] += 1
end
end
score = (count[:promoters] - count[:detractors]).to_f / arr.size * 100
=> 14.285714285714285
Shorter solution:
metrics = { promoters: 9..10, passives: 7..8, detractors: 0..6 }
count = metrics.each {|k, v| metrics[k] = arr.count {|e| v === e}}
score = (count[:promoters] - count[:detractors]).to_f / arr.size * 100
=> 14.285714285714285
There are a some other ways of doing this as well, but for simplicity this should work.
array = [10, 9, 10, 6, 2, 5, 10]
total_count = array.size
promoters = array.count {|x| x > 8}
passives = array.count {|x| x > 6 && x <9}
detractors = array.count {|x| x < 7}
promoters_perc = promoters.to_f / total_count * 100
detractors_perc = detractors.to_f / total_count * 100
score = promoters_perc - detractors_perc
Here is one more way to do this, code is self explanatory.
data = [10, 9, 10, 6, 2, 5, 10]
score_range = [promoters = (9..10), passives = (7..8), detractors = (0..6)]
#=> [9..10, 7..8, 0..6]
grouped = data.group_by {|i| score_range.select {|sr| sr.cover?(i)} }.to_h
#=> {[9..10]=>[10, 9, 10, 10], [0..6]=>[6, 2, 5]}
percentage = grouped.map {|(k),v| [k, (v.size * 100.0/ data.size).round]}.to_h
#=> {9..10=>57, 0..6=>43}
nps = percentage[promoters] - percentage[detractors]
#=> 14
bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
{name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
{name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}
I have this table, how can I iterate from last index to first? It should break depends from lvl. I just want to show this table from the best weapon. For example if Player have level 53, then I want to show only weapons for his lvl or lower. I need to show that from the best one (at top) its why I want to iterate from the last index. Could someone help?
EDIT:
Thanks for help. There is still a problem that I need this changed table later. It shows all fine but I need to buy all litems from this (changed) list later. So I must replace in some way this 2 tables. Is there any easy way to do that? I tried to remove elements from this table but it still not works.
Or its possible to make some map in Lua? It must be dynamic sized so i cant use table i guess. Something with key - value
A numeric for loop, counting down, is the best option:
local t = {2,4,6,8}
for i = #t, 1, -1 do
print(t[i])
end
Assuming that the table is not necessarily sorted into level order (unlike the example), we need to do two things:
Find which swords are in the level range
Sort them into descending order
Now the first one in the temporary table is the "best" sword.
Like this:
bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
{name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
{name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}
myLevel = 53 -- wanted level
-- temporary table
possible = { }
-- extract ones which are in range
for k, v in ipairs (bestSword) do
if v.lvl <= myLevel then
table.insert (possible, v)
end -- if
end -- for
if #possible == 0 then
print "No matching swords"
else
table.sort (possible, function (a, b) return a.atk > b.atk end )
bestSword = possible [1]
print ("Best sword is", bestSword.name, "lvl =", bestSword.lvl,
"atk = ", bestSword.atk)
end -- if
Or its possible to make some map in Lua? It must be dynamic sized so i cant use table i guess. Something with key - value
Tables in Lua are maps. Every table has key/value pairs. The one you are using there are simply numerically-keyed tables.
All tables are dynamically sized.
Is addControls() compatible with type=pieChart using nPlot()?
The pie chart disappears as soon as I try to add controls.
Im attempting the following:
Sp = c("a", "b", "c", "d")
A = c(10, 4, 3, 0)
B = c(3, 5, 22, 4)
C = c(1, 0 ,10, 8)
df = data.frame(Sp, A, B, C)
p1 <- nPlot(x = "Sp", y ="A" ,
color = 'Sp', data = df, type = 'pieChart')
p1$addControls('y', 'A', values=c('A','B','C'))
p1
It works perfectly if I use multiBarHorizontalChart instead.
Thank you!
On further investigation, I realized that this is a bug in rCharts, which has been fixed on the dev branch. You can install it using devtools::install_github('ramnathv/rCharts#dev').
So running the code below will produce this chart with controls
library(rCharts)
Sp = c("a", "b", "c", "d")
A = c(10, 4, 3, 0)
B = c(3, 5, 22, 4)
C = c(1, 0 ,10, 8)
df = data.frame(Sp, A, B, C)
p1 <- nPlot(x = "Sp", y ="A" , data = df,
group = 'Sp', type = 'pieChart'
)
p1$addControls('y', 'A', values=c('A','B','C'))
p1