test.py

   1 def store():
   2     n=raw_input()
   3     sum1=0
   4     for i in range(len(n)):
   5         sum1=sum1 + int(n[i])
   6     t=sum1%3
   7     if(t==0):
   8         print 2
   9     else:
  10         if(t==2):
  11            print 1
  12            print 2
  13         else:
  14            print 1
  15            print 1
  16 store()

test2.py

   1 def queens (row, col):
   2     def safe (ran, rst):
   3         def check (pos):
   4             return ran != rst[pos] and abs(ran - rst[pos]) != pos + 1
   5         return all([check(pos) for pos in range(len(rst))])
   6     return [[]] if col == 0 else [[ran] + rst for ran in range(row) for rst in queens(row, col - 1) if safe(ran, rst)]

wordscounter.py

   1 words = {}
   2 try:
   3     while True:
   4         s = raw_input()
   5         if not s:
   6             continue
   7         if s in words:
   8             words[s]+=1
   9         else:
  10             words[s]=1
  11 except EOFError:
  12     for i in sorted(words.keys()):
  13         print "%s %d" % (i, words[i])

test.py

   1 class Person:
   2     def __getitem__(self, key):
   3         return key
   4 
   5 
   6 p = Person()
   7 s = Person()
   8 
   9 Person.name = "Unknown"
  10 p.name = "Jack"
  11 p.gender = "Male"
  12 p.age = 30
  13 s.name = "Rose"
  14 s.gender = "Female"
  15 
  16 print Person
  17 print p 
  18 print s
  19 print Person.name
  20 print p.name
  21 print p.gender
  22 print p.age
  23 print s.name
  24 print s.gender
  25 
  26 print s[0]

multiply99.py

   1 def multi99(n):
   2     for i in range(1,n+1):
   3         s = ''
   4         for j in range(1,i+1):
   5             s += '\t%d'% (i*j)
   6         print s
   7 
   8 multi99(20)

datediff.py

   1 import datetime
   2 a = datetime.date(*[int(i) for i in raw_input().split()])
   3 b = datetime.date(*[int(i) for i in raw_input().split()])
   4 print (a - b).days()

accept.py

   1 import socket
   2 import threading
   3 import select
   4 allsocks = []
   5 
   6 def acc():
   7         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   8         sock.bind("", 2555)
   9         sock.listen(5)
  10         while 1:
  11                 newsock, remote = sock.accept()
  12                 allsocks.append(newsock)
  13 
  14 
  15 newthread = threading.Thread(target = acc)
  16 newthread.start()
  17 
  18 while 1:
  19         (rdsocks,,) = select.select(allsocks, [],[], 0.1)
  20         for s in rdsocks:
  21                 msg = s.recv(100)

autosubmit.py

   1 import glob
   2 import urllib
   3 import re
   4 reobject = re.compile('(?P<hello>[0-9]*)\.(?P<ext>.*)')
   5 for filename in glob.glob('[0-9]*.*'):
   6     mo = reobject.match(filename)
   7     if mo:
   8         prob, ext = mo.groups()
   9     else:
  10         continue
  11     if ext.lower() in ['c']:
  12         language = 0
  13     elif ext.lower() in ['cc', 'cpp']:
  14         language = 1
  15     else:
  16         continue
  17     params = urllib.urlencode({'language':language, 'user_id':'wzuacm', 
  18         'passwd':'wzuacm', 'prob_id':prob, 'source':open(filename).read() })
  19     print params
  20     request = urllib.urlopen("http://acm.zju.edu.cn/submit_process.php", params)
  21     request.read()
  22     request.close()

   1 #!/usr/bin/python
   2 
   3 def max(*args):
   4     """print the maximum element of args
   5     """
   6     m = args[0]
   7     for i in args:
   8         if i > m:
   9             m = i
  10     print m

Python代码片段 (2020-03-31 16:55:59由czk编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.