How to get Current Row of a CSV File - python-2.7

Trying to skip i rows of a data file (then process j rows). Plenty of answers on length of csv file. No success with count or .line_num.
How is current line number accessed in csv.reader?
import csv
def csv_reader(df):
i = 90
with open(df, 'r') as csvfile:
for line in range(0, i):
next(csvfile)
for line in csv.reader(csvfile, delimiter=' ', skipinitialspace = True):
print(csv.reader.line_num) # Invalid line
Sample data:
Last Quarter
Visit Astronomy
Daily Weather History & Observations
2018 Temp. (°C) Dew Point (°C) Humidity (%) Sea Level Press. (hPa) Visibility (km) Wind (km/h) Precip. (mm) Events
Mar high avg low high avg low high avg low high avg low high avg low high avg high sum
1 27 19 12 13 9 4 82 49 21 1016 1012 1007 10 10 10 24 11 - 0.00
2 25 20 14 14 12 9 82 61 32 1017 1014 1010 10 10 10 21 10 - 0.00
3 31 22 14 15 13 7 94 59 13 1014 1011 1007 10 10 10 27 8 - 0.00
4 30 21 13 15 13 6 82 59 13 1016 1012 1009 10 10 10 34 11 - 0.00
5 24 19 15 16 13 11 82 71 46 1022 1016 1013 10 10 10 35 13 - 0.00 Rain
6 20 14 9 12 9 6 82 60 31 1028 1024 1021 10 10 10 32 19 47 0.00
7 23 16 9 13 10 7 100 71 29 1029 1027 1024 10 10 5 29 11 37 0.00 Rain
Other random data follows

You can access line number using enumerate.
for index, line in enumerate(csv.reader(csvfile, delimiter=' ', skipinitialspace = True)):
print('Index %s' % str(index + 1)) # enumerate starts with 0

Related

VLOOKUL OR OFFSET

I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

cost of cutting a plank

I know that this question has been asked but i cant understand the problem in my code. I know that we have to use cost in desending order for minimum cost and i did same but still gives wrong output.
A board composed of m×n wooden squares and asks him to find the minimum cost of breaking the board back down into individual 1×1 pieces. To break the board down, Bob must make cuts along its horizontal and vertical lines.
To reduce the board to squares, xn−1 vertical cuts must be made at locations x1,x2,…,xn−2,xn−1 and ym−1 horizontal cuts must be made at locations y1,y2,…,ym−2,ym−1. Each cut along some xi (or yj) has a cost, cxi (or cyj). If a cut of cost c passes through n already-cut segments, the total cost of the cut is n×c.
The cost of cutting the whole board down into 1×1 squares is the sum of the cost of each successive cut. Recall that the cost of a cut is multiplied by the number of already-cut segments it crosses through, so each cut is increasingly expensive.
Input Format
The first line contains a single integer, T, denoting the number of test cases. The subsequent 3T lines describe each test case in 3 lines.
For each test case, the first line has two positive space-separated integers, m and n, detailing the respective height (y) and width (x) of the board.
The second line has m−1 space-separated integers listing the cost, cyj, of cutting a segment of the board at each respective location from y1,y2,…,ym−2,ym−1.
The third line has n−1 space-separated integers listing the cost, cxi, of cutting a segment of the board at each respective location from x1,x2,…,xn−2,xn−1.
Note: If we were to superimpose the m×n board on a 2D graph, x0, xn, y0, and yn would all be edges of the board and thus not valid cut lines.
Constraints
1≤T≤20
2≤m,n≤1000000
,0≤cxi,cyj≤1000000000
Output Format
For each of the T test cases, find the minimum cost (MinimumCost) of cutting the board into 1×1 squares and print the value of MinimumCost % (1000000000+7).
#include <iostream>
#include <limits>
using namespace std;
int main() {
int t,ch=0;
long int pos,m,n,h=1,l=1;
long long int cost=0,*x,*y,temp;
cin>>t;
while(t>0)
{cin>>m>>n;
cost=0;
x = new long long int[n-1];
y = new long long int[m-1];
for (long i=0;i<m-1;i++)
cin>>y[i];
for(long i=0;i<n-1;i++)
cin>>x[i];
h=1;
l=1;
while((h!=m)|(l!=n))
{ch=0;
temp=0;
for (long i=0;i<m-1;i++)
if (temp<y[i])
{temp=y[i];
pos=i;
}
for(long i=0;i<n-1;i++)
if (temp<x[i])
{temp=x[i];
pos=i;
ch=1;
}
cost=cost+temp*(ch==0?l:h);
if (ch==0)
{y[pos]=-1;
h++;}
else
{x[pos]=-1;
l++;
}
}
cout<<cost%1000000007;
t--;
}
return 0;
}
Test case that gives wrong output:
Input:
5
52 30
2 30 79 47 4 56 47 67 25 30 75 58 47 54 66 61 6 64 28 41 75 36 1 92 42 61 35 56 12 86 84 14 68 63 13 72 19 60 39 96 43 14 55 42 21 73 3 27 37 84 68
64 72 21 56 14 35 44 71 47 82 7 14 50 71 79 23 42 92 14 39 35 81 46 29 2 19 84 81 57
23 43
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
60 76
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
58 40
71 58 61 51 33 3 43 48 94 30 29 40 59 83 12 43 64 69 64 65 42 57 40 72 64 98 98 47 56 6 85 79 65 46 30 98 49 25 98 96 7 27 88 66 10 0 62 26 69 78 92 64 87 84 88 51 35
87 50 91 45 35 22 62 81 53 61 83 30 59 31 38 39 19 56 1 20 70 28 41 48 72 57 35 56 46 39 91 85 41 34 30 77 57 93 10
47 94
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
Output
51028
1912
33638
91124
27525
This line could be a contributing factor:
while((h!=m)|(l!=n))
The | operator is a binary arithmetic operator.
Maybe you were looking for ||, which is the logical OR operator:
while((h!=m) || (l!=n))

SQL Update Subsequent Column OFFSET FETCH NEXT

I like to know is there a way to doing auto looping / counter batch, updating SQL column like using OFFSET / FETCH NEXT method
QUESTION : Below table have 20 rows, I like to update DealerId column the First 4 rows as 1,2,3,4 and the next subsequent 4 rows repeating as 1,2,3,4 values
Something like below
NEED TO MODIFY TABLE
Id DealerId
1 1 1
2 2 2
3 3 3
4 4 4
5 5 1
6 6 2
7 7 3
8 8 4
9 9 1
10 10 2
11 11 3
12 12 4
13 13 1
14 14 2
15 15 3
16 16 4
17 17 1
18 18 2
19 19 3
20 20 4
ORIGINAL TABLE
Id DealerId StoreId TerminalId MessageNo CreatedDate
1 1 86 5027 029500021201403031434350039 2014-03-03 14:34:37.347
2 2 86 5027 029500021201403031434350039 2014-03-05 10:31:59.903
3 3 86 5027 029500021201403031434350039 2014-03-05 10:33:41.293
4 4 86 5027 029500021201403031434350039 2014-03-05 10:46:50.057
5 5 86 5027 029500021201403031434350039 2014-03-05 10:50:23.910
6 6 33 5338 004000003201403051508010255 2014-03-05 15:08:03.247
7 7 26 5595 704201181201403061024330013 2014-03-06 10:24:34.590
8 8 26 5595 704201181201403061026180022 2014-03-06 10:26:19.517
9 9 33 5338 004000003201403061043150312 2014-03-06 10:43:16.013
10 10 86 5027 029500021201403031434350039 2014-03-06 14:27:51.717
11 11 86 5027 029500021201403031434350039 2014-03-06 14:38:40.593
12 12 86 5027 029500021201403031434350039 2014-03-06 14:44:25.947
13 13 521 4905 051100003002447 2014-03-07 12:51:07.487
14 14 521 4905 051100003002447 2014-03-07 12:55:07.300
15 15 521 4905 051100003002447 2014-03-07 12:56:24.793
16 16 521 4905 051100003002447 2014-03-07 12:57:43.123
17 17 521 4905 051100003002447 2014-03-07 14:15:11.093
18 18 632 5120 088800003201403071441280026 2014-03-07 14:41:29.733
19 19 632 5120 088800003201403071456500050 2014-03-07 14:56:51.727
20 20 632 5120 088800003201403071459240064 2014-03-07 14:59:24.953
Assuming that all id's are consequently, starting from 1:
In MySQL:
update OriginalTable
set DealreId = mod(id-1, 4)+1
and in Microsoft SQL Server:
update OriginalTable
set DealreId = ((id-1)%4)+1
And if the id's are not consequently (or are not starting from 1) you can use cursor to update it one by one:
DECLARE c1 CURSOR FOR
SELECT id, dealerId
FROM OriginalTable
ORDER BY id, dealerId
OPEN c1
declare #id int
declare #dealerId int
declare #i int
set #i = 1
FETCH NEXT FROM c1
INTO #id, #dealerId
while ##FETCH_STATUS = 0
BEGIN
update OriginalTable
set dealerId = #i
where current of c1
if (#i < 4)
set #i = #i + 1
else
set #i = 1
FETCH NEXT FROM c1
INTO #id, #dealerId
END

Using the jpeg quantization matrix of one image to compress another image

I have two images, A and B, and I need to estimate B's quantization table and compress A using this table. What is the best way to do this, using libjpeg or, even better, opencv?
I've used libjpeg's utility 'djpeg' to find the quantization table of a image, but I'm not sure how to interpret its output and use it with libjpeg. Besides, I need to find this matrix and do the compression from inside my program, which renders (I think) 'djpeg' unusable in this case.
Following, is the output of 'djpeg' for a test image, running it with:
djpeg -v -v cat1.jpg > /dev/null
Start of Image
JFIF APP0 marker: version 1.01, density 96x96 1
Define Quantization Table 0 precision 0
5 3 3 5 7 12 15 18
4 4 4 6 8 17 18 17
4 4 5 7 12 17 21 17
4 5 7 9 15 26 24 19
5 7 11 17 20 33 31 23
7 11 17 19 24 31 34 28
15 19 23 26 31 36 36 30
22 28 29 29 34 30 31 30
Define Quantization Table 1 precision 0
5 5 7 14 30 30 30 30
5 6 8 20 30 30 30 30
7 8 17 30 30 30 30 30
14 20 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
Start Of Frame 0xc0: width=450, height=320, components=3
Component 1: 2hx2v q=0
Component 2: 1hx1v q=1
Component 3: 1hx1v q=1
Define Huffman Table 0x00
0 1 5 1 1 1 1 1
1 0 0 0 0 0 0 0
Define Huffman Table 0x10
0 2 1 3 3 2 4 3
5 5 4 4 0 0 1 125
Define Huffman Table 0x01
0 3 1 1 1 1 1 1
1 1 1 0 0 0 0 0
Define Huffman Table 0x11
0 2 1 2 4 4 3 4
7 5 4 4 0 1 2 119
Start Of Scan: 3 components
Component 1: dc=0 ac=0
Component 2: dc=1 ac=1
Component 3: dc=1 ac=1
Ss=0, Se=63, Ah=0, Al=0
End Of Image
Thanks in advance!

functioning of bitwise and

This question was asked in an interview, can someone tell what does the following code do? It gives output 15 for 150, 3 for 160, 15 for 15. What mathematical operation is it performing on 'n'.
int foo(int n)
{
int t,count=0;
t=n;
while(n)
{
count=count+1;
n=(n-1)&t;
}
return count;
}
It seems to calculate the number max(n**2-1, 0), where n is the number of 1 bits in a number's binary representation:
0 0 0b0
1 1 0b1
2 1 0b10
3 3 0b11
4 1 0b100
5 3 0b101
6 3 0b110
7 7 0b111
8 1 0b1000
9 3 0b1001
10 3 0b1010
11 7 0b1011
12 3 0b1100
13 7 0b1101
14 7 0b1110
15 15 0b1111
16 1 0b10000
17 3 0b10001
18 3 0b10010
19 7 0b10011
20 3 0b10100
21 7 0b10101
22 7 0b10110
23 15 0b10111
24 3 0b11000
25 7 0b11001
26 7 0b11010
27 15 0b11011
28 7 0b11100
29 15 0b11101
30 15 0b11110
31 31 0b11111
32 1 0b100000
33 3 0b100001
34 3 0b100010
35 7 0b100011
36 3 0b100100
37 7 0b100101
38 7 0b100110
39 15 0b100111
40 3 0b101000
41 7 0b101001
42 7 0b101010
43 15 0b101011
44 7 0b101100
45 15 0b101101
46 15 0b101110
47 31 0b101111
48 3 0b110000
49 7 0b110001
50 7 0b110010
51 15 0b110011
52 7 0b110100
53 15 0b110101
54 15 0b110110
55 31 0b110111
56 7 0b111000
57 15 0b111001
58 15 0b111010
59 31 0b111011
60 15 0b111100
61 31 0b111101
62 31 0b111110
63 63 0b111111
64 1 0b1000000
65 3 0b1000001
66 3 0b1000010
67 7 0b1000011
68 3 0b1000100
69 7 0b1000101
70 7 0b1000110
71 15 0b1000111
72 3 0b1001000
73 7 0b1001001
74 7 0b1001010
75 15 0b1001011
76 7 0b1001100
77 15 0b1001101
78 15 0b1001110
79 31 0b1001111
80 3 0b1010000
81 7 0b1010001
82 7 0b1010010
83 15 0b1010011
84 7 0b1010100
85 15 0b1010101
86 15 0b1010110
87 31 0b1010111
88 7 0b1011000
89 15 0b1011001
90 15 0b1011010
91 31 0b1011011
92 15 0b1011100
93 31 0b1011101
94 31 0b1011110
95 63 0b1011111
96 3 0b1100000
97 7 0b1100001
98 7 0b1100010
99 15 0b1100011
It is easier to find out the "mathematical operation", when function is changed to recursive:
int foo(int n, int t)
{
if( n )
return foo( (n-1) & t ) + 1
else
return 0;
}
So formula is:
F(0,t) = 0
F(n,t) = F( (n-1) & t, t ) + 1
foo(n) = F(n,n)
I don't have any idea, is that wellknown formula for counting something, or not.
You may find answer from math.stackexchange.com
That is a method known as Brian Kernighan's way to count set bits :
unsigned int v; // count the number of bits set in v
unsigned int c; // c accumulates the total bits set in v
for (c = 0; v; c++)
{
v &= v - 1; // clear the least significant bit set
}
Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.
Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"