Hello new here in stackoverflow, also I am new with the programation in Python and still learning.
I want to know why I am getting a Syntax error in the second for loop, I am trying to compare two arrays of the same lenght, when ax > bx A recive 1 point, when ax < bx B recive 1 point and where ax == bx nobody get points.
def solve(a0, a1, a2, b0, b1, b2):
A = 0
B = 0
a = [a0 , a1 ,a2]
b = [b0, b1, b2]
for x in a and for y in b:
if x > y:
pointA + 1
if x==y:
pass
else:
pointB + 1
result = [pointA, pointB]
return result
a0, a1, a2 = raw_input().strip().split(' ')
a0, a1, a2 = [int(a0), int(a1), int(a2)]
b0, b1, b2 = raw_input().strip().split(' ')
b0, b1, b2 = [int(b0), int(b1), int(b2)]
result = solve(a0, a1, a2, b0, b1, b2)
print " ".join(map(str, result))
then with some Investigation I tried:
from itertools import product
import sys
def solve(a0, a1, a2, b0, b1, b2):
A = 0
B = 0
a = [a0 , a1 ,a2]
b = [b0, b1, b2]
A = sum(1 if x>y else 0 for x, y in product(a, b))
B = sum(1 if x<y else 0 for x, y in product(a, b))
result = [A, B]
return result
a0, a1, a2 = raw_input().strip().split(' ')
a0, a1, a2 = [int(a0), int(a1), int(a2)]
b0, b1, b2 = raw_input().strip().split(' ')
b0, b1, b2 = [int(b0), int(b1), int(b2)]
result = solve(a0, a1, a2, b0, b1, b2)
print " ".join(map(str, result))
but when the Input is:
1 1 1
0 0 0
I got:
9 0
Can someone explain what I am doing wrong and why? Thank you in advance.
Regards,
R
and expects boolean values not expressions, so this: for x in a and for y in b: will never work.
You could use zip():
1 a
2 b
3 c
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> for x, y in zip(a, b):
... print('{} {}'.format(x, y))
...
1 a
2 b
3 c
And please correct your indentations.
Related
I have this sample code that creates a band diagonal matrix
T = 6;
d1 = ones(T-2, 1);
d2 = 2*ones(T-1, 1);
d3 = 3*ones(T, 1);
f = sparse(diag(d1, -2) + diag(d2, -1) + diag(d3) + diag(d2, 1) + diag(d1, 2));
but I want to avoid creating the full TxT matrix, so I want to use spdiags, like this:
f2 = spdiags(d1, -2, T, T) + spdiags(d2, -1, T, T) + spdiags(d3, 0, T, T) + ...
spdiags(d2, 1, T, T) + spdiags(d1, 2, T, T);
matlab tells me that "index exceeds matrix dimensions" and the problem comes from these commands
spdiags(d2, 1, T, T)
spdiags(d3, 2, T, T)
but these commands work normally:
spdiags(d1, -2, T, T)
spdiags(d2, -1, T, T)
What's going on here? The final matrix should look like the sparse form of this:
f =
3 2 1 0 0 0
2 3 2 1 0 0
1 2 3 2 1 0
0 1 2 3 2 1
0 0 1 2 3 2
0 0 0 1 2 3
Also these are sample matrices that Im using as examples only.
This code works too:
T = 6;
d1 = ones(T-2, 1);
d2 = 2*ones(T-1, 1);
d3 = 3*ones(T, 1);
f = sparse(diag(d1, -2) + diag(d2, -1) + diag(d3) + diag(d2, 1) + diag(d1, 2));
B = [[d1;0;0], [d2;0], d3, [0;d2], [0;0;d1]];
f2 = spdiags(B, -2:2, T, T);
The documentation is not very clear. It looks like you need your d vector to be of length T, even if some values will be ignored (namely, the first values are ignored for positive diagonals, and the last are ignored for negative diagonals). But somehow Matlab only actually complains for positive diagonals; for negative diagonals it does accept shorter vectors.
So: use all d vectors of length T:
T = 6;
d1 = ones(T, 1);
d2 = 2*ones(T, 1);
d3 = 3*ones(T, 1);
f2 = spdiags(d1, -2, T, T) + spdiags(d2, -1, T, T) + spdiags(d3, 0, T, T) + ...
spdiags(d2, 1, T, T) + spdiags(d1, 2, T, T);
By the way, you can build a matrix containing all d vectors as columns (now that they all have the same length) and call spdiags just once:
f2 = spdiags([d1 d2 d3 d2 d1], -2:2, T, T);
Two arrays A,B are given of same length (not sorted).
Make pairs of their entries (one from A and one from B), such that average difference in the entries (|a1-b1|,|a2-b2|,....) is minimum.
I have thought of sorting them and then making pairs of same index entries.
Will this work ?
If yes, how ? else some other solution with proof.
I'm assuming that this problem can be formally stated as follows: given two n-element vectors A and B, find permutations A' of A and B' of B so as to minimize the L1 norm of A' - B'.
If so, then your proposed algorithm is correct. Suppose that we have a solution with an inversion, that is, a1 matched with b2 and a2 matched with b1 such that a1 < a2 and b1 < b2. The contribution of these pairs to the L1 norm is
|a1 - b2| + |a2 - b1| >= |a1 - b1| + |a2 - b2|,
where the inequality follows from an inelegant case argument that we defer for the moment. Accordingly, by rematching a1 with b1 and a2 with b2, we decrease the number of inversions without increasing the cost. It follows by induction that the matching with no inversions is optimal.
Case argument: symmetrically (by interchanging A and B), there are three possible interleavings.
(1) a1 <= a2 <= b1 <= b2
(2) a1 <= b1 <= a2 <= b2
(3) a1 <= b1 <= b2 <= a2
In case (1),
|a1 - b2| + |a2 - b1| = b2 - a1 + b1 - a2
= b1 - a1 + b2 - a2
= |a1 - b1| + |a2 - b2|.
In case (2),
|a1 - b2| + |a2 - b1| = b2 - a1 + a2 - b1
>= b2 - a1 + a2 - b1 - 2 (a2 - b1)
= b1 - a1 + b2 - a2
= |a1 - b1| + |a2 - b2|.
In case (3),
|a1 - b2| + |a2 - b1| = b2 - a1 + a2 - b1
>= b2 - a1 + a2 - b1 - 2 (b2 - b1)
= b1 - a1 + a2 - b2
= |a1 - b1| + |a2 - b2|.
Edit: misread the question, the below describes why the sum of differences is maximized when you sort the two lists in opposite order
Here is another way to see that the algorithm you described gives you the optimal solution. Note that when you write out |x - y| it equals either x - y or y - x. Thus,
|a_1 - b_1| + |a_2 - b_2| + ... + |a_n - b_n|
can be thought of as summing the numbers a_1, ..., a_n, b_1, ... , b_n, except that exactly n of them have their sign flipped. Clearly, this is maximized when you choose to negate the n smallest numbers out of a_1, ..., a_n, b_1, ..., b_n. And indeed, that is exactly what your algorithm does.
Suppose I have 1D RGB array of the following structure:
I = [r1 r2 ... rN; g1 g2 ... gN; b1 b2 ... bN];
where
N = H*W;
ans H and W are height and width of the picture respectively.
How to reshape it to colored image format HxW, which is represented by 3D matrix so that
I2(1,1,1) = r1
I2(1,1,2) = g1
I2(1,1,3) = b1
I2(2,1,1) = r2
I2(2,1,2) = g2
I2(2,1,3) = b2
...
I2(H,W,1) = rN
I2(H,W,2) = gN
I2(H,W,3) = bN
(if I am correct thinking that normal 1D -> 2D reshape works by columns)
UPDATE
This reshaping can be done the following way
R = I(1,:);
R = reshape(R,H,W);
G = I(2,:);
G = reshape(G,H,W);
B = I(3,:);
B = reshape(B,H,W);
I2 = cat(3, R, G, B);
Can it be done shorter, with one reshape call for example?
I think that what you're looking for is: reshape(I', H, W, 3)
Ok, Say, I got a table that have 3 columns: c1, c2, c3
C1 - C2 - C3
A2 - B2 - N2
K1 - B2 - N1
K1 - B3 - N1
L1 - A2 - C1
Ok, when users search for any combination of A1, A2, A3, B1, B2, ... then the system can be able to pick the rows with the closet match (it means as long as the word appears in 1 column, the system will pick it, the more words appear in more columns the closer it matches) & order them according to the closest match
Ex1: a user searches for "K1 A2 C1 N1", the system will show:
K1 - B2 - N1
K1 - B3 - N1
A2 - B2 - N2
L1 - A2 - C1
Ex2: a user searches for "K1 A2 C1 N2 B2", the system will show:
A2 - B2 - N2
L1 - A2 - C1
K1 - B2 - N1
K1 - B3 - N1
My solution is to split the search string into separate words & then search each of these words against the columns in the table. But I am not sure it is the optimum query since the DB have to search in many loops.
So if u r expert in DB, then what is the best query in this scenario?
select c1
,c2
,c3
,case when c1 in ('K1','A2','C1','N1') then 1 else 0 end
+case when c2 in ('K1','A2','C1','N1') then 1 else 0 end
+case when c3 in ('K1','A2','C1','N1') then 1 else 0 end as sortweight
from theTable
where c1 in ('K1','A2','C1','N1')
or c2 in ('K1','A2','C1','N1')
or c3 in ('K1','A2','C1','N1') order by sortweight desc
,c1
,c2
,c3
Someone from other forum suggested the above query
I test the code
L:=[2,1];
sum('a||b*L[1]', 'b' = 1 .. 2);
It returns a1*L[1]+a2*L[1], but I expect to get a1*2+a2*2 after evaluation of L[1].
Any ideas?
Thanks.
EDIT:
I have one further question. Here's the test code:
L := [2, 1]
test := proc (i) local a1; a1 := 1; add(a || b*L[i], b = 1 .. 2) end proc
test(1);
will result
2 a1 + 2 a2
without evaluating a1 which is a local variable defined in function test.
I expect to get 2*1+2*a2. Any further idea?
Your first line is just an equation, with =, and not an actual assignment with :=. So you weren't doing an assignment to L.
And the uneval quotes are misapplied, in the sum call, and wrap too much.
You could also use add instead of sum, to rely on the special evaluation rules of add and thus get rid of the need for the uneval quotes.
> L:=[2,1];
L := [2, 1]
> add(cat(a,b)*L[1], b = 1 .. 2);
2 a1 + 2 a2
> add((a||b)*L[1], b = 1 .. 2);
2 a1 + 2 a2
> sum('a||b'*L[1], 'b' = 1 .. 2);
2 a1 + 2 a2
> sum('cat(a,b)'*L[1], 'b' = 1 .. 2);
2 a1 + 2 a2