用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();
}
}
}


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

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];

}
}

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

4.
[color=blue]这道题需要用到python专有的一个核心模块,java无解,直接google答案,进入下一题[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值