挑战PYTHON

挑战PYTHON第十一关 

又是图片处理,而且是一张像打了马赛克的图片。我们能从里边得到什么数据呢。


标题是奇偶,看来是只选择奇数像素的点了。

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

pi=Image.open("cave.jpg")

X,Y = pi.size

for y in range(Y):
    for x in range(X):
        if (x+y)%2==1:
            pi.putpixel((x,y),(0,0,0))
pi.show()
好嘛,是EVIL



挑战PYTHON第十二关

图像地址是evil1.jpg,改为evil2.jpg获得如下图片


又获得evil2.gfx文件

按照evil1.jpg将牌分成5份的意思,将evil2.gfx分成5份,ubuntu自动识别为图片。


好了,答案就是disproportional 

挑战PYTHON第十三关

给evil打电话。催债?那是google的事吧。好吧,找到电话号码就可以了。

evil是谁,evil4.jpg会告诉你是Bert。因为其他人下面的回复会变成

He is not the evil

import xmlrpclib
server=xmlrpclib.ServerProxy("http://www.pythonchallenge.com/pc/phonebook.php")
print server.system.listMethods()
print server.phone('Bert')
#555-ITALY
答案也有了 italy

挑战PYTHON第十四关

一坨像大便的面包


还带着条码,有谁会买这玩意吗?看似条码的这玩意其实是10000*1的图片,调整大小为100*100如下图。

我想应该是用python把像素转换为100*100的图片才对。看到大便有没有受到启发,要把这一条像素,像拉大便一样,一圈一圈盘到最中间。

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

pi1=Image.open("wire.png")
pi2=Image.new("RGB",(100,100))
X,Y=pi2.size
leng=50
curve=[(1,0),(0,1),(-1,0),(0,-1)]
x,y,z=0,0,0
for le in range(50):#共计50圈
    for i in curve:#完成一圈
        for l in range(99-2*le):#完成一行
            print x,y,z
            pi2.putpixel((x,y),pi1.getpixel((z,0)))
            x += i[0]
            y += i[1]
            z+=1
    x+=1
    y+=1
pi2.show()
pi2.save("wire2.png")

看来答案就是cat,这只猫好像叫uzi

挑战PYTHON第十五关






python threading效果测试

刚把golang的routine测试了一下,情不自禁转python2.7的threading进行测试。(python3测试结果比python2.7要差一些)
由于python的性能问题,我们把循环降低了2个数量级。
通过测试数据,可以肯定的是python多线程分配到了每个逻辑CPU。但每个逻辑CPU的利用率都很低,怀疑是python多线程中每个时刻只有1个线程在运行,时不时换换CPU而已,所以效率很低,这也是python多线程被大家称为假象的原因。而且线程调度开销过高,多线程明显比单线程耗时高很多。
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from threading import Thread
import math,time

def dothings(s,starttime):
    i=0
    while True:
        if i==10000000:break
        d=i*i
        d=math.sqrt(d)
        i+=1
    ti = time.time()
    print "thread %d cost %d seconds."%(s,ti-starttime)

def test(ng):
    starttime=time.time()
    ths=[]
    for i in range(ng):
        th = Thread(target=dothings,args=(i,starttime))
        ths.append(th) 
    for th in ths:
        th.start()
    for th in ths:
        th.join()

if __name__=="__main__":
    for i in range(4):
        print "thread %d."%(i+1)
        test(i+1)


thread 1.
thread 0 cost 3 seconds.
thread 2.
thread 0 cost 11 seconds.
thread 1 cost 11 seconds.
thread 3.
thread 2 cost 17 seconds.
thread 0 cost 18 seconds.
thread 1 cost 18 seconds.
thread 4.
thread 0 cost 24 seconds.
thread 1 cost 25 seconds.
thread 3 cost 25 seconds.
thread 2 cost 25 seconds.

golang goroutine效果测试

             以前没有关注过多线程效率问题,如今golang的goroutine如此方便,就对启用逻辑CPU数量和运行goroutine数量的变化做了个测试。体现了goroutine并发的一些特性。

       从数据上看有以下特色(逻辑CPU数量:num_cpu,goroutine数量:num_routine, 单CPU单任务基础耗时routine1_time):
1.并发:
num_cpu小于num_routine时,可以看作是每个逻辑CPU只运行一个routine,多余routine进行排队等待。等某个routine运行完后,从排队的routine选择一个运行。下边的数据很明显说明这个问题。

cpu:1,goroutine:4
goroutine 1 cost 10098797000 nanoseconds.
goroutine 2 cost 20178997000 nanoseconds.
goroutine 3 cost 30291315000 nanoseconds.
goroutine 4 cost 40370410000 nanoseconds.
num_cpu不小于num_routine时,并发效果很好。
cpu:1,goroutine:1
goroutine 1 cost 10077725000 nanoseconds.
cpu:2,goroutine:2
goroutine 2 cost 10131996000 nanoseconds.
goroutine 1 cost 10177410000 nanoseconds.
2.逻辑CPU和实体CPU:num_cpu不小于num_routine时(num_routine==4),1个CPU耗时4倍routine1_time,2个CPU耗时2倍,3个CPU耗时2倍多,4个CPU耗时2倍。
   3、4个CPU时表现非常失望,为什么?因为我的电脑是双CPU 4逻辑CPU的i3内核。把CPU加上逻辑二字后CPU数量翻倍,但性能下降很多,每个routine耗时均为2倍time_routine.通过双核四任务与四核四任务对比,可发现逻辑CPU并不增加性能,但实现了并发。

cpu:2,goroutine:4
goroutine 1 cost 10133366000 nanoseconds.
goroutine 2 cost 10211199000 nanoseconds.
goroutine 3 cost 20349898000 nanoseconds.
goroutine 4 cost 20407353000 nanoseconds.
cpu:4,goroutine:4
goroutine 1 cost 19594099000 nanoseconds.
goroutine 3 cost 20004954000 nanoseconds.
goroutine 2 cost 20286426000 nanoseconds.
goroutine 4 cost 20325848000 nanoseconds.
所以在设置runtime.GOMAXPROCS时,如果同意让一部分人先富裕起来,建议不要高于实体CPU的数量。如果你出于其他要求,需要所有routine并发,当然可以选择让所有人都慢下来。

Go语言: 高亮代码由发芽网提供

package main

import (
    "fmt"
    "runtime"
    "math"
    "time"
)

var c = make(chan int)

func dothings(s int,starttime int64){
    for i:=0.0;i<1000000000;i++{
        b:=i*i
        b=math.Sqrt(b*b)
    }
    ti := time.Now().UnixNano()
    fmt.Printf("goroutine %d cost %d nanoseconds.\n",s,ti-starttime)
    c<-1
}

func test(nc,ng int){
    runtime.GOMAXPROCS(nc)
    starttime := time.Now().UnixNano()
    for i:=1;i<=ng;i++{
        go dothings(i,starttime)
    } 
    for i:=1;i<=ng;i++{
        <-c
    }
}

func main(){
    for i:=1;i<5;i++{
        for j:=1;j<5;j++{
            fmt.Printf("cpu:%d,goroutine:%d\n",i,j)
            test(i,j)
        }
    }
}


cpu:1,goroutine:1
goroutine 1 cost 10077725000 nanoseconds.
cpu:1,goroutine:2
goroutine 1 cost 10062447000 nanoseconds.
goroutine 2 cost 20169990000 nanoseconds.
cpu:1,goroutine:3
goroutine 1 cost 10100405000 nanoseconds.
goroutine 2 cost 20159491000 nanoseconds.
goroutine 3 cost 30259100000 nanoseconds.
cpu:1,goroutine:4
goroutine 1 cost 10098797000 nanoseconds.
goroutine 2 cost 20178997000 nanoseconds.
goroutine 3 cost 30291315000 nanoseconds.
goroutine 4 cost 40370410000 nanoseconds.
=========================================
cpu:2,goroutine:1
goroutine 1 cost 10079556000 nanoseconds.
cpu:2,goroutine:2
goroutine 2 cost 10131996000 nanoseconds.
goroutine 1 cost 10177410000 nanoseconds.
cpu:2,goroutine:3
goroutine 1 cost 10126434000 nanoseconds.
goroutine 2 cost 10179198000 nanoseconds.
goroutine 3 cost 20205823000 nanoseconds.
cpu:2,goroutine:4
goroutine 1 cost 10133366000 nanoseconds.
goroutine 2 cost 10211199000 nanoseconds.
goroutine 3 cost 20349898000 nanoseconds.
goroutine 4 cost 20407353000 nanoseconds.
=========================================
cpu:3,goroutine:1
goroutine 1 cost 10064761000 nanoseconds.
cpu:3,goroutine:2
goroutine 1 cost 10150462000 nanoseconds.
goroutine 2 cost 10182808000 nanoseconds.
cpu:3,goroutine:3
goroutine 1 cost 11184450000 nanoseconds.
goroutine 2 cost 14814134000 nanoseconds.
goroutine 3 cost 15711271000 nanoseconds.
cpu:3,goroutine:4
goroutine 1 cost 12906835000 nanoseconds.
goroutine 2 cost 14257683000 nanoseconds.
goroutine 3 cost 16885781000 nanoseconds.
goroutine 4 cost 23757542000 nanoseconds.
=========================================
cpu:4,goroutine:1
goroutine 1 cost 10104092000 nanoseconds.
cpu:4,goroutine:2
goroutine 1 cost 10201556000 nanoseconds.
goroutine 2 cost 10235368000 nanoseconds.
cpu:4,goroutine:3
goroutine 1 cost 14536086000 nanoseconds.
goroutine 3 cost 15100665000 nanoseconds.
goroutine 2 cost 15390762000 nanoseconds.
cpu:4,goroutine:4
goroutine 1 cost 19594099000 nanoseconds.
goroutine 3 cost 20004954000 nanoseconds.
goroutine 2 cost 20286426000 nanoseconds.
goroutine 4 cost 20325848000 nanoseconds.


挑战PYTHON

挑战PYTHON第六关 

提示信息:
<!-- <-- zip --> 转换URL为zip文件
得到如下zip文件列表

打开reademe.txt

打开90052.txt提示:

Next nothing is 94191

套取格式最终获取46145.txt内容

Collect the comments.

很明显是要我们读取zip文件的comment信息
导入zipfile库,使用zipfile.ZipFile("channel.zip"),按照94191至46145逐个获取comment信息。

运行显示结果

OK,下一关地址入口 HOCKEY?NO

it's in the air. look at the letters.

看上图会发现HOCKEY,HOCKEY分别是由oxygen画出来的。
所以下一关地址是 oxygen

挑战PYTHON第七关 
打开后获得图片


看来需要使用PIL了,中间一行有我们需要的数据。


输出图片的7像素间隔数据的RGB数值相同。R值转换为字符

再将提示的[105, 110, 116, 101, 103, 114, 105, 116, 121]转换为字符便获得了过关钥匙 integrity

挑战PYTHON第八关 


HTML代码得到如下提示。据过关的网友提示BZh91AY是bzip2的数据头
<!--
un: 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'
pw: 'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'
-->
通过简单的解码获得帐号密码

点击小蜜蜂 登陆


下一关入口 小蜜蜂

挑战PYTHON
第九关 


看图得到提示,黑点连线可以画出轮廓。
#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PIL import Image

pi=Image.open("good.jpg")
#pi=Image.new("RGB",pi.size)
a=[146399163403170393169391166386170381170371170355169346167335170329170320170310171301,173290178289182287188286190286192291194296195305194307191312190316190321192331193338196,341197346199352198360197366197373196380197383196387192389191392190396189400194401201,402208403213402216401219397219393216390215385215379213373213365212360210353210347212,338213329214319215311215306216296218290221283225282233284238287243290250291255294261,293265291271291273289278287279285281280284278284276287277289283291286294291296295299,300301304304320305327306332307341306349303354301364301371297375292384291386302393324,391333387328375329367329353330341331328336319338310341304341285341278343269344262346,259346251349259349264349273349280349288349295349298354293356286354279352268352257,351249350234351211352197354185353171351154348147342137339132330122327120314116304117293,118284118281122275128265129257131244133239134228136221137214138209135201132192130184131,175129170131159134157134160130170125176114176102173103172108171111163115156116149117142116,13611512911512411512011511511711312010912210212210012195121891158711082109841188912393129,1001301081321101331101361071381051409513886141791497715581162901659716799171109171107161,111156113170115185118208117223121239128251133259136266139276143290148310151332155348156,353153366149379147394146399]
b=[156141165135169131176130187134191140191146186150179155175157168157163157159157158164159,175159181157191154197153205153210152212147215146218143220132220125217119209116196115185114,1721141671121611091651071709917197167891648116277155811488714096138105141110136111126113,129118117128114137115146114155115158121157128156134157136156136]
c=a+b
for i in range(len(c)/2):
    print i,len(c)
    pi.putpixel((c[i*2],c[i*2+1]),(255,255,255))
pi.show()



看清楚是有长牛角哦,所以是公牛

挑战PYTHON第十关 



原来是这头牛。会得到sequence.txt,求len(a[30]) = ?

a = [1, 11, 21, 1211, 111221, 
会得到什么规律吗?
我是看不出规律来,不过我有google,其实是百度知道。。。

“其实每一行都是对上一行的“统计”。 
第一行:“1”统计为:1个1,去掉“个”字,就变成了“11”,也就是第二行。 
同理,第二行可统计为:2个1,去掉“个”字,就变成了“21”,也就是第三行。 
同理,第三行可统计为:1个2和1个1,去掉“个”字和“和”字,就变成了“1211”,也就是第四行。 
同理,第四行可统计为:1个1和1个2和2个1,去掉“个”字和“和”字,就变成了“111221”,也就是第五行。 
同理,第五行可统计为:3个1和2个2和1个1,去掉“个”字和“和”字,就变成了“312211”,也就是第六行。”
#!/usr/bin/env python
#-*- coding:utf-8 -*-

def next(num):
    c=[]
    for i in num:
        if c:
            if c[-1][0] == i:
                c[-1][1] += 1
                continue
        c.append([i,1])
    nextstr=''
    for i in c:
        nextstr+=str(i[1])+i[0]
    return nextstr
a=['1']
for i in range(30):
    a.append(next(a[-1]))
print len(a[30])

结果当然是5808了。

使用gedit调试golang

前几天受到某位同学的指导(sorry,后来找不到文章来源了),在gedit的插件上添加了方便调试go语言程序的脚本。经过使用处理了bug,也个性化的修改了下,能够更快速的调试。如下一个是编译的,另外一个是编译并执行。6g|6l的编译速度要比gccgo快一些,所以我选择使用6g|6l。使用32位的同学请自行将6g|6l修改为8g|8l。关于gedit添加脚本的方法请自行Google。 (..更多内容)