'Python'에 해당되는 글 49건

  1. 2010.07.10 Self number (1)
  2. 2010.07.08 100!
  3. 2010.06.10 Quick Sort
  4. 2010.02.02 [Python] Simple template maker
  5. 2010.01.22 [Python] 짧은 코드 모음
  6. 2010.01.22 [Python] 초간단 Lotto 생성기 소스
  7. 2010.01.02 [Python] Simple file viewer
  8. 2009.08.25 [Python] 회전 이동
  9. 2007.03.22 폴더내의 파일 이름 바꾸기
Coding/Python 삽질기2010. 7. 10. 03:52


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)

 

Count : 494
Sum : 1227365


 

'Coding > Python 삽질기' 카테고리의 다른 글

Self number ... 2  (0) 2010.07.13
Self number  (1) 2010.07.10
몬테카를로 법을 이용한 원주율 계산  (0) 2010.07.09
100!  (0) 2010.07.08
Posted by chobocho

댓글을 달아 주세요

  1. 문제는 아마 functional.or.kr에서 퍼 오는것 같은데, 코드는 어째 꽤 절차지향적인 냄새가 남아있네요?

    2010.07.13 09:42 [ ADDR : EDIT/ DEL : REPLY ]

Coding/Python 삽질기2010. 7. 8. 00:01

#-*- 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))



1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
21! = 51090942171709440000
22! = 1124000727777607680000
23! = 25852016738884976640000
24! = 620448401733239439360000
25! = 15511210043330985984000000
26! = 403291461126605635584000000
27! = 10888869450418352160768000000
28! = 304888344611713860501504000000
29! = 8841761993739701954543616000000
30! = 265252859812191058636308480000000
31! = 8222838654177922817725562880000000
32! = 263130836933693530167218012160000000
33! = 8683317618811886495518194401280000000
34! = 295232799039604140847618609643520000000
35! = 10333147966386144929666651337523200000000
36! = 371993326789901217467999448150835200000000
37! = 13763753091226345046315979581580902400000000
38! = 523022617466601111760007224100074291200000000
39! = 20397882081197443358640281739902897356800000000
40! = 815915283247897734345611269596115894272000000000
41! = 33452526613163807108170062053440751665152000000000
42! = 1405006117752879898543142606244511569936384000000000
43! = 60415263063373835637355132068513997507264512000000000
44! = 2658271574788448768043625811014615890319638528000000000
45! = 119622220865480194561963161495657715064383733760000000000
46! = 5502622159812088949850305428800254892961651752960000000000
47! = 258623241511168180642964355153611979969197632389120000000000
48! = 12413915592536072670862289047373375038521486354677760000000000
49! = 608281864034267560872252163321295376887552831379210240000000000
50! = 30414093201713378043612608166064768844377641568960512000000000000
51! = 1551118753287382280224243016469303211063259720016986112000000000000
52! = 80658175170943878571660636856403766975289505440883277824000000000000
53! = 4274883284060025564298013753389399649690343788366813724672000000000000
54! = 230843697339241380472092742683027581083278564571807941132288000000000000
55! = 12696403353658275925965100847566516959580321051449436762275840000000000000
56! = 710998587804863451854045647463724949736497978881168458687447040000000000000
57! = 40526919504877216755680601905432322134980384796226602145184481280000000000000
58! = 2350561331282878571829474910515074683828862318181142924420699914240000000000000
59! = 138683118545689835737939019720389406345902876772687432540821294940160000000000000
60! = 8320987112741390144276341183223364380754172606361245952449277696409600000000000000
61! = 507580213877224798800856812176625227226004528988036003099405939480985600000000000000
62! = 31469973260387937525653122354950764088012280797258232192163168247821107200000000000000
63! = 1982608315404440064116146708361898137544773690227268628106279599612729753600000000000000
64! = 126886932185884164103433389335161480802865516174545192198801894375214704230400000000000000
65! = 8247650592082470666723170306785496252186258551345437492922123134388955774976000000000000000
66! = 544344939077443064003729240247842752644293064388798874532860126869671081148416000000000000000
67! = 36471110918188685288249859096605464427167635314049524593701628500267962436943872000000000000000
68! = 2480035542436830599600990418569171581047399201355367672371710738018221445712183296000000000000000
69! = 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
70! = 11978571669969891796072783721689098736458938142546425857555362864628009582789845319680000000000000000
71! = 850478588567862317521167644239926010288584608120796235886430763388588680378079017697280000000000000000
72! = 61234458376886086861524070385274672740778091784697328983823014963978384987221689274204160000000000000000
73! = 4470115461512684340891257138125051110076800700282905015819080092370422104067183317016903680000000000000000
74! = 330788544151938641225953028221253782145683251820934971170611926835411235700971565459250872320000000000000000
75! = 24809140811395398091946477116594033660926243886570122837795894512655842677572867409443815424000000000000000000
76! = 1885494701666050254987932260861146558230394535379329335672487982961844043495537923117729972224000000000000000000
77! = 145183092028285869634070784086308284983740379224208358846781574688061991349156420080065207861248000000000000000000
78! = 11324281178206297831457521158732046228731749579488251990048962825668835325234200766245086213177344000000000000000000
79! = 894618213078297528685144171539831652069808216779571907213868063227837990693501860533361810841010176000000000000000000
80! = 71569457046263802294811533723186532165584657342365752577109445058227039255480148842668944867280814080000000000000000000
81! = 5797126020747367985879734231578109105412357244731625958745865049716390179693892056256184534249745940480000000000000000000
82! = 475364333701284174842138206989404946643813294067993328617160934076743994734899148613007131808479167119360000000000000000000
83! = 39455239697206586511897471180120610571436503407643446275224357528369751562996629334879591940103770870906880000000000000000000
84! = 3314240134565353266999387579130131288000666286242049487118846032383059131291716864129885722968716753156177920000000000000000000
85! = 281710411438055027694947944226061159480056634330574206405101912752560026159795933451040286452340924018275123200000000000000000000
86! = 24227095383672732381765523203441259715284870552429381750838764496720162249742450276789464634901319465571660595200000000000000000000
87! = 2107757298379527717213600518699389595229783738061356212322972511214654115727593174080683423236414793504734471782400000000000000000000
88! = 185482642257398439114796845645546284380220968949399346684421580986889562184028199319100141244804501828416633516851200000000000000000000
89! = 16507955160908461081216919262453619309839666236496541854913520707833171034378509739399912570787600662729080382999756800000000000000000000
90! = 1485715964481761497309522733620825737885569961284688766942216863704985393094065876545992131370884059645617234469978112000000000000000000000
91! = 135200152767840296255166568759495142147586866476906677791741734597153670771559994765685283954750449427751168336768008192000000000000000000000
92! = 12438414054641307255475324325873553077577991715875414356840239582938137710983519518443046123837041347353107486982656753664000000000000000000000
93! = 1156772507081641574759205162306240436214753229576413535186142281213246807121467315215203289516844845303838996289387078090752000000000000000000000
94! = 108736615665674308027365285256786601004186803580182872307497374434045199869417927630229109214583415458560865651202385340530688000000000000000000000
95! = 10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000
96! = 991677934870949689209571401541893801158183648651267795444376054838492222809091499987689476037000748982075094738965754305639874560000000000000000000000
97! = 96192759682482119853328425949563698712343813919172976158104477319333745612481875498805879175589072651261284189679678167647067832320000000000000000000000
98! = 9426890448883247745626185743057242473809693764078951663494238777294707070023223798882976159207729119823605850588608460429412647567360000000000000000000000
99! = 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

'Coding > Python 삽질기' 카테고리의 다른 글

몬테카를로 법을 이용한 원주율 계산  (0) 2010.07.09
100!  (0) 2010.07.08
Quick Sort  (0) 2010.06.10
[Python] Simple template maker  (0) 2010.02.02
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2010. 6. 10. 22:46


 Haskell로 Quick Sort를 만들면 2줄로 간단히 나온다.

quicksort []       =  []
quicksort (x:xs)  = quicksort[y | y <- xs, y < x] ++ [x] ++ quicksort[y | y <- xs, y >= x]


Python 도 유사하게 구현이 가능하다.
 

import random

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])


* Test code

#----------------------------------------------------------
# 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;  


* 실행결과
[1, 4, 2, 7, 9, 8, 3, 6, 5, 5]
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9]

'Coding > Python 삽질기' 카테고리의 다른 글

100!  (0) 2010.07.08
Quick Sort  (0) 2010.06.10
[Python] Simple template maker  (0) 2010.02.02
[Python] 짧은 코드 모음  (0) 2010.01.22
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2010. 2. 2. 01:23

#-*- 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()


'Coding > Python 삽질기' 카테고리의 다른 글

Quick Sort  (0) 2010.06.10
[Python] Simple template maker  (0) 2010.02.02
[Python] 짧은 코드 모음  (0) 2010.01.22
[Python] 초간단 Lotto 생성기 소스  (0) 2010.01.22
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2010. 1. 22. 01:10
# 리스트에서 음수 제거 하기
def remove_neg(num_list):
    num_list[:] = filter(lambda x: x > 0, num_list)
#-----------------------------------------------------------------------------------

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] Simple template maker  (0) 2010.02.02
[Python] 짧은 코드 모음  (0) 2010.01.22
[Python] 초간단 Lotto 생성기 소스  (0) 2010.01.22
[Python] Simple file viewer  (0) 2010.01.02
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2010. 1. 22. 00:41
import random

lotto_set = ()
while ( len(lotto_set) < 6 ):
    lotto_set = set([random.randrange(1, 47, 1) for k in range(6)])
    
lotto = list(lotto_set)
lotto.sort()

print lotto

[2, 26, 33, 36, 39, 46]

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] 짧은 코드 모음  (0) 2010.01.22
[Python] 초간단 Lotto 생성기 소스  (0) 2010.01.22
[Python] Simple file viewer  (0) 2010.01.02
[Python] 회전 이동  (0) 2009.08.25
Posted by chobocho
TAG Lotto, Python

댓글을 달아 주세요

Coding/Python 삽질기2010. 1. 2. 00:47

#-*- 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()



실행화면

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] 초간단 Lotto 생성기 소스  (0) 2010.01.22
[Python] Simple file viewer  (0) 2010.01.02
[Python] 회전 이동  (0) 2009.08.25
WxPython - HelloWorld  (0) 2008.12.22
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2009. 8. 25. 21:52

# 원점에서 100만큼 떨어진 점 (0, 100)을 원점을 중심으로 15도씩 회전 하였을 때의 좌표
# 리스트를 계산하는 코드

import math

point_xy = []
 
for idx in range (0, 24) :
      end_y = -100;     
      degree = 15.0 * idx
      rad = math.pi * degree / 180.0
      new_end_x = int(-end_y * math.sin(rad))
      new_end_y = int(end_y * math.cos(rad))
   
      point_xy.append((new_end_x, new_end_y))
     
print point_xy


[(0, -100), (25, -96), (49, -86), (70, -70), (86, -50), (96, -25), (100, 0), (96, 25), (86, 49), (70, 70), (49, 86), (25, 96), (0, 100), (-25, 96), (-50, 86), (-70, 70), (-86, 50), (-96, 25), (-100, 0), (-96, -25), (-86, -50), (-70, -70), (-50, -86), (-25, -96)]

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] Simple file viewer  (0) 2010.01.02
[Python] 회전 이동  (0) 2009.08.25
WxPython - HelloWorld  (0) 2008.12.22
Sudoku를 풀어주는 스크립트  (0) 2008.10.09
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2007. 3. 22. 02:10
#-*- coding: cp949 -*-
# filename : rename.py

import os
import glob
import stat
import sys

if __name__ == "__main__":

 all_flist = glob.glob ("*.py")
 
 for file in all_flist:
  if os.path.isdir(file):
   pass
 
  command = "ren %s %s_.py" % (file,  file[:-3])
  os.system(command)
  print file

'Coding > Python 삽질기' 카테고리의 다른 글

피보나치 수열 문제  (0) 2007.09.16
폴더내의 파일 이름 바꾸기  (0) 2007.03.22
스도쿠 제작  (0) 2006.10.22
helloworld.cgi  (0) 2006.10.15
Posted by chobocho
TAG Python, rename

댓글을 달아 주세요