How to print 10 number 10 times,9 number 9 times etc..Using Python - python-2.7

Output should be like this
10 10 10 10 10 10 10 10 10 10
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
.
.
.
1

tyr it dear
l = [1,2,3,4,5,6,7,8,9,10]
d = []
for i in l:
d.extend([i]*i)
print d[::-1]

Related

Loop a matrix and a vector

I have a vector with 2 elements a_destinations that represent the source and destination with two integers. 2 4 or 5 1...
I have a matrix test as follows:
0 15 15 15 13 13 15 15 13 13 15 15 15 13 15 15
2 1 2 2 2 2 2 2 2 2 10 2 2 2 10 2
6 1 2 3 3 3 6 6 6 6 6 6 6 6 6 6
7 2 2 3 4 4 2 7 7 7 7 7 7 7 7 7
8 3 3 3 4 5 3 3 8 8 3 3 3 8 3 3
9 4 4 4 4 5 4 4 4 9 4 4 4 9 4 4
7 2 2 2 2 2 6 7 7 7 11 11 7 7 11 7
12 6 6 3 3 3 6 7 8 8 12 12 12 12 12 12
9 7 7 7 4 4 7 7 8 9 7 7 7 9 7 7
13 8 8 8 8 5 8 8 8 9 13 13 13 13 13 13
14 1 11 11 11 11 11 11 11 11 10 11 11 11 14 14
12 6 6 12 12 12 6 12 12 12 10 11 12 12 10 12
15 7 7 7 7 7 7 7 7 13 11 11 12 13 11 15
0 12 12 12 9 9 12 12 9 9 12 12 12 13 12 12
15 10 10 10 10 10 10 10 10 10 10 10 10 10 14 15
0 12 12 12 12 12 12 12 12 12 14 12 12 12 14 15
where test[i][j] represents the path from i to j
each time test[i][j] != j we do the loop again
Example:
path from 2 to 4 >> test[2][4] = 3, test[3][4] = 4: we output: 2, 3, 4
path from 1 to 7 >> test[1][7] = 2, test[2][7] = 6 , test[6][7] = 7: we output 1, 2 ,6, 7
I tried as follows:
std::vector<vector<int>> test;
test = Graphe->P; // MATRIX IS FILLED like on top
vector< int > a_destinations; // Vector with the destinations: 2 4 or 5 10 or 1 4 ...
for ( unsigned i = 0; i < test.size(); i++){
for (unsigned j = 0; j< test.size(); j++){
for (unsigned k = 0; k < a_destinations->size() - 1 ; k ++){
if ( a_destinations->at(k) == i && a_destinations->at(k+1) == j ){
if (test[i][j] == a_destinations->at(k+1)){
cout << a_destinations->at(k) << ", " <<test[i][j];
} else {
cout << a_destinations->at(k) << ", " << test[a_destinations->at(k)][j];
}
cout << ", " << test[i][j];// << ", " << a_destinations->at(k+1);
}
}
}
}
But i end up with 2 destinations always.

How do I add a Header for Rows in a 2D Array?

I Need to output an 2D Array that has a label header for the colums and the rows.
the columns is easy i just ouput a string above the table but i cannot figure out how to add the word ROW in vertical letters at the begining of the table.
it has to look like this.
C o l u m n s
| 1 2 3 4 5 6
----------------------------------
1 | 2 3 4 5 6 7
R 2 | 3 4 5 6 7 8
O 3 | 4 5 6 7 8 9
W 4 | 5 6 7 8 9 10
S 5 | 6 7 8 9 10 11
6 | 7 8 9 10 11 12
i cannot figure out how to get the rows label

openCV: Plotting a colormap from a matrix

I have a 50 x 50 matrix having certain values from 1 to 50. I need to plot a color map for the same using OpenCV .
I have already done this in MATLAB (with help from this answer):
>> A = randi([10,60],100,100);
>> colormap('hot')
>> imagesc(A)
>> colorbar
And the output comes like .
I would like to do the same using openCV.
How do I proceed for this ?
I am unable to understand how do I implement the function of "hot" which appears like this (see only hot)
:
Additionally I would also be interested in putting a color bar as shown in the first image.
Update (Final code I am running)
Unfortunately still I am unable to plot the correct color map: Here is the MATLAB code:
Note : A is a 10 by 100 matrix.
A=[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 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 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
]
colormap('hot');
imagesc(A)
colorbar
And here is the output:
Now I created a text file with same data, and my text file looks like this:
And used the following code to achieve the same result as of MATLAB:
int main()
{
ifstream Read("myfile.txt");
vector<int> nums;
while ( !Read.eof() ) {
int n;
Read >> n;
nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat(nums);
cout<<mat<<endl; //for testing
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_JET );
// Show the result:
imshow("colormap", image);
waitKey(0);
return 0;
}
But the output from the above is meaningless.
I need to match the results from MATLAB and Opencv.
you will have to reshape the 1d vector, to a 2d mat :
//
// before doing anything else, CLEAN UP YOUR DAMN TXTFILE.
// it should contain nothing than numbers and spaces.
ifstream Read("m.txt");
// be extra picky about the type here.
// we are reading in a 8bit grayscale map.
vector<uchar> nums;
for (int i=0; (i<100*10)&&(!Read.eof()); i++ ) {
int n;
Read >> n;
nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
// your data is in the [0..9] range, so scale up to [0..255] uchar range
mat2d *= (255/10);
cout<<mat2d<<endl; //for testing
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_HOT );
// Show the result:
imshow("colormap", image);
waitKey(0);

Getting WA for SPOJ ANDROUND (5832)

I am trying out this problem: http://www.spoj.com/problems/ANDROUND
My algo makes of 2D array of 32 rows and N columns (32 because after 31 rounds, the array doesn't change). It then calculates each field in the 2D array by taking the AND of the value in the previous row (column remaining same), and its neighbours. I have checked with almost all sorts of input and have got the desired output. But still getting WA. Here is my code. If anybody could point out the error or give a test case for which my program fails.
#include <cstdio>
#include <cstring>
using namespace std;
static long A[33][20002], N, T, K;
int main()
{
scanf("%ld", &T);
while(T--){
memset(A, 0, sizeof A);
scanf("%ld %ld", &N, &K); K=(K>31)? 31:K; \\Setting K=31 if K>31
for(int i=1; i<=N; ++i) scanf("%ld", &A[0][i]);
A[0][0]=A[0][N]; A[0][N+1]=A[0][1]; \\first row is the input array
for(int i=1; i<=K; ++i){
for(int j=1; j<=N; ++j)
A[i][j]= (A[i-1][j]&A[i-1][j-1]) & A[i-1][j+1]; \\taking AND of previous element in column and its neighbors.
A[i][0]=A[i][N]; A[i][N+1]=A[i][1]; \\for making array cyclic.
}
for (int i=1; i<=N; ++i) printf("%ld ", A[K][i]); \\Printing the array after required rounds.
printf("\n");
}
return 0;
}
Thank you.
because after 31 rounds, the array doesn't change
What makes you believe that?
After R rounds, the element at index i is only influenced by the elements
A[i-R] ... A[i+R]
(with suitable wrapping if i <= R or N < i+R).
So it is only guaranteed that the array remains unchanged after N/2 rounds.
Try this test case.
1
500 1000000000
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 6

How to program all the possible for answer [][]/[] = [][]/[] = [][]/[] with no repeat number from 1 to 9? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is a program language was written by C++, fill all the number from 1 to 9 without repeat in the equation [][]/[] = [][]/[] = [][]/[]. I did some tests but seems the answers are not completely, e.g. 57/6 = 19/2 = 38/4, can anyone help? thanks.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
/* [][]/[]=[][]/[]=[][]/[] */
bool equal(vector<double> a)
{
double exc = 0.001;
if (fabs((a[0]*10+a[1])/a[2] - (a[3]*10+a[4])/a[5]) > exc)
return false;
if (fabs((a[0]*10+a[1])/a[2] - (a[6]*10+a[7])/a[8]) > exc)
return false;
return true;
}
int main()
{
vector<double> a;
double data=1.0;
for (int i=1; i<=9; ++i)
{
data = i*1.0;
a.push_back(data);
}
while (next_permutation(a.begin(), a.end()))
{
if (equal(a))
{
for (vector<double>::iterator it=a.begin(); it!=a.end(); ++it)
cout << *it << " ";
cout << endl;
}
}
return 0;
}
output:
1 9 2 3 8 4 5 7 6
1 9 2 5 7 6 3 8 4
2 1 3 4 9 7 5 6 8
2 1 3 5 6 8 4 9 7
2 7 3 5 4 6 8 1 9
2 7 3 8 1 9 5 4 6
3 8 4 1 9 2 5 7 6
3 8 4 5 7 6 1 9 2
4 9 7 2 1 3 5 6 8
4 9 7 5 6 8 2 1 3
5 4 6 2 7 3 8 1 9
5 4 6 8 1 9 2 7 3
5 6 8 2 1 3 4 9 7
5 6 8 4 9 7 2 1 3
5 7 6 1 9 2 3 8 4
5 7 6 3 8 4 1 9 2
8 1 9 2 7 3 5 4 6
8 1 9 5 4 6 2 7 3
Maybe there are severl answers in different sequence, you can filter by necessary.