zoukankan      html  css  js  c++  java
  • Python Challenge 第十四关

    14关页面上是两张图,一张是一个卷面包,一张类似条形码的东西。没任何提示,就看源代码,果然,有一行注释:

    <!-- remember: 100*100 = (100+99+99+98) + (...  -->

    然后把那两张图都下了下来。面包那张没看出什么端倪, 反倒是条形码那张,下下来居然是个 10000*1的奇葩图片。然后这个注释里的列表,刚好100*100也等于10000,看来应该处理的就是这张图了。

    现在应该先把这个注释中的列表给求出来。每个组合四个数,数字抵减,加起来为10000,可以猜想两组相加,相对应数的和为100,这样就需要 10000 / 100 / 4 * 2 = 50 组。可以用个列表解析:

    l = [[i, i-1, i-1, i-2] for i in xrange(100, 0, -2)]

    为了测验一下对不对,可以把元素都加一下:

    a = [sum(i) for i in l]
    print sum(a)

    刚好10000。

    得到了这个列表,联想到刚才那个面包圈,想着先横着100,再竖着99,再向左横着99,向上竖98,刚好就是一圈,这样一圈一圈下去就能把这个 10000*1 的图给围成 100*100的了:

        img_old = Image.open(r'C:UsersLeoDesktopwire.png')
        img_old_value = img_old.load()
        img_new = Image.new(img_old.mode, (100, 100))
        img_new_value = img_new.load()
        
        l = [[i, i-1, i-1, i-2] for i in xrange(100, 0, -2)]
        idx = 0
        x = y = 0
        for indices in l:
            for j in xrange(indices[0]):
                img_new_value[x, y] = img_old_value[idx, 0]
                x += 1
                idx += 1
            x -= 1
            for j in xrange(indices[1]):
                img_new_value[x, y] = img_old_value[idx, 0]
                y += 1
                idx += 1
            y -= 1
            for j in xrange(indices[2]):
                img_new_value[x, y] = img_old_value[idx, 0]
                x -= 1
                idx += 1
            x += 1
            for j in xrange(indices[3]):
                img_new_value[x, y] = img_old_value[idx, 0]
                y -= 1
                idx += 1
            y += 1
        img_new.show()       

    这里 idx 是原图的 x 坐标,在大循环里,分别有四个小循环,分别是在新图中向右、向下、向左、向上赋值。这里我开始写的时候每个小循环后面没有那些 +=1 或 -=1,结果总是报错 index out of range。后来仔细一分析,比如第一行的像素,赋值得循环100次,这样 x 的值就被累加到了 100,但是 x 的最大值为 99,这样就超了,下面的每次小循环都一样,这样就需要在循环完再减/加一次。

    得到的图像是一只猫。然后改了一下 url 为 cat,进入一个新页面,有那只猫的原图。上书一句:and its name is uzi. you'll hear from him later. 试了下把 cat 改成 uzi,进入了下一关:http://www.pythonchallenge.com/pc/return/uzi.html

    之后去看网站上其他人的解法,发现我的代码太复杂。这里有个例子:

    def spiral(source):
         target = Image.new(source.mode, (100, 100))
         left, top, right, bottom = (0, 0, 99, 99)
         x, y = 0, 0
         dirx, diry = 1, 0
         for i in xrange(10000):
             target.putpixel((x, y), source.getpixel((i, 0)))
             if dirx == 1 and x == right:
                 dirx, diry = 0, 1
                 top += 1
             elif dirx == -1 and x == left:
                 dirx, diry = 0, -1
                 bottom -= 1
             elif diry == 1 and y == bottom:
                 dirx, diry = -1, 0
                 right -= 1
             elif diry == -1 and y == top:
                 dirx, diry = 1, 0
                 left += 1
             x += dirx
             y += diry
         return target

    这里只有一个循环,根据边界条件来改变坐标,逻辑更加清楚,值得借鉴。

  • 相关阅读:
    Otter详解
    为什么要使用Netty
    haproxy实现mysql集群负载均衡
    Mysql主从复制
    java编程思想读书笔记三(HashMap详解)
    代码界的石器时代
    补码的产生与应用
    java编程思想读书笔记二(对象的创建)
    java编程思想读书笔记一(面向对象)
    Apache VFS
  • 原文地址:https://www.cnblogs.com/dukeleo/p/3471841.html
Copyright © 2011-2022 走看看