一道面试题,帮忙解决者拿走本账号所有可用分!(周日是期限的最后一天了,急求答案)

续写经典 2013-01-05 07:38:50
这个是本账号积分信息,解决问题者可拿走所有能用的积分!


下面是笔试题的原文信息:

我们的要求是去解析指定的url的html页面,url的number参数为1~1434.
解析开始的number由配置文件决定,解析到1434结束。
解析的example为number499,在题目中有。
解析的结果输出到一个excel文件中(全部结果)。

在面试之前我们首先进行的是笔试阶段,笔试的题目是这样的,如下:
题目:
Below is the requirement for the project. You need to have a configuration file in which you should have an URL and the starting number of the puzzle, e.g. the number is 1, means the program will extract all puzzles, if the number is 50, then it will extract puzzles starting from puzzle 50 to the end.

This is a very straight-forward screen scraping job which can be done in your language of choice: perl, python, php, java, net, ruby, whatever. Deliverables will be excel output and code.


We need to screenscrape data for the Arukone puzzles located here at the following URL:
http://www.menneske.no/arukone/5x5/eng/?number=1


Output should be one excel file includes all puzzles.

Every puzzle should be outputed like: puzzle number, difficulty level, and location of each numbered point in the order (number, row coordinate, column coordnate).
For example, the sample 5x5 puzzle located at http://www.menneske.no/arukone/5x5/eng/?number=499
would have output:


499,super easy,3,0,4
499,super easy,2,1,1
499,super easy,3,1,3
499,super easy,1,1,4
499,super easy,2,3,4
499,super easy,1,4,4


There should be 1,434(constant) puzzles for the 5x5 excel file.

If you have questions, please do not hesitate to call or write I,our senior engineer will answer all your questions or guide you through the test.
Please reply with your estimated finish date.
如果你及时的完成了这些基本的要求,请考虑下解析1434个puzzles需要多长的时间,请就使用多线程减短解析时间改进你的程序。


考题是公开的,没有任何限制,你可以寻求任何方式解答这道题目,笔试时间最长期限为3天,1月7日(星期一)做好之后先把答案邮件给我,再约定时间面试。如果你提前解答完成也可以邮件给我,到时我会回复,联系方式见以下签名。




...全文
571 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
haha277175 2013-01-16
  • 打赏
  • 举报
回复
请问你面试过了吗
wapigzhu 2013-01-06
  • 打赏
  • 举报
回复
睡了哦,要是觉得能行把帖子的100分给我就行了,不要你全部分 祝你面试能过哈
wapigzhu 2013-01-06
  • 打赏
  • 举报
回复

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PuzzleParsor {
	private static class Config{
		private String solutionPath;
		private int startIndex;
		private int maxIndex;
		
		public Config(String solution, int startIndex, int maxIndex){
			this.solutionPath = solution;
			this.startIndex = startIndex;
			this.maxIndex = maxIndex;
		}

		public String getSolutionPath() {
			return solutionPath;
		}

		public int getStartIndex() {
			return startIndex;
		}

		public int getMaxIndex() {
			return maxIndex;
		}
	}
	/*
	private static final int NORTH = 8;
	private static final int WEST = 16;
	private static final int EAST = 32;
	private static final int SOUTH = 64;
	*/
	private static final String lineSeparator = System.getProperty("line.separator");
	
	public static void main(String[] args) throws Exception {
		Config config = getConfig();
		String solutionPath = config.getSolutionPath();
		int startIndex = config.getStartIndex();
		int maxIndex = config.getMaxIndex();
		for(int i = startIndex; i <= maxIndex; i ++){
			//输出
			output(extractPuzzleSolutionFromUrl(i, solutionPath + i));
		}
	}
	
	private static void output(List<String> list) throws Exception{
		StringBuffer stringBuffer = new StringBuffer();
		for(String str : list){
			stringBuffer.append(str);
		}
		RandomAccessFile resultFile = new RandomAccessFile("result.xls", "rw");
		resultFile.getChannel().write(Charset.forName("utf-8").encode(stringBuffer.toString()), resultFile.length());
		resultFile.close();
	}
	
	private static List<String> extractPuzzleSolutionFromUrl(int index, String urlName)  throws Exception{
		System.out.println("process:" + index);
		List<String> resultList = new ArrayList<String>();
		String content = extractPuzzleTableFromUrl(urlName);
		Pattern difficultyPattern = Pattern.compile("Difficulty: (.*?)<br");
		Matcher difficultyMatcher = difficultyPattern.matcher(content);
		String difficulty = "";
		if(difficultyMatcher.find())
			difficulty = difficultyMatcher.group(1);
		Pattern pattern = Pattern.compile("<tr class=\"arukone\">(.*?)</tr>");
		Matcher matcher = pattern.matcher(content);
		List<String> rowList = new ArrayList<String>();
		while(matcher.find()){
			rowList.add(matcher.group(1));
		}
		int[][] table = parseTableInfoToCharArray(rowList);
		for(int i = 0; i < table.length; i ++){
			for(int j = 0; j < table[i].length; j ++){
				if(table[i][j] != 0){
					resultList.add(index + "," + difficulty + "," + table[i][j] + "," + i + "," + j + lineSeparator);
				}
			}
		}
		return resultList; 
	}
	
	private static int[][] parseTableInfoToCharArray(List<String> rowList){
		int[][] array = new int[rowList.size()][];
		for(int i = 0; i < rowList.size(); i ++){
			array[i] = parseRowInfoToArray(rowList.get(i));
		}
		return array;	
	}
	
	private static int[] parseRowInfoToArray(String info){
		String[] cells = info.split("</td>");
		int[] result = new int[cells.length];
		Pattern pattern = Pattern.compile("<td class=\"[^>]*\">([^<]+)");
		for(int i = 0; i < cells.length; i ++){
			Matcher matcher = pattern.matcher(cells[i]);
			if(matcher.find()){
				String value = matcher.group(1);
				result[i] = Integer.parseInt(value);
			}
		}
		return result;
	}
	
	private static String extractPuzzleTableFromUrl(String urlName) throws Exception{
		URL url = new URL(urlName);
		URLConnection urlConnection = url.openConnection();
		InputStream inputStream = urlConnection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuffer stringBuffer = new StringBuffer();
		String str = null;
		while((str = reader.readLine()) != null){
			stringBuffer.append(str);
		}
		String content = stringBuffer.toString();
		int startIndex = content.indexOf("<!-- main -->");
		int endIndex = content.indexOf("<!-- footer -->");
		return content.substring(startIndex, endIndex);
	}
	
	private static Config getConfig() throws Exception{
		RandomAccessFile file = new RandomAccessFile("config.txt", "r");
		ByteBuffer buffer = file.getChannel().map(MapMode.READ_ONLY, 0, file.length());
		CharBuffer charBuffer = Charset.forName("utf-8").decode(buffer);
		String content = charBuffer.toString();
		String solution = extractInfo(content, lineSeparator, "path");
		int maxIndex = Integer.parseInt(extractInfo(content, lineSeparator, "maxIndex"));
		int startIndex = Integer.parseInt(extractInfo(content, lineSeparator, "startIndex"));
		file.close();
		return new Config(solution, startIndex, maxIndex);
	}
	
	private static String extractInfo(String content, String lineSeparator, String name){
		Pattern pathPattern = Pattern.compile(name + "=(.*?)" + lineSeparator + "|$");
		Matcher pathMatcher = pathPattern.matcher(content);
		if(pathMatcher.find())
			return pathMatcher.group(1);
		return "";
	}
	
}


配置文件config.txt
path=http://www.menneske.no/arukone/5x5/eng/showpuzzle.html?number=
startIndex=1400
maxIndex=1413

没用多线程哦,你可以自己改,改的话要把output同步,另外我没写到excel,随便写了个txt文件,用excel也能打开,如果非要用excel你去网上找找有例子,还要下jar包什么的
续写经典 2013-01-06
  • 打赏
  • 举报
回复
引用 19 楼 wapigzhu 的回复:
引用 18 楼 a125138 的回复:引用 17 楼 wapigzhu 的回复:睡了哦,要是觉得能行把帖子的100分给我就行了,不要你全部分 祝你面试能过哈 首先谢谢您的帮助,希望您能再指点一下,我把源码拷到项目里报异常,你帮忙看下哪里出问题了! 哦,这个我大意了, 你可以在config.txt的最后一行后面加一个回车,就行了 或者改下代码 147行:P……
非常感谢您的帮助!
wapigzhu 2013-01-06
  • 打赏
  • 举报
回复
引用 18 楼 a125138 的回复:
引用 17 楼 wapigzhu 的回复:睡了哦,要是觉得能行把帖子的100分给我就行了,不要你全部分 祝你面试能过哈 首先谢谢您的帮助,希望您能再指点一下,我把源码拷到项目里报异常,你帮忙看下哪里出问题了!
哦,这个我大意了, 你可以在config.txt的最后一行后面加一个回车,就行了 或者改下代码 147行:Pattern pathPattern = Pattern.compile(name + "=(.*)(?:" + lineSeparator + "|$)");改成这样
续写经典 2013-01-06
  • 打赏
  • 举报
回复
引用 17 楼 wapigzhu 的回复:
睡了哦,要是觉得能行把帖子的100分给我就行了,不要你全部分
祝你面试能过哈


首先谢谢您的帮助,希望您能再指点一下,我把源码拷到项目里报异常,你帮忙看下哪里出问题了!

wapigzhu 2013-01-05
  • 打赏
  • 举报
回复
这道题好像没有要求把答案算出来?X,我还想复杂了,解析答案去了
续写经典 2013-01-05
  • 打赏
  • 举报
回复
引用 12 楼 wapigzhu 的回复:
发现个小技巧,他答案页面有答案,直接解析出来不算作弊吧
拜托了!希望能尽快帮我解决!先谢谢了 他的要求是: 去解析指定的url的html页面,url的number参数为1~1434. 解析开始的number由配置文件决定,解析到1434结束。 解析的example为number499,在题目中有。 解析的结果输出到一个excel文件中(全部结果)。
续写经典 2013-01-05
  • 打赏
  • 举报
回复
引用 12 楼 wapigzhu 的回复:
发现个小技巧,他答案页面有答案,直接解析出来不算作弊吧
当然可以,你看清他的要求是从配置文件中读取的参数开始,一直到1413结束,然后输出到excel
wapigzhu 2013-01-05
  • 打赏
  • 举报
回复
发现个小技巧,他答案页面有答案,直接解析出来不算作弊吧
续写经典 2013-01-05
  • 打赏
  • 举报
回复
引用 9 楼 wapigzhu 的回复:
有点麻烦,不过不是太难,解析html,取出来放到数组里面就行了,不过后面的puzzle我还没想好,一个点寻路还行,它是n个点在里面交叉寻路
对啊!好像还是需要规律的
wapigzhu 2013-01-05
  • 打赏
  • 举报
回复
也可以考虑下,先从1开始寻,如果后面的数能顺利完成,那就完成了,如果不能,就从不能的这个点开始寻?
wapigzhu 2013-01-05
  • 打赏
  • 举报
回复
有点麻烦,不过不是太难,解析html,取出来放到数组里面就行了,不过后面的puzzle我还没想好,一个点寻路还行,它是n个点在里面交叉寻路
续写经典 2013-01-05
  • 打赏
  • 举报
回复
主要是从来都没接触过解析html源码的东西,而且他指定的url还是1413个智力游戏,还要输出到excel中
续写经典 2013-01-05
  • 打赏
  • 举报
回复
引用 6 楼 tangwei070 的回复:
如果都不知道什么意思, 赶紧打个电话过去问问。 面试题后面都说了,有问题,可以去咨询。
最开始写的就是我问的结果: 我们的要求是去解析指定的url的html页面,url的number参数为1~1434. 解析开始的number由配置文件决定,解析到1434结束。 解析的example为number499,在题目中有。 解析的结果输出到一个excel文件中(全部结果)。
tangwei070 2013-01-05
  • 打赏
  • 举报
回复
如果都不知道什么意思, 赶紧打个电话过去问问。 面试题后面都说了,有问题,可以去咨询。
续写经典 2013-01-05
  • 打赏
  • 举报
回复
引用 3 楼 tangwei070 的回复:
499,super easy,3,0,4 499,super easy,2,1,1 499,super easy,3,1,3 499,super easy,1,1,4 499,super easy,2,3,4 499,super easy,1,4,4 这是从哪里来的 ?
这个大概就是结果吧
续写经典 2013-01-05
  • 打赏
  • 举报
回复
引用 2 楼 tangwei070 的回复:
解析啥? http://www.menneske.no/arukone/5x5/eng/?number=1 这个网站我打不开。 根据思路, 可以用httpclient, 获取返回页面的元素自己去分析。
关键就是没思路!不知道怎么入手
tangwei070 2013-01-05
  • 打赏
  • 举报
回复
499,super easy,3,0,4 499,super easy,2,1,1 499,super easy,3,1,3 499,super easy,1,1,4 499,super easy,2,3,4 499,super easy,1,4,4 这是从哪里来的 ?
tangwei070 2013-01-05
  • 打赏
  • 举报
回复
解析啥? http://www.menneske.no/arukone/5x5/eng/?number=1 这个网站我打不开。 根据思路, 可以用httpclient, 获取返回页面的元素自己去分析。
加载更多回复(1)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧