如何用基於數組的語言K(或Q)表達這個命令函數?如何用一個基於數組的語言(如K(或Q))來表示這個命令函數?
在馬虎C++:
vector<int> x(10), y(10); // Assume these are initialized with some values.
// BTW, 4 is just a const -- it's part of the algorithm and is arbitrarily chosen.
vector<int> result1(x.size() - 4 + 1); // A place to hold a resulting array.
vector<int> result2(x.size() - 4 + 1); // A place to hold another resulting array.
// Here's the code I want to express functionally.
for (int i = 0; i <= x.size() - 4; i++) {
int best = x[i + 0] - y[i + 0];
int bad = best;
int worst = best;
for(int j = 0; j < 4; j++) {
int tmp = x[i + j] - y[i + 0];
bad = min(bad, tmp);
if(tmp > best) {
best = tmp;
worst = bad;
}
}
result1[i] = best
result2[i] = worst
}
我最想看到這個kdb和Q,但其他功能的語言是受歡迎的。
這段代碼試圖做什麼? –
它試圖計算兩件事:首先,在x中的每個點上,我們希望在接下來的4個元素上有最大值(讓我們稱之爲Nx = max(x),在位置Px處,其中Px = 0..3。Second ,在x的每個點上,我們都希望下一個Px點的最小值。 – Badmanchild