Tag Archives: q

Generate all permutations in q/KDB+

My solution : perm:{[s] $[(count s)=1;s;[p:(rotate[1]\)s;raze((string first each p),/:’perm each 1_/:p)]]} Results : perm “a” “a” perm “ab” “ab” “ba” perm “abc” “abc” “acb” “bca” “bac” “cab” “cba” The kdb+ personal developers blog contains a thread on generating all the … Continue reading

Posted in Q, Regular Expressions | Tagged , | 2 Comments

Exponential Smoothing in q/KDB+

The formula for exponential smoothing is : S(1) = Y(0) S(t) = Alpha * Y(t) + (1-Alpha) S(t-1) 1) The corresponding solution in Q is : es:{{(x*z)+(1-x)*y}[x]\[y]} where, x is the smoothing factor(Alpha) and y is the list containing the … Continue reading

Posted in Pricing, Q, Regression, Time Series | Tagged , , | Leave a comment

Greatest Common Divisor in Q/KDB+

Euclid’s algorithm : Recursive definition of the gcd function using Euclid’s algorithm is : function gcd(a, b) { if (b = 0) return a else return gcd(b, a mod b) } Solution in Q : g:{$[y=0;:x;:g[y;x mod y]];} Results : … Continue reading

Posted in Q | Tagged , , | Leave a comment

Subvector grade down – Q Idioms 15

x:1 0 0 1 0 0 1 0 y:14 12 18 15 13 16 11 17 K solution : {,/x+’>:’x _ y}[&x;y] 2 0 1 5 3 4 7 6 Solution Posted : {raze x +’ idesc each x _ … Continue reading

Posted in Q, Q Idioms | Tagged , , | Leave a comment

Subvector grade up – QIdiom 7

x:1 0 0 1 0 0 1 0 y:14 12 18 16 13 15 11 17 Solution in K : {,/x+'<:’x _ y}[&x;y] 1 0 2 4 3 5 6 7 Solution Posted : {raze x +’ iasc each x … Continue reading

Posted in Q, Q Idioms | Tagged , , | Leave a comment

Factorial Program

Q Solution 1:  factorial:{prd 1+til x} Q Solution 2: n:5 do[-1+f:r:n;r*:f-:1] r 120 The solution 2 is referred from Execution Control chapter in Q for Mortals. Results : factorial[5] 120

Posted in Q | Tagged , , | 1 Comment