codeblock

2016年3月30日 星期三

Python Challenge level 14


http://www.pythonchallenge.com/pc/return/italy.html (login: huge/file)

- "Walk around" is the title. A spiral bread. A 10000*1 picture named "wire.png" presented as a 100*100 one. An equation: 100*100 = (100+99+99+98) + (...
- What we should do is rearrange the pixels in wire.png in a spiral manner like that bread and make it a 100*100 picture!
- The 100, 99, 99, 98, 98, 97, 97, ... is the number of steps we should take when we travel a 100*100 square matrix in a spiral manner. Firstly, go right 100 elements, and go down 99 elements, go left 99 and go up 98 elements; then go right again 97, go down 97, and so forth.
- 1+2+3+...+n = n(n-1)/2
  2(1+2+3+...+n) = n(n-1) = n*n - n
  so, n*n = 2*1+2*2+2*3+...+2*(n-1) + n

from PIL import Image
import F_14
wire = Image.open("wire.png")
width, height = wire.size
pixels = wire.load()

result = Image.new(mode="RGB", size=(100, 100), color=0)
pixcels_result = result.load()

step_list=[100-1]
for i in range(99):
 step_list.append(99-i)
 step_list.append(99-i)

current_direct = "r"
location = [0, 0]
step = 0

for i in range(len(step_list)):
 for j in range(step_list[i]):
  pixcels_result[location[0], location[1]] = pixels[step, 0]
  step = step + 1
  location = F_14.get_next_location(location, current_direct)
  
 current_direct = F_14.get_next_direction(current_direct)

result.save("result.jpg")

F_14.py
def get_next_location(location, current_direct):
 next_locaiton  = location 
 if current_direct == "d": next_locaiton[1] += 1
 elif current_direct == "r": next_locaiton[0] += 1
 elif current_direct == "u": next_locaiton[1] -= 1
 elif current_direct == "l":  next_locaiton[0] -= 1
 return next_locaiton

def get_next_direction(current_direct):
 next_direction = ""
 if current_direct == "d": next_direction = "l"
 elif current_direct == "r": next_direction = "d"
 elif current_direct == "u": next_direction = "r"
 elif current_direct == "l":  next_direction = "u"
 return next_direction
- The result is a cute cat!







- Browse http://www.pythonchallenge.com/pc/return/cat.html, you'll see "and its name is uzi. you'll hear from him later. " The final answer is "uzi".

沒有留言:

張貼留言