'Dojo/Python'에 해당되는 글 4건
- 2009/02/06 Code Golf - Choose
- 2008/12/12 Saving-time (python)
- 2008/10/08 File Modification
- 2008/09/24 Ugly Numbers
원문
내 사이즈는 87바이트. 포럼을 보니 파스칼 삼각형을 이용하는 방법도 있는것 같은데. 1등은 39바이트. 내 코드의 반도 안된다. 어떻게한거지?
1바이트를 줄여 86바이트가 되었다.
def c(t):
s=1
for i in range(1,t[1]+1):
s=s*(t[0]-i+1)/i
print s
c(input())
s=1
for i in range(1,t[1]+1):
s=s*(t[0]-i+1)/i
print s
c(input())
내 사이즈는 87바이트. 포럼을 보니 파스칼 삼각형을 이용하는 방법도 있는것 같은데. 1등은 39바이트. 내 코드의 반도 안된다. 어떻게한거지?
def c(t,s):
for i in range(1,t[1]+1):
s=s*(t[0]-i+1)/i
print s
c(input(),1)
for i in range(1,t[1]+1):
s=s*(t[0]-i+1)/i
print s
c(input(),1)
1바이트를 줄여 86바이트가 되었다.
링크
첫 번째 코드 : 290바이트. 전체 61위 (2008.12.12. 오전 10시 현재)
- 출력되는 시계모양을 정의하는 코드사이즈를 줄이는데 치중했음
- 시계모양의 텍스트에서 어떤 패턴을 발견하면 더 줄일 수 있지 않을까 생각중.
첫 번째 코드 : 290바이트. 전체 61위 (2008.12.12. 오전 10시 현재)
s=' '
o='o'
n='\n'
c=[s*8,o,n,s*4,o,s*7,o,n,n,s,o,s*13,o,n,n,o,s*15,o,n,n,s,o,s*13,o,n,n,s*4,o,s*7,o,n,s*8,o]
i=[1,6,12,17,23,29,32,27,21,15,10,4]
a = raw_input().split(':')
h=i[int(a[0])%12]
m=i[int(a[1])/5]
c[h]='h'
if h==m:c[m]='x'
else: c[m]='m'
print ''.join(k for k in c)
o='o'
n='\n'
c=[s*8,o,n,s*4,o,s*7,o,n,n,s,o,s*13,o,n,n,o,s*15,o,n,n,s,o,s*13,o,n,n,s*4,o,s*7,o,n,s*8,o]
i=[1,6,12,17,23,29,32,27,21,15,10,4]
a = raw_input().split(':')
h=i[int(a[0])%12]
m=i[int(a[1])/5]
c[h]='h'
if h==m:c[m]='x'
else: c[m]='m'
print ''.join(k for k in c)
- 출력되는 시계모양을 정의하는 코드사이즈를 줄이는데 치중했음
- 시계모양의 텍스트에서 어떤 패턴을 발견하면 더 줄일 수 있지 않을까 생각중.
파일의 앞부분 1024 바이트를 잘라서 원래 파일 이름과 함께 md5(원파일이름).idx 에 저장. 원래파일이름은 md5(원래파일이름)으로 변경. 반대의 기능도 수행.
#st.py
import os
import md5
import glob
def makeIdx(fName, head):
idxNm = md5.new(fName.split('/')[-1]).hexdigest() + '.idx'
fIdx = file(idxNm, 'wb+')
fIdx.write(fName + ' ' + head)
fIdx.close()
def getInfoFromIdx(fName):
size = os.path.getsize(fName)
fIdx = file(fName, 'rb')
fName = fIdx.read(size-1024)[:-1]
head = fIdx.read(1024)
fIdx.close()
return fName, head
def transform(fName):
f = file(fName, 'r+b')
head = f.read(1024)
makeIdx(fName, head)
f.seek(0)
f.write('a'*1024)
f.close()
os.rename(fName, md5.new(fName.split('/')[0]).hexdigest())
def restore(idxNameWithExt):
idxNameWithtExt = idxNameWithExt[2:]
print idxNameWithtExt
if not os.path.exists(idxNameWithExt):
print '.idx file not exist'
return False
idxNameWoExt = idxNameWithExt.split('.idx')[0]
if not os.path.exists(idxNameWoExt):
print 'file not exist'
return False
size = os.path.getsize(idxNameWithExt)
orgName, head = getInfoFromIdx(idxNameWithExt)
f = file(idxNameWoExt, 'r+b')
f.seek(0)
f.write(head)
f.close()
#os.rename(idxName.split('.idx')[0], orgName)
#print idxNameWoExt, orgName
os.rename(idxNameWoExt, orgName)
os.remove(idxNameWithtExt)
#if __name__=='__main__':
#transform('sd.jpg')
#restore('f301ea0db5dde29b5b5db4ff6953014b.idx')
#g = glob.glob(os.path.join('./', '*.idx'))
#print g
#menu
import os
import md5
import glob
def makeIdx(fName, head):
idxNm = md5.new(fName.split('/')[-1]).hexdigest() + '.idx'
fIdx = file(idxNm, 'wb+')
fIdx.write(fName + ' ' + head)
fIdx.close()
def getInfoFromIdx(fName):
size = os.path.getsize(fName)
fIdx = file(fName, 'rb')
fName = fIdx.read(size-1024)[:-1]
head = fIdx.read(1024)
fIdx.close()
return fName, head
def transform(fName):
f = file(fName, 'r+b')
head = f.read(1024)
makeIdx(fName, head)
f.seek(0)
f.write('a'*1024)
f.close()
os.rename(fName, md5.new(fName.split('/')[0]).hexdigest())
def restore(idxNameWithExt):
idxNameWithtExt = idxNameWithExt[2:]
print idxNameWithtExt
if not os.path.exists(idxNameWithExt):
print '.idx file not exist'
return False
idxNameWoExt = idxNameWithExt.split('.idx')[0]
if not os.path.exists(idxNameWoExt):
print 'file not exist'
return False
size = os.path.getsize(idxNameWithExt)
orgName, head = getInfoFromIdx(idxNameWithExt)
f = file(idxNameWoExt, 'r+b')
f.seek(0)
f.write(head)
f.close()
#os.rename(idxName.split('.idx')[0], orgName)
#print idxNameWoExt, orgName
os.rename(idxNameWoExt, orgName)
os.remove(idxNameWithtExt)
#if __name__=='__main__':
#transform('sd.jpg')
#restore('f301ea0db5dde29b5b5db4ff6953014b.idx')
#g = glob.glob(os.path.join('./', '*.idx'))
#print g
#menu
#mn.py
from st import *
import glob
import re
r = re.compile('[0-9a-fA-F]+')
def mnuMain():
print '1. List .idx Files'
print '2. List normal Files'
print '3. Transform'
print '4. Restore'
print '5. Exit'
return input('Select: ')
def getItems(ext=''):
items = []
if ext == '':
ext = '*'
#for each in glob.glob(os.path.join('./', ext)):
for each in glob.glob(os.path.join('./', ext)):
if ext == '*':
items.append(each[2:])
elif each.endswith('.idx'):
nm, hd = getInfoFromIdx(each)
items.append([nm, each])
return items
def mnuListIdx():
print '\n### List of transformed files\n'
i = 1
items = getItems('*.idx')
for item in items:
print '%d. %s (%s)' % (i, item[0], item[1] )
i+=1
print '\n### End of List.\n\n'
def mnuListNorm():
print '\n### List of every files\n'
i = 1
items = getItems()
for item in items:
if r.match(item) is None:
print '%d. %s' % (i, item)
i+=1
print '\n### End of List.\n\n'
def mnuTransform():
mnuListNorm()
items = getItems()
select = input('Select a file to transform : ')
transform(items[select-1])
def mnuRestore():
mnuListIdx()
items = getItems('*.idx')
#print items[0][1]
select = input('Select a file to restore : ')
restore(items[select-1][1])
def main():
while True:
choice = mnuMain()
if choice not in range(1,5):
break
{1:mnuListIdx, 2:mnuListNorm, 3:mnuTransform, 4:mnuRestore}[choice]()
if __name__=='__main__':
main()
from st import *
import glob
import re
r = re.compile('[0-9a-fA-F]+')
def mnuMain():
print '1. List .idx Files'
print '2. List normal Files'
print '3. Transform'
print '4. Restore'
print '5. Exit'
return input('Select: ')
def getItems(ext=''):
items = []
if ext == '':
ext = '*'
#for each in glob.glob(os.path.join('./', ext)):
for each in glob.glob(os.path.join('./', ext)):
if ext == '*':
items.append(each[2:])
elif each.endswith('.idx'):
nm, hd = getInfoFromIdx(each)
items.append([nm, each])
return items
def mnuListIdx():
print '\n### List of transformed files\n'
i = 1
items = getItems('*.idx')
for item in items:
print '%d. %s (%s)' % (i, item[0], item[1] )
i+=1
print '\n### End of List.\n\n'
def mnuListNorm():
print '\n### List of every files\n'
i = 1
items = getItems()
for item in items:
if r.match(item) is None:
print '%d. %s' % (i, item)
i+=1
print '\n### End of List.\n\n'
def mnuTransform():
mnuListNorm()
items = getItems()
select = input('Select a file to transform : ')
transform(items[select-1])
def mnuRestore():
mnuListIdx()
items = getItems('*.idx')
#print items[0][1]
select = input('Select a file to restore : ')
restore(items[select-1][1])
def main():
while True:
choice = mnuMain()
if choice not in range(1,5):
break
{1:mnuListIdx, 2:mnuListNorm, 3:mnuTransform, 4:mnuRestore}[choice]()
if __name__=='__main__':
main()
UglyNumbers
내 노트북을 기준으로 이 코드는 0.00913초. 내 코드는 0.0693초. 약 8배 정도의 차이가 난다.
def ugly(pos):
count = 0
uglys = [2,3,5]
minVal = 0
while count != pos-1:
minVal = min(uglys)
if uglys.count(minVal*2)==0:
uglys.append(minVal*2)
if uglys.count(minVal*3)==0:
uglys.append(minVal*3)
if uglys.count(minVal*5)==0:
uglys.append(minVal*5)
uglys.remove(minVal)
count += 1
print minVal
if __name__=='__main__':
ugly(1550)내 노트북을 기준으로 이 코드는 0.00913초. 내 코드는 0.0693초. 약 8배 정도의 차이가 난다.

Prev
Rss Feed