Given a screen with all pixels having one of two colors. Now I will click on a random pixel.
Then that pixel & all the adjacent pixels with same color should change the color to the second color.
adjacent = vertically or horizontally above or blow.
package com.zhuyu_deng.test; public class Test { private static void printMatrix(char[][] a) { for (int i = 0; i < a.length; ++i) { for (int j = 0; j < a[i].length; ++j) { System.out.print(a[i][j] + " "); } System.out.println(); } } // 关于FloodFill算法的解释 // 以下就是源程序 事实上就是广度搜索 // 但是必须要建立一个所谓的方向数组 private static void floodFill(char[][] a, int x, int y) { class Node { public Node(int x, int y) { this.x = x; this.y = y; } int x; int y; } int px[] = new int[] {0, 0, -1, 1}; int py[] = new int[] {-1, 1, 0, 0}; char orgColor = a[x][y]; char revColor = a[x][y] == 'W' ? 'B' : 'W'; int size = 0; Node stack[] = new Node[a.length * a[0].length]; stack[size] = new Node(x, y); while (size >= 0) { Node cur = stack[size]; size--; a[cur.x][cur.y] = revColor; for (int i = 0; i < 4; ++i) { int xx = cur.x + px[i]; int yy = cur.y + py[i]; if (xx >= 0 && xx < a.length && yy >= 0 && yy < a[0].length) { if (a[xx][yy] == orgColor) stack[++size] = new Node(xx, yy); } } } } public static void main(String args[]) { char a[][] = new char[][] { { 'W', 'B', 'W', 'W', 'B', 'W' }, { 'B', 'B', 'W', 'W', 'B', 'W' }, { 'W', 'B', 'B', 'B', 'W', 'B' }, { 'W', 'B', 'W', 'W', 'B', 'B' } }; printMatrix(a); floodFill(a, 2, 2); System.out.println(); printMatrix(a); } }
Edit: Question seem to be not clear to some ppl. Giving an ex:
Given below & clicked on 2nd row, 2nd col
W B W W B W
B B W W B W
W B B B W B
W B W W B B
Covert to
W W W W B W
W W W W B W
W W W W W B
W W W W B B
package com.zhuyu_deng.test; import java.util.Stack; public class Test { private static void printMatrix(char[][] a) { for (int i = 0; i < a.length; ++i) { for (int j = 0; j < a[i].length; ++j) { System.out.print(a[i][j] + " "); } System.out.println(); } } private static void floodFill(char[][] a, int x, int y) { class Node { public Node(int x, int y) { this.x = x; this.y = y; } int x; int y; } char orgColor = a[x][y]; char revColor = a[x][y] == 'W' ? 'B' : 'W'; int size = 0; Node stack[] = new Node[a.length * a[0].length]; stack[size] = new Node(x, y); while (size >= 0) { Node cur = stack[size]; size--; a[cur.x][cur.y] = revColor; if (cur.x - 1 >= 0 && orgColor == a[cur.x - 1][cur.y]) { stack[++size] = (new Node(cur.x-1, cur.y)); } if (cur.x + 1 < a.length && orgColor == a[cur.x + 1][cur.y]) { stack[++size] = (new Node(cur.x+1, cur.y)); } if (cur.y - 1 >= 0 && orgColor == a[cur.x][cur.y - 1]) { stack[++size] = (new Node(cur.x, cur.y-1)); } if (cur.y + 1 < a[0].length && orgColor == a[cur.x][cur.y + 1]) { stack[++size] = (new Node(cur.x, cur.y + 1)); } } } public static void main(String args[]) { // int[] a = {-2,11,-4,13,-5,-2}; int[][] b = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 }, { -1, 8, 0, -2 } }; int[][] matrix = { { 2, 3, 4, 1 }, { 1, 1, 3, 9 }, { 2, 2, 3, 1 }, { 2, 2, 3, 1 } }; char a[][] = new char[][] { { 'W', 'B', 'W', 'W', 'B', 'W' }, { 'B', 'B', 'W', 'W', 'B', 'W' }, { 'W', 'B', 'B', 'B', 'W', 'B' }, { 'W', 'B', 'W', 'W', 'B', 'B' } }; printMatrix(a); floodFill(a, 2, 2); System.out.println(); printMatrix(a); } }
如文件word.txt内容如下:
what is you name?
my name is zhang san。
要求统计word.txt中出现“is”的次数?
代码如下:
PerWordMapper
package com.hadoop.wordcount;
import java.io.IOException; import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper;
public class PerWordMapper extends Mapper<Object, Text, Text, IntWritable> {
public Text keyText = new Text(); public IntWritable intValue = new IntWritable(1); @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { String str = value.toString(); StringTokenizer to = new StringTokenizer(str); while (to.hasMoreTokens()) { String t = to.nextToken(); //此处为判断统计字符串的地方 if(t.equals("is")){ keyText = new Text(t); context.write(keyText, intValue); } } } }
PerWordReducer
package com.hadoop.wordcount; import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class PerWordReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public IntWritable intValue = new IntWritable(0); @Override protected void reduce(Text key, Iterable<IntWritable> value, Context context) throws IOException, InterruptedException { int sum = 0; while(value.iterator().hasNext()){ sum += value.iterator().next().get(); } intValue.set(sum); context.write(key, intValue); } }
PerWordCount
package com.hadoop.wordcount; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; import com.hadoop.mapreducer.MapperClass; import com.hadoop.mapreducer.ReducerClass; import com.hadoop.mapreducer.WordCount; public class PerWordCount { public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); System.out.println("otherArgs.length:"+otherArgs.length); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job = new Job(conf, "word count"); job.setJarByClass(PerWordCount.class); job.setMapperClass(PerWordMapper.class); job.setCombinerClass(PerWordReducer.class); job.setReducerClass(PerWordReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
你可以在博主的新页面同样看到这篇文章。
这篇文章我在写第二遍,第一遍写的很详细,可惜因为我很久没用vim了,它生气了,我不小心按错了键,写的好几百行的都没了,这肯定是上天嫌我懒得太久没写了故意来给我这么一下的,好了,不说废话了,下面就是将怎么使用python脚本来在terminal同时更新你在微博和twitter的状态了。
先说微博
首先第一步我们都是要先申请一个开发者的帐号的,关于具体的一些细节可以看看这里,里面讲了授权使用的方式和原理,像我使用的是第三方的SDK,就只是简单的看了一下,没有太深入的了解,不过想要开发SDK的同学还是可以好好研究的。
下面就是代码了:
from weibo import APIClient from re import split import urllib,httplib APP_KEY = '1******671' #youre app key APP_SECRET = 'e623c*************bfa30b23' #youre app secret # callback url, your must set this URL in your "my application->appInfos-> advanced info" CALLBACK_URL = 'http://ww****shore.com' ACCOUNT = 'bo******@gmail.com'#your email address PASSWORD = '*********' #your pw #for getting the code contained in the callback url def get_code(url): conn = httplib.HTTPSConnection('api.weibo.com') postdata = urllib.urlencode ({'client_id':APP_KEY,'response_type':'code','redirect_uri':CALLBACK_URL,'action':'submit','userId':ACCOUNT,'passwd':PASSWORD,'isLoginSina':0,'from':'','regCallback':'','state':'','ticket':'','withOfficalFlag':0}) conn.request('POST','/oauth2/authorize',postdata,{'Referer':url,'Content-Type': 'application/x-www-form-urlencoded'}) res = conn.getresponse() location = res.getheader('location') code = location.split('=')[1] conn.close() return code def post_weibo(post_contents): print "weibo posting..." #for getting the authorize url client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) url = client.get_authorize_url() code = get_code(url) r = client.request_access_token(code) access_token = r.access_token # The token return by sina expires_in = r.expires_in #save the access token client.set_access_token(access_token, expires_in) results = client.post.statuses__update(status=post_contents) return results
从上面的代码中,有一点需要注意一下那就是,get_code(),这个函数是获取了授权码,在本来的SDK中是没有这一步的,但是少了这一步我们就需要人工的在浏览区的网页上点击连接授权,而我却只想在命令行里面完成所有的事情,所以才有了这个函数,函数中是使用了httplib包模拟了web请求,并且处理了返回的信息,从而获取到了授权码。
之所以没有实现人人网,也是因为授权码的问题,可以看看人人网的开发者文档,授权方式和微博相似,不同就在于不能够我们自己通过代码发送请求来获取授权码,这也就解释了为什么人人网推荐的python SDK都是挂在GAE上的,应该就是因为这个原因,需要我们亲自在网页上进行授权,也正式因为此,我才放弃了人人。其实他们有一个用户名密码端的服务,不过需要填表申请,表我是填了,不过后面就没有消息了,我相信如果我申请成功了,那么就可以使用和微博相同的方式了获取授权码从而获取调用API的tokens了。还是希望有聪明才智的哪位大牛搞成功了,看到这篇文章可以教一下我。
再说Twitter
看了推特的文档才知道美帝的东西就是好,他们用授权方式要更加先进,减少了授权了步骤,使用REST API也使开发者更方便的开发SDK,所以推特的SDK明显要好很多。最受欢迎的tweepy是我的第一次尝试,失败原因至今不是很明确,但因该和我使用的代理有关系,也就是说在国内使用代理上推特和Fb的要注意下,使用tweepy可能会带来的失败。除此之外,还有一点提醒大家需要注意,就是申请的twitter的App是有访问级别的限制的,默认是read only,可以自己改成 read&write 或者 read, write & direct message 都是可以的。
后来我选择的