2008年7月4日星期五

google treasure hunt ----zip

问题:

统计下面zip文件夹中满足下面条件的数据,并把它们的乘积返回。
所有文件名或文件路径中包含foo,且以.txt结尾的文件中的第4行的总数
所有文件名或文件路径中包含EFG ,且以.js结尾的文件中的第5行的总数
提示:如果相应的行不存在,不统计



Question

Here is a random zip archive for you to download:
GoogleTreasureHunt08_18057390569245304937.zip

Unzip the archive, then process the resulting files to obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text.

Sum of line 4 for all files with path or name containing foo and ending in .txt
Sum of line 5 for all files with path or name containing EFG and ending in .js
Hint: If the requested line does not exist, do not increment the sum.

Multiply all the above sums together and enter the product below.
(Note: Answer must be an exact, decimal representation of the number.)


程序:



static Int64 sum(string folder, string ext, string foundstring, Int64 linenum)
{
Int64 result=0;
string[] files = Directory.GetFiles(folder, ext, SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.ToLower().Contains(foundstring))
using (StreamReader reader = new StreamReader(file))
{
Int64 count = 0;
string line;
while ((line=reader.ReadLine())!=null)
{
count++;
if (count == linenum)
{
Int64 temp = Convert.ToInt64(line);
result+=temp;
break;
}
}
}

}
return result;


}

没有评论: