Is there a way, using ClickHouse's lambdas, to perform a max function over two integers ?
Like so:
SELECT
[0,1,2,3,4,5] as five,
arrayMap(i -> max(five[i], 3), arrayEnumerate(five)) as X
Returns
five expected X
0,1,2,3,4,5 3,3,3,3,4,5
Not sure I understand your example (it is not syntactically correct), but for max over two integers ClickHouse has function greatest(x,y):
SELECT
[0, 1, 2, 3, 4, 5] AS five,
arrayMap(i -> greatest(i, 3), five) AS X
┌─five──────────┬─X─────────────┐
│ [0,1,2,3,4,5] │ [3,3,3,3,4,5] │
└───────────────┴───────────────┘
Related
I'm writing a simple interpreter that people can use (among other things) to sort a list via a comparison function (preferably a stable sort). Sorting algorithms that I'm familiar with all seem to require a variable number of calls to that comparison function and you don't know ahead of time which items will be compared to each other. That won't work because of the nature of what work is done in the interpreted language vs the runtime at what times.
The steps required by the interpreter are:
Step 1: Create a list of as to be compared to another list of bs, one a & b at a time. Something like sort([1, 2, 3]) producing:
a = [2, 3]
b = [1, 2]
(2 is compared to 1 and then 3 is compared to 2 in the above example, going index by index.)
Step 2: Create two new lists (before and after) with the same number of items as a and b to represent the result of the comparison function. The values are any null or non-null value. Something like:
before = [2, 3]
after = [null, null]
(2 should come before 1, representing 1 from b as null. The non-null values are preserved, but any non-null value could be in 2's place.)
I can impose a limitation that the values in before and after must be items from the lists a and b, but I'd prefer not to if I possibly can. I mention it because I'm unsure how I could know where the non-null value came from (a or b). But if the items compared from a and b are the same but only one is null at the end, I have the same problem.
Step 3: Use those two lists before and after to sort the original list. Something like:
sort([1, 2, 3], greaterThan) => [3, 2, 1]
// a = [2, 3]
// b = [1, 2]
// before = [2, 3]
// after = [null, null]
(If both values are non-null or both null, it should favor their original order relative to each other, or a "stable" sort.)
In such a trivial example, the items in a and b are sufficient to sort the list. The Javascript (the language the interpreter is written in) Array.sort method will compare them like this:
(2, 1)
(3, 2)
and be done. But if the order of the original list were [2, 3, 1] then it has to do:
(3, 2)
(1, 3)
(1, 2)
(1, 3)
(I don't know why or what algorithm they use).
In that example, I would have to provide lists a and b as [3, 1, 1, 1] and [2, 3, 2, 3] respectively.
How do I get a list for a and b that will work given any comparison function or order of the original list -- and then use the resulting before and after lists to sort that original list?
I'm trying to generate some Kakuros, generate not solve.
I have all rules to generate it, but the firsts results are senseless, those are like squares.
Now I want to skip some branches, 15000 for example, to see the kakuro generated at that point.
I have tried with an Auxiliary Variable, but when it fails, the Kakuro Generator start again.
You can keep a dynamic counter-like predicate in the knowledge base that gets increased every time the main predicate is executed. The value of the counter is changed with assert and retract, i.e., it is not a variable within your main predicate but a globally stored value.
Within your main predicate, if you add the condition that the counter should be higher than some skip value, then you force backtracking over the actual rules for a specified number of iterations.
As an example, consider the built-in predicate permutation/2 which computes permutations of a list (note: tested using SWI-Prolog, other interpreters have different built-in predicates). Example output:
?- permutation([1,2,3,4,5],L).
L = [1, 2, 3, 4, 5] ;
L = [1, 2, 3, 5, 4] ;
L = [1, 2, 4, 3, 5] ;
L = [1, 2, 4, 5, 3] ;
L = [1, 2, 5, 3, 4] ;
L = [1, 2, 5, 4, 3] ;
If you want to skip the first 5 iterations in your query, you can use the following code:
:- dynamic iteration_nr/1.
iteration_nr(0).
get_permutations(L1,L2,Skip) :-
permutation(L1,L2),
iteration_nr(N),
N2 is N+1,
retract(iteration_nr(N)),
asserta(iteration_nr(N2)),
Skip < N2. % force backtracking here if counter < Skip
Example output:
?- get_permutations([1,2,3,4,5],L2,5).
L2 = [1, 2, 5, 4, 3] ;
L2 = [1, 3, 2, 4, 5] ;
L2 = [1, 3, 2, 5, 4]
Note that asserta is used here (i.e., assert at the start) instead of plain assert, which is deprecated. Note also that the counter will keep the value, so when you run this a second time in the same session the results will be different. To reset the counter you can use a separate initialization predicate, for example:
init_and_get_permutations(L1,L2,Skip) :-
retractall(iteration_nr(_)),
asserta(iteration_nr(0)),
get_permutations(L1,L2,Skip).
Further note: the use of assert and retract is not really considered 'clean' Prolog programming, because it is procedural and changes the knowledge base. However, for some applications it can be useful.
I want to get number of permutations of {1, ..., n} for which Insertion Sort does exactly n(n-1)/2 comparisions.
For example, for {1, 2, 3, 4} we got (4, 3, 2, 1), (3, 4, 2, 1), (4, 2, 3, 1) etc. - for all of them InsertionSort does 4*3/2 = 6 comparisions.
Anybody know some exact formula for that?
I am thinking about something like (n-1) + 1 = n, where
1 stands for reverse sequence and then we can swap all of (n-1) pairs in reverse sequence.
Here is a hint. The complete list for (1, 2, 3, 4) are:
(4, 3, 2, 1)
(3, 4, 2, 1)
(4, 2, 3, 1)
(2, 4, 3, 1)
(4, 3, 1, 2)
(3, 4, 1, 2)
(4, 1, 3, 2)
(1, 4, 3, 2)
Look at it from last column to first.
Walk step by step through the insertion sorts. See where they merge. Do you see a pattern there?
Reversing it, can you figure out how I generated this list? Can you prove that the list is complete?
The why is what matters here. Just saying 2n-1 is useless.
n(n-1)/2 is the sum of all elements in the range (1, n - 1). Since your sequence has length n, you can expand that range to (0, n - 1).
The number of swaps for each insertion would be:
run # list value swaps
1 [] a 0 (no swaps possible)
2 [a] b 1
3 [b, a] c 2
...
10 [i,...,a] j 9
...
n [...] ? n - 1
So we need to move every element through the entire list in order to achieve the required count of swaps. The number of comparisons can be at most one higher than the number of swaps, which means each value that is being inserted must either be placed at the first or second index of the resulting list. Or
Put differently, assuming ascending ordering of the output:
The input list should in general be a nearly descending list, where each element in the list may be preceded by at most one element that is not larger than the element in question.
Introduction
While trying to do some cathegorization on nodes in a graph (which will be rendered differenty), I find myself confronted with the following problem:
The Problem
Given a superset of elements S = {0, 1, ... M} and a number n of non-disjoint subsets T_i thereof, with 0 <= i < n, what is the best algorithm to find out the partition of the set S called P?
P = S is the union of all disjoint partitions P_j of the original superset S, with 0 <= j < M, such that for all elements x in P_j, every x has the same list of "parents" among the "original" sets T_i.
Example
S = [1, 2, 3, 4, 5, 6, 8, 9]
T_1 = [1, 4]
T_2 = [2, 3]
T_3 = [1, 3, 4]
So all P_js would be:
P_1 = [1, 4] # all elements x have the same list of "parents": T_1, T_3
P_2 = [2] # all elements x have the same list of "parents": T_2
P_3 = [3] # all elements x have the same list of "parents": T_2, T_3
P_4 = [5, 6, 8, 9] # all elements x have the same list of "parents": S (so they're not in any of the P_j
Questions
What are good functions/classes in the python packages to compute all P_js and the list of their "parents", ideally restricted to numpy and scipy? Perhaps there's already a function which does just that
What is the best algorithm to find those partitions P_js and for each one, the list of "parents"? Let's note T_0 = S
I think the brute force approach would be to generate all 2-combinations of T sets and split them in at most 3 disjoint sets, which would be added back to the pool of T sets and then repeat the process until all resulting Ts are disjoint, and thus we've arrived at our answer - the set of P sets. A little problematic could be caching all the "parents" on the way there.
I suspect a dynamic programming approach could be used to optimize the algorithm.
Note: I would have loved to write the math parts in latex (via MathJax), but unfortunately this is not activated :-(
The following should be linear time (in the number of the elements in the Ts).
from collections import defaultdict
S = [1, 2, 3, 4, 5, 6, 8, 9]
T_1 = [1, 4]
T_2 = [2, 3]
T_3 = [1, 3, 4]
Ts = [S, T_1, T_2, T_3]
parents = defaultdict(int)
for i, T in enumerate(Ts):
for elem in T:
parents[elem] += 2 ** i
children = defaultdict(list)
for elem, p in parents.items():
children[p].append(elem)
print(list(children.values()))
Result:
[[5, 6, 8, 9], [1, 4], [2], [3]]
The way I'd do this is to construct an M × n boolean array In where In(i, j) = Si ∈ Tj. You can construct that in O(Σj|Tj|), provided you can map an element of S onto its integer index in O(1), by scanning all of the sets T and marking the corresponding bit in In.
You can then read the "signature" of each element i directly from In by concatenating row i into a binary number of n bits. The signature is precisely the equivalence relationship of the partition you are seeking.
By the way, I'm in total agreement with you about Math markup. Perhaps it's time to mount a new campaign.
I am working on a simple combinatorics part, and found that I need to recover position of two bits given position of other two bits in 4-bits srring.
for example, (0,1) maps to (2,3), (0,2) to (1,3), etc. for a total of six combinations.
My solution is to test bits using four nested ternary operators:
ab is a four bit string, with two bits set.
c = ((((ab & 1) ? (((ab & 2) ? ... ))) : 0)
abc = ab | c
recover the last bit in the same fashion from abc.
I have to clarify, without using for loops, my target language is C++ meta-programming templates. I know I specified language explicitly, but it's still agnostic in my opinion
can you think of a better way/more clever way?
thanks
Just xor the value with binary 1111 - this will flip the four bits, giving you the other two.
cd = ab ^ 0xF;
The problem space is rather small, so a LUT-based solution is fast and easy.
Python:
fourbitmap = {
3: (2, 3),
5: (1, 3),
6: (0, 3),
9: (1, 2),
10: (0, 2),
12: (0, 1),
}
def getother2(n):
return fourbitmap.get(n, None)
Python:
def unset_bits(input=0x5):
for position in range(4):
if not (2**position) & input:
yield position
Yields:
>>> list( unset_bits(0x1) )
[1, 2, 3]
>>> list( unset_bits(0x2) )
[0, 2, 3]
>>> list( unset_bits(0x3) )
[2, 3]