用Java解PythonChallenge(第二天)

3.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChallengeThree {
	// http://www.pythonchallenge.com/pc/def/equality.html
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader("c:\\4.txt"));
			Pattern pattern = Pattern
					.compile("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+");
			Matcher matcher;
			String temp = null;

			while ((temp = br.readLine()) != null) {
				matcher = pattern.matcher(temp);
				while (matcher.find()) {
					System.out.print(matcher.group(1));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


注:与上一题类似,网页的源码中有用注释写的一段乱码,让我们找出两侧被3个(且只为3个)大写字母包围的小写字母

4.
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class ChallengeFour {
	// http://www.pythonchallenge.com/pc/def/linkedlist.html
	public static void main(String[] args) {
		ChallengeFour cf = new ChallengeFour();
		try {
			String currentNum = "12345";
			while (currentNum.matches("[0-9]{1,}")) {
				System.out.println(currentNum);
				if (currentNum.equals("92118")) {
					currentNum = cf.getNextNum(String.valueOf(Integer
							.parseInt(currentNum) / 2));
				} else {
					currentNum = cf.getNextNum(currentNum);
				}
			}
			System.out.println("The last result is " + currentNum);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getNextNum(String currentNum) throws IOException {
		URL url = new URL(
				"http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
						+ currentNum);
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new BufferedInputStream(url.openStream())));
		String temp = null;
		String[] strings = null;
		while ((temp = br.readLine()) != null) {
			strings = temp.split(" ");
		}
		br.close();
		return strings[strings.length - 1];

	}
}

注:点击图片,调用linkedlist.php?nothing=12345得到了and the next nothing is 92512 从中得到规律,根据每次获得的nothing值来进行下次url访问。


4.
这道题需要用到python专有的一个核心模块,java无解,直接google答案,进入下一题

你可能感兴趣的:(java,.net,PHP,python,Google)