PyCon Jp 2012 ã®ãTop of Pythonistaãã§2ä½ã«ãªãã¾ããï¼
PyCon JP 2012å
ã§éå¬ãããTop of Pythonista決å®æ¦ã«ã¦ããªãã¨2ä½ã«ãªã£ã¦ãã¾ãã¾ããï½
åç´ã«ãã¹ã³ã¢å¹çï¼åä½é¢ç©å½ããã®ã¹ã³ã¢ï¼ã®é«ãé ã«å·¦ä¸ããé
ç½®ãããã ãã§ãããããæ´ç·´ããã¦ãªãã¦ãã£ãã¼éãã³ã¼ããªãã§ãããã¾ãå®è£
ããè
åã¡ã¨ãããã¨ã§ï½
ã¦ãªããã§æ¥ããããã§ããã³ã¼ãã
# -*- coding: utf-8 -*- import json import random import sys def arrange(area, item): for i in range(len(area)): for j in range(len(area[0])): if area[i][j] == 0: judged = judge(area, item,i, j) if judged[0]: put(area, item, i, j) return {'id': item['id'], 'x': j, 'y': i} elif judged[1]: continue else: print('break!') break return None def judge(area, item, i, j): if not (len(area) > (i+item['height']) \ and len(area[0]) > (j+item['width'])): return (False, False) for y in range(i, i+item['height']): for x in range(j, j+item['width']): if area[y][x] == 1: return (False, True) return (True, False) def put(area, item, i, j): for y in range(i, i+item['height']): for x in range(j, j+item['width']): area[y][x] = 1 def efficiency(item): return 1.0 * item['score'] / (item['height'] * item['width']) ifname = sys.argv[1] ofname = sys.argv[2] width = int(sys.argv[3]) height = int(sys.argv[4]) items = json.load(open(ifname)) items = sorted(items, key=lambda x: -efficiency(x)) area = [[0 for j in range(width)] for i in range(height)] result = [] for item in items: matchItem = arrange(area, item) if matchItem: result.append(matchItem) json.dump(result, open(ofname, 'w'))
The Zen of Python, by Tim Peters ...ä¸ç¥... Flat is better than nested. ...å¾ç¥...
ããããªãããã¹ããã¾ããã§ãorz