'Coding > Python 삽질기' 카테고리의 다른 글
[Python] 파일 zip으로 압축하기 (0) | 2010.10.25 |
---|---|
Self number ... 2 (0) | 2010.07.13 |
Self number (1) | 2010.07.10 |
[Python] 파일 zip으로 압축하기 (0) | 2010.10.25 |
---|---|
Self number ... 2 (0) | 2010.07.13 |
Self number (1) | 2010.07.10 |
1000! (0) | 2010.07.14 |
---|---|
Self number (1) | 2010.07.10 |
몬테카를로 법을 이용한 원주율 계산 (0) | 2010.07.09 |
1 부터 5000 사이의 Self number 개수와 그 합을 구하는 예제
#-*- coding: cp949 -*-
# Self Number
# filename : self_number.py
# author : chobocho at korea.com
# date : 2010. 7. 10
#
def Generator(num):
sum = num
while (num > 0):
sum += (num % 10)
num /= 10
return sum
#-------------------------------------------------------------------------------
# main
if __name__ == "__main__":
start_number = 1 # 시작 숫자
end_number = 5000 # 마지막 숫자
self_number_count = 0;
self_number_sum = 0;
mylist = [0] * (end_number + 1) * 2
for idx in range (start_number, (end_number + 1)):
mylist[Generator(idx)] += 1
for idx in range (start_number, (end_number + 1)):
if ( mylist[idx] == 0 ):
self_number_count += 1
self_number_sum += idx
print "Count %d" %(self_number_count)
print "Sum %d" %(self_number_sum)
Self number ... 2 (0) | 2010.07.13 |
---|---|
몬테카를로 법을 이용한 원주율 계산 (0) | 2010.07.09 |
100! (0) | 2010.07.08 |
Self number (1) | 2010.07.10 |
---|---|
100! (0) | 2010.07.08 |
Quick Sort (0) | 2010.06.10 |
#-*- coding: cp949 -*-
# Factorial
# filename : factorial.py
# author : chobocho at korea.com
# date : 2010. 7. 7
#
import sys
def factorial(num):
ret = 0;
if (num < 0):
ret = -1
elif (num == 0):
ret = 1
else :
ret = 1
for idx in range (1, num+1):
ret = ret * idx
return ret
#-------------------------------------------------------------------------------
# main
if __name__ == "__main__":
for i in range (1, 101):
print "%d!= %d" %(i, factorial(i))
몬테카를로 법을 이용한 원주율 계산 (0) | 2010.07.09 |
---|---|
Quick Sort (0) | 2010.06.10 |
[Python] Simple template maker (0) | 2010.02.02 |
Haskell로 Quick Sort를 만들면 2줄로 간단히 나온다.
Python 도 유사하게 구현이 가능하다.
def qsort(list):
if len(list) <= 1:
return list
pivot = random.choice(list)
list.remove(pivot)
return qsort([it for it in list if it < pivot]) + [pivot] + qsort([it for it in list if it >= pivot])
#----------------------------------------------------------
# main
if __name__ == "__main__":
before_sort = [1, 4, 2, 7, 9, 8, 3, 6, 5, 5]
random.seed()
print before_sort;
after_sort = qsort(before_sort)
print after_sort;
100! (0) | 2010.07.08 |
---|---|
[Python] Simple template maker (0) | 2010.02.02 |
[Python] 짧은 코드 모음 (0) | 2010.01.22 |
#-*- coding: cp949 -*-
# Name : py_email.py
# Author : chobocho.com
# Version :
# Copyright :
# Description : Simple template maker
#
from Tkinter import *
import tkMessageBox
import os.path
import glob
import re
class App:
def __init__ (self, master):
frame = Frame(master)
frame.pack()
# Text Area
f0 = Frame(frame, width = 100, height = 100)
f0.grid(row = 0, column = 0)
self.text_scrollbar = Scrollbar (f0, orient=VERTICAL)
self.text = Text(f0, width=80, height = 20, yscrollcommand=self.text_scrollbar.set)
self.text_scrollbar.config(command=self.text.yview)
self.text_scrollbar.pack(side=RIGHT, fill=Y)
self.text.pack()
# List Box area
f1 = Frame(frame, width = 100, height = 100)
f1.grid(row = 0, column = 1)
self.list_label = Label(f1, text="Template")
self.list_label.pack()
self.listbox_scrollbar = Scrollbar (f1, orient=VERTICAL)
self.listbox = Listbox(f1, height=19, yscrollcommand=self.listbox_scrollbar.set)
self.listbox_scrollbar.config(command=self.listbox.yview)
self.listbox_scrollbar.pack(side=RIGHT, fill=Y)
# 파일에서 템플릿을 읽어서 보여 줄 것
self.ReadTemplate()
self.listbox.bind("<Double-Button-1>", self.LoadFile)
self.listbox.pack(side=RIGHT)
# 보내는 사람과 받는 사람을 저장하는 부분
f3 = Frame(frame, width = 100, height = 40)
f3.grid(row = 1, column = 0)
self.label_From = Label(f3, text="From")
self.label_From.grid(row = 0, column = 0, sticky = W)
self.label_To = Label(f3, text="To")
self.label_To.grid(row = 1, column = 0, sticky = W)
self.input_from = Entry(f3)
self.input_from.grid(row = 0, column = 1)
self.input_to = Entry(f3)
self.input_to.grid(row = 1, column = 1)
# 버튼을 그려주는 부분
f4 = Frame(frame, width = 100, height = 40)
f4.grid(row = 1, column = 1)
#self.load_button = Button(f4, text="Load", command = self.LoadFile)
#self.load_button.grid(row = 0, column = 0)
self.clear_button = Button(f4, text="Clear", command = self.ClearText)
self.clear_button.grid(row = 0, column = 0)
self.info_button = Button(f4, text="Info", command = self.ShowInfo)
self.info_button.grid(row = 0, column = 1)
def ClearText(self):
self.text.delete(1.0, END)
def ShowInfo(self):
tkMessageBox.showinfo("Information","http://chobocho.com\nVersion 0.2")
def GetFromTo(self):
self.sender = self.input_from.get()
self.receiver = self.input_to.get()
def ReadTemplate(self):
all_flist = glob.glob ("*.txt")
for f in all_flist:
if os.path.exists(f):
self.listbox.insert(END, f.decode('cp949'))
def LoadFile(self, event):
self.items = self.listbox.curselection()
self.el = self.listbox.get(self.items)
self.GetFromTo()
self.ClearText()
if os.path.exists(self.el):
self.fp = open(self.el, 'r')
self.tempFileData = self.fp.read()
self.fp.close()
self.text_data_1 = re.sub('\$SENDER_NAME_' , self.sender , self.tempFileData)
self.text_data = re.sub('\$RECEIVER_NAME_' , self.receiver , self.text_data_1)
self.text.insert(1.0, self.text_data.decode('cp949'))
#----------------------------------------------------------
# main
if __name__ == "__main__":
root = Tk()
app = App(root)
root.mainloop()
Quick Sort (0) | 2010.06.10 |
---|---|
[Python] 짧은 코드 모음 (0) | 2010.01.22 |
[Python] 초간단 Lotto 생성기 소스 (0) | 2010.01.22 |
[Python] Simple template maker (0) | 2010.02.02 |
---|---|
[Python] 초간단 Lotto 생성기 소스 (0) | 2010.01.22 |
[Python] Simple file viewer (0) | 2010.01.02 |
[Python] 짧은 코드 모음 (0) | 2010.01.22 |
---|---|
[Python] Simple file viewer (0) | 2010.01.02 |
[Python] 회전 이동 (0) | 2009.08.25 |
#-*- coding: cp949 -*-
# Name : py_fileview.py
# Author : chobocho.com
# Version :
# Copyright :
# Description : Simple text file viewer
#
from Tkinter import *
import tkMessageBox
import os.path
import glob
class App:
def __init__ (self, master):
frame = Frame(master)
frame.pack()
# Text Area
f0 = Frame(frame, width = 100, height = 100)
f0.grid(row = 0, column = 0)
self.text_scrollbar = Scrollbar (f0, orient=VERTICAL)
self.text = Text(f0, width=80, height = 20, yscrollcommand=self.text_scrollbar.set)
self.text_scrollbar.config(command=self.text.yview)
self.text_scrollbar.pack(side=RIGHT, fill=Y)
self.text.pack()
# List Box area
f1 = Frame(frame, width = 100, height = 100)
f1.grid(row = 0, column = 1)
self.list_button = Button(f1, text="File List", command=self.ShowInfo)
self.list_button.pack()
self.listbox_scrollbar = Scrollbar (f1, orient=VERTICAL)
self.listbox = Listbox(f1, height=19, yscrollcommand=self.listbox_scrollbar.set)
self.listbox_scrollbar.config(command=self.listbox.yview)
self.listbox_scrollbar.pack(side=RIGHT, fill=Y)
# 파일 내용을 읽어서 보여 줄 것
self.ReadTemplate()
self.listbox.bind("<Double-Button-1>", self.LoadFile)
self.listbox.pack(side=RIGHT)
def ClearText(self):
self.text.delete(1.0, END)
def ShowInfo(self):
tkMessageBox.showinfo("Information","http://chobocho.com\nVersion 0.2")
def ReadTemplate(self):
all_flist = glob.glob ("*.txt")
for f in all_flist:
if os.path.exists(f):
self.listbox.insert(END, f.decode('cp949'))
def LoadFile(self, event):
self.items = self.listbox.curselection()
self.el = self.listbox.get(self.items)
self.ClearText()
if os.path.exists(self.el):
self.fp = open(self.el, 'r')
self.tempFileData = self.fp.read()
self.fp.close()
self.text.insert(1.0, self.tempFileData.decode('cp949'))
#----------------------------------------------------------
# main
if __name__ == "__main__":
root = Tk()
app = App(root)
root.mainloop()
[Python] 초간단 Lotto 생성기 소스 (0) | 2010.01.22 |
---|---|
[Python] 회전 이동 (0) | 2009.08.25 |
WxPython - HelloWorld (0) | 2008.12.22 |