print string n times, where n= (field in first line)-1 - bash

I have a file like this:
X 7 1 3
X 8 1 4
X 9 1 6
X 13 2 8
X 20 6 11
Y 13 2 8
Y 19 6 10
Y 20 6 11
basically if we call column 2 of the first line 'n', I want to add a string to the top n-1 times so that the output is:
X 1 0 0
X 2 0 0
X 3 0 0
X 4 0 0
X 5 0 0
X 6 0 0
X 7 1 3
X 8 1 4
X 9 1 6
X 13 2 8
X 20 6 11
Y 13 2 8
Y 19 6 10
Y 20 6 11
Note that column 1 line 1 is variable. Is there a way to do this in awk?

awk to the rescue!
$ awk 'NR==1{for(i=1;i<$2;i++) print $1, i, 0, 0} 1' file | column -t
X 1 0 0
X 2 0 0
X 3 0 0
X 4 0 0
X 5 0 0
X 6 0 0
X 7 1 3
X 8 1 4
X 9 1 6
X 13 2 8
X 20 6 11
Y 13 2 8
Y 19 6 10
Y 20 6 11

Related

How to find the index of the first row of a matrix that satisfies two conditions in APL Language?

One more question to learn how to use APL Language.
Suppose you have an array, as an example:
c1
c2
c3
c4
c5
c6
3
123
0
4
5
6
3
134
0
2
3
4
3
231
180
1
2
5
4
121
0
3
2
4
4
124
120
4
6
3
4
222
222
5
3
5
So, how to find out which row has a value of 4 in the 1st column and a value grather than 0 in the 3rd column?
The expected answer is 5th line, in the just 5
When you want to make such "queries", think Boolean masks.
table ← 6 6⍴3 123 0 4 5 6 3 134 0 2 3 4 3 231 180 1 2 5 4 121 0 3 2 4 4 124 120 4 6 3 4 222 222 5
Let's extract the first column:
table[;1]
3 3 3 4 4 4
And indicate which elements have a value of 4:
table[;1] = 4
0 0 0 1 1 1
Similarly, we can indicate which elements of column 3 have value greater than 0:
table[;3] > 0
0 0 1 0 1 1
Their intersection (logical AND) indicates all rows that fulfil your criteria:
(table[;1] = 4) ∧ (table[;3] > 0)
0 0 0 0 1 1
The index of the first 1 is the row number for the first row that fulfils your criteria:
((table[;1] = 4) ∧ (table[;3] > 0)) ⍳ 1
5
Try it online!
Alternatively, we can use the final mask to filter the table and obtain all rows that fulfil your criteria:
((table[;1] = 4) ∧ (table[;3] > 0)) ⌿ table
4 124 120 4 6 3
4 222 222 5 3 5
Try it online!
Or we can generate all the row numbers:
⍳ 1 ↑ ⍴ table
1 2 3 4 5 6
Then use our Boolean mask to filter that, finding the row numbers of all the rows that fulfil your criteria:
((table[;1] = 4) ∧ (table[;3] > 0)) ⌿ ⍳ 1 ↑ ⍴ table
5 6
Try it online!

Minesweeper puzzle

I'm creating a minesweeper solver, but it's unable to solve this puzzle and nor can I figure out how to do it by hand.
Is this puzzle possible without guessing?
1 1 1 0
1 x 2 1
1 2 ? ?
0 1 ? ?
1 2 2 ?
x 2 ? ?
1 2 ? ?
There are 3 mines remaining.
Here are two possibilities I can up with:
1 1 1 0
1 x 2 1
1 2 x 1
0 1 1 1
1 2 2 2
x 2 x x
1 2 2 2
1 1 1 0
1 x 2 1
1 2 3 x
0 1 x 2
1 2 2 2
x 2 x 1
1 2 1 1
It is not possible without guessing. Here are two (of several) solved grids that match your example.
1 1 1 0
1 x 2 1
1 2 x 1
0 1 2 2
1 2 2 x
x 2 x 2
1 2 1 1
1 1 1 0
1 x 2 1
1 2 2 x
0 1 x 2
1 2 2 2
x 2 2 x
1 2 x 2

plotting multiple graphs and animation from a data file in gnuplot

Suppose I have the following sample data file.
0 1 2
0 3 4
0 1 9
0 9 2
0 19 0
0 6 1
0 11 0
1 3 2
1 3 4
1 1 6
1 9 2
1 15 0
1 6 6
1 11 1
2 3 2
2 4 4
2 1 6
2 9 6
2 15 0
2 6 6
2 11 1
first column gives value of time. Second gives values of x and 3rd column y. I wish to plot graphs of y as functions of x from this data file at different times,
i.e, for t=0, I shall plot using 2:3 with lines up to t=0 index. Then same thing I shall do for the variables at t=1.
At the end of the day, I want to get a gif, i.e, an animation of how the y vs x graph changes shape as time goes on. How can I do this in gnuplot?
What have you tried so far? (Check help ternary and help gif)
You need to filter your data with the ternary operator and then create the animation.
Code:
### plot filtered data and animate
reset session
$Data <<EOD
0 1 2
0 3 4
0 1 9
0 9 2
0 19 0
0 6 1
0 11 0
1 3 2
1 3 4
1 1 6
1 9 2
1 15 0
1 6 6
1 11 1
2 3 2
2 4 4
2 1 6
2 9 6
2 15 0
2 6
2 11 1
EOD
set terminal gif animate delay 50 optimize
set output "myAnimation.gif"
set xrange[0:20]
set yrange[0:10]
do for [i=0:2] {
plot $Data u 2:($1==i?$3:NaN) w lp pt 7 ti sprintf("Time: %g",i)
}
set output
### end of code
Result:
Addition:
The meaning of $1==i?$3:NaN in words:
If the value in the first column is equal to i then the result is the value in the third column else it will be NaN ("Not a Number").

Tree searching algorithm: how to determine quickly if A has a sure-to-win strategy

The original question goes like this: There are 99 stones, A and B are playing a game that, each one take some stones in turn, and each turn one can only take 1, 2, 4, or 6 stones, the one take the last stone wins. If A is the first one to take stones, how many stones shall A take in the first turn?
This seems a quite complex tree searching quiz, listing out all the branches, then work it bottom up: the leaf with A taking the last stone is marked as "win"; for the intermediate node that whatever strategies B might take, if A always has a way to reach a node marked as "win", this node is also marked as "win".
But this approach is quite time consuming. Is there any smart algorithm to check out if A has a "guaranteed to win" strategy?
O(n) solution
If we start with 1, 2, 4 or 6 stones, A will always win, because he'll just take them all in the first move.
If we start with 3, A will lose no matter what he does, because regardless of whether he takes 1 or 2, B will take 2 or 1 next and win.
If we start with 5, A will win by taking 2 first, thus sending B to the case above, where he starts with 3 stones.
If we start with 7, A will win by taking 4, sending B to the same case with 3.
If we start with 8, A will lose no matter what he does: whatever he takes, he will send B to a winning position.
If we start with 9, A can take 1 and send B to the situation with 8, causing him to lose.
If we start with 10, A can take 2 and send B to the situation with 8 again, causing him to lose.
By now, it should become quite obvious how you can incrementally build an O(n) solution: let win[i] = true if i stones are winnable for the first person to move
We have:
win[1] = win[2] = win[4] = win[5] = win[6] = true, win[3] = false
win[x > 6] = not (win[x - 6] and win[x - 4] and win[x - 2] and win[x - 1])
For example:
win[7] = not (win[1] and win[3] and win[5] and win[6])
= not (true and false and true and true)
= not false
= true
Compute this up until the number you're interested in and that's it. No trees involved.
O(1) solution
By looking carefully at the above solution, we can derive a simple constant time solution: note that A can only lose if he sends B to a winning position no matter what he does, so if k - 6, k - 4, k - 2, k - 1 are all winning positions.
If you compute win for a few values, the pattern becomes obvious:
win[k] = false if k = 3, 8, 11, 16, 19, 24, 27, 32...
=> win[k] = false iff k mod 8 == 3 or k mod 8 == 0
For 99, 99 mod 8 = 3, so A does not have a sure winning strategy.
OK, so we can see that:
Every turn, number of stones can be taken is less than 7, so the result should be related to modulus 7.
So, for n < 1000, I have printed out the sequence of number of stones that makes the first person win, modulus 7, and it is a truly repeated cycle.
1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5 0 1 3 4 5 6 1 2 4 5 6 0 2 3 5 6 0 1 3 4 6 0 1 2 4 5 0 1 2 3 5 6 1 2 3 4 6 0 2 3 4 5
This cycle has the length is 56, so the problem can be solved in O(1) by finding the result of first 56 numbers.

Adding zeros between every 2 elements of a matrix in matlab/octave

I am interested in how can I add rows and columns of zeros in a matrix so that it looks like this:
1 0 2 0 3
1 2 3 0 0 0 0 0
2 3 4 => 2 0 3 0 4
5 4 3 0 0 0 0 0
5 0 4 0 3
Actually I am interested in how can I do this efficiently, because walking the matrix and adding zeros takes a lot of time if you work with a big matrix.
Update:
Thank you very much.
Now I'm trying to replace the zeroes with the sum of their neighbors:
1 0 2 0 3 1 3 2 5 3
1 2 3 0 0 0 0 0 3 8 5 12... and so on
2 3 4 => 2 0 3 0 4 =>
5 4 3 0 0 0 0 0
5 0 4 0 3
as you can see i'm considering all the 8 neighbors of an element, but again using for and walking the matrix slows me down quite a bit, is there a faster way ?
Let your little matrix be called m1. Then:
m2 = zeros(5)
m2(1:2:end,1:2:end) = m1(:,:)
Obviously this is hard-wired to your example, I'll leave it to you to generalise.
Here are two ways to do part 2 of the question. The first does the shifts explicitly, and the second uses conv2. The second way should be faster.
M=[1 2 3; 2 3 4 ; 5 4 3];
% this matrix (M expanded) has zeros inserted, but also an extra row and column of zeros
Mex = kron(M,[1 0 ; 0 0 ]);
% The sum matrix is built from shifts of the original matrix
Msum = Mex + circshift(Mex,[1 0]) + ...
circshift(Mex,[-1 0]) +...
circshift(Mex,[0 -1]) + ...
circshift(Mex,[0 1]) + ...
circshift(Mex,[1 1]) + ...
circshift(Mex,[-1 1]) + ...
circshift(Mex,[1 -1]) + ...
circshift(Mex,[-1 -1]);
% trim the extra line
Msum = Msum(1:end-1,1:end-1)
% another version, a bit more fancy:
MexTrimmed = Mex(1:end-1,1:end-1);
MsumV2 = conv2(MexTrimmed,ones(3),'same')
Output:
Msum =
1 3 2 5 3
3 8 5 12 7
2 5 3 7 4
7 14 7 14 7
5 9 4 7 3
MsumV2 =
1 3 2 5 3
3 8 5 12 7
2 5 3 7 4
7 14 7 14 7
5 9 4 7 3

Resources