What is the efficient way to find all possible increasing sub-sequences of a given length in Scala?
For eg: Suppose there is a sequence of numbers as follows:
7 6 9 11 16 10 12
The possible increasing subsequences of size 3 for the above sequence are :
7 9 11
7 9 16
7 9 10
7 9 12
7 11 16
7 11 12
7 10 12
6 9 11
6 9 16
etc…
I would like to know which Scala data structures would help in finding the sub-sequences quickly.
This is a Scala solution to your question:
def subSequence[A](list: List[A], size: Int)(implicit ord: Ordering[A]): List[List[A]] =
if (size <= 1) {
list.map(List(_))
} else {
( for {
t1 <- list.tails if t1.length >= size - 1
a = t1.head
t2 <- subSequence(t1.tail, size - 1)
b = t2.head if ord.lt(a, b)
} yield a :: t2
).toList
}
Hopefully someone else will provide a tail-recursive version of this which should perform better.
This is one possible solution.
val l = List(7, 6, 9, 11, 16, 10, 12)
l.combinations(3).filter(x => x == x.sorted) // this gives the desired result for size 3 and `3` can be changed to some number `n` for different size of combinations.
I just created combinations and filtered later which might not be very efficient.But, I am interested to know more efficient answers by generating only increasing combinations.
Related
Let's say I've got a function that defines a matrix in terms of it's i and j coordinates:
f: {y+2*x}
I'm trying to create a square matrix that evaluates this function at all locations.
I know it needs to be something like f ' (til 5) /:\: til 5, but I'm struggling with rest.
Rephrasing your question a bit, you want to create a matrix A = [aij] where aij = f(i, j), i, j = 0..N-1.
In other words you want to evaluate f for all possible combinations of i and j. So:
q)N:5;
q)i:til[N] cross til N; / all combinations of i and j
q)a:f .' i; / evaluate f for all pairs (i;j)
q)A:(N;N)#a; / create a matrix using #: https://code.kx.com/q/ref/take/
0 1 2 3 4
2 3 4 5 6
4 5 6 7 8
6 7 8 9 10
8 9 10 11 12
P.S. No, (til 5) /:\: til 5 is not exactly what you'd need but close. You are generating a list of all pairs i.e. you are pairing or joining the first element of til 5 with every element of (another) til 5 one by one, then the second , etc. So you need the join operator (https://code.kx.com/q/ref/join/):
(til 5),/:\: til 5
You were close. But there is no need to generate all the coordinate pairs and then iterate over them. Each Right Each Left /:\: manages all that for you and returns the matrix you want.
q)(til 5)f/:\:til 5
0 1 2 3 4
2 3 4 5 6
4 5 6 7 8
6 7 8 9 10
8 9 10 11 12
I have been investigating various collision resolution techniques for hashtables implemented via open addressing. However, all the collision resolution methods I have investigated so far (linear probing, quadratic probing, double hashing) have the pitfall that there exists a probing sequence that produces a cycle whose length is less than the size of the table. This becomes problematic when you're trying to insert an element with the open addressing scheme because there are free buckets to insert an entry but they might not be reachable if they're not part of the cycle.
For example, if we're using linear probing on a table of size 12 with the linear function: H(k, i) = (h(k) + 4*i) mod 12 then a cycle would occur if a particular key hashes to 8 and all the slots 0, 4, and 8 are already filled:
H(k, 0) = 8 + 0 mod 12 = 8
H(k, 1) = 8 + 4 mod 12 = 0
H(k, 2) = 8 + 8 mod 12 = 4
H(k, 3) = 8 + 12 mod 12 = 8
H(k, 4) = 8 + 16 mod 12 = 0
H(k, 5) = 8 + 20 mod 12 = 4
H(k, 6) = 8 + 24 mod 12 = 8
...
Similar cycles can also be found with quadratic and double hashing if the probing sequence is bad, so my question then is how are cycles handled? Or do we always pick hash functions/special table sizes which do not permit cycles which are too short?
This question already has answers here:
Generate a matrix containing all combinations of elements taken from n vectors
(4 answers)
Closed 8 years ago.
I have n states like 10, 15, 18, ... and I want to generate all possible combinations of my states in an array with length of m.
For example, if m=5 and n=2 (states={10,15}), I want to generate all possible combinations like this example:
comb = { 10 10 10 10 15
10 10 10 15 10
10 10 10 15 15
...
15 15 15 15 10 }
Each row should consist of one possible combination of 10, 15. How to generate this array in MATLAB?
One approach is:
m = 4;
n = 2;
states = [10, 15];
v = mod(perms(1:(2*(m-1))),n) + 1;
ind = unique(v(:,1:m),'rows');
comb = states(ind)
comb =
10 10 10 15
10 10 15 10
10 10 15 15
10 15 10 10
10 15 10 15
10 15 15 10
10 15 15 15
15 10 10 10
15 10 10 15
15 10 15 10
15 10 15 15
15 15 10 10
15 15 10 15
15 15 15 10
Note that this has horrible performance for high values of m and n).
Try something like:
n = 5;
states = [10,15];
m = numel(states);
sz = m*ones(1,n);
idx = {0};
idx = repmat(idx,1,n);
comb = zeros(m^n,n);
for i = 1:(m^n)
[idx{:}] = ind2sub(sz,i);
comb(i,:) =states([idx{:}]);
end
This will give you all possible combinations and put them in the matrix comb; then you can sort out all the unwanted combinations afterwards - it is unclear which combinations you aren't interested in (it could for example be the combinations with not all elements of the same state, or the combinations with at least one of each of the states represented).
I have a matrix in Matlab and want to find the indeces of all rows, where some of the columns meet a specific criteria.
Example
M =
1 5 9 13
2 6 10 14
10 14 11 15
4 8 10 14
Now I want to find the incedeces of all rows, where M(:,3) == 10 AND M(:,4) == 14.
The result should be:
R =
0
1
0
1
I though about something like
find(ismember(M,[* * 10 14]),1)
but ismember() won't work with wildcars.
R = (M(:,3) == 10 & M(:,4) == 14);
should be sufficient.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find multiple of a number that can be written with 1s and 0s
Given the number n (2 <= n <= 1000), find the lowest nonzero multiple of which is written in base 10 with digits 0 and 1 only. Examples: 2 -> 10, 3 -> 111, 4 -> 100, 7 -> 1001, 11 -> 11, 9 -> 111 111 111.
I think, follow the remaining division of numbers consist of numbers n which is formatted 0/1.Thanks for your help!
{10/3= 3 remaining 1 -> and the finaly is 111 !!!
10/4= 4 ramining 2 -> and the finaly is 100 !!!
10/6= 1 ramainin 4 -> and the finaly is 1110 !!!
I don't understand is the logic}
The question is basically saying: Find the first non-zero multiple of n that consists of only 1s and 0s. And we're not talking binary (base 2) or remainders or anything fancy here. Here are some examples, in the format:
n => The first multiple of n with only 1s and 0s is x (n * y = x)
--------------------------------------------------------------------------
2 => (2 x 5 = 10)
3 => (3 x 37 = 111)
4 => (4 x 25 = 100)
7 => (7 x 143 = 1001)
11 => (11 x 1 = 11)
9 => (9 x 12,345,679 = 111,111,111)
You need to figure out an algorithm to make it work! You could use brute force:
for each n between 2 and 1000
y = 1
x = 0
do
x = n * y++
while (x is not 0 and string(x) is not all 0s and 1s)
print n => x
next n
I implemented this in C# to test it and it gave me the following output for n between 2 and 10:
2 => 10
3 => 111
4 => 100
5 => 10
6 => 1110
7 => 1001
8 => 1000
9 => 111111111
10 => 10
There are probably faster implementations, but this should give you an idea of where to start.
If you're asking for help interpreting the (homework) question, here's what I think it means: "For a given number, find out lowest multiple of it that contains only digits 1 or 0"
So, for example, if the number is 2:
Multiples of 2 = {2, 4, 6, 8, 10, 12, 14, .... }
|
|
this is your answer!
The non-bruteforce way to do this would be to iterate throught numbers that contain only 0 and 1 then figure out if the number is a multiple of the number in question. This approach will be substantially more efficient than iterating through the multiples of n and determining if it contains only 0 and 1.
An easy way to get a list of numbers that contain only 0 and 1 would be iterate throught the integers and for each value, interpret its binary representation as a decimal number.
Here's an online demo to get you started: http://jsfiddle.net/6j5De/4/
Since it's likely to be homework, I'll leave it up to you to translate that to your subject language.