ÉæË®The Python Challenge
ÔÚStack Overflow ÉÏ¿´µ½Ñ§Ï°Python µÄÒ»¸ö·½·¨ÊÇÓÃPython ÆÆ½âThe Python Challenge¡£µ«ÎÒϲ»¶ÓÃRuby£¬Ë¹ÜµÃ×ÅÄØ^_^
0. ÈëÃŹغܼòµ¥¡£
p 2**38
1. ÆÆ½âÒ»¶Î»°£¬¹Û²ìͼƬºÜÈÝÒ×·¢ÏÖ½âÂë±í°Ñ×Öĸ±íÑ»·ÓÒÒÆÁ½Î»¡£
riddle = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
riddle.scan(/./).each do |char|
if /[a-z]/ =~ char
print (?a + (char[0] - ?a + 2) % 26).chr
else
print char
end
end
ÒëÎÄ£ºi hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.
ÔÙ¶Ôurl ("map")ʵʩ±ä»»µÃ"ocr"¡£
update: ·¢ÏÖStringÓиöÄÚÖÃÌæ»»º¯Êýtr()¡£¿´£¬¶à¼ò½à¡£
class String
def rot2
self.tr("a-xyz","c-zab")
end
end
p riddle.rot2
2. ²é¿´ÍøÒ³Ô´Â룬¿ÉÒÔ¿´µ½ÍøÒ³×¢ÊÍÖÐÓÐÒ»¶ÑÂÒÂ룬ÉÏÃæÓо仰"find rare characters in the mess below:"(“ÕÒ³öÏ¡ÉÙµÄ×Ö·û”)¡£Ïëµ½ÓÃhash À´Í³¼Æ¸÷¸ö×Ö·ûµÄ³öÏÖ´ÎÊý£¬²¢¼Ç¼Ê״γöÏÖµÄ˳Ðò¡£
riddle = '...' # the mess here
count = Hash.new(0)
seq = []
riddle.scan(/./).each do |char|
seq << char if count[char] == 0
count[char] += 1
end
p count, seq
Äã»áµÃµ½"equality"¡£µÚÈý¹Ø£¬ÎÒÀ´ÁË£¡
Ïà¹ØÎĵµ£º
>>> import string
>>> s='adbecf'
>>>
tt=string.maketrans("abc","ABC")
>>> s.translate(tt,"")
'AdBeCf'
>>>
s.translate(tt,"")
ºóÃæµÄÄǸö¿Õ×Ö·û´´¾ÍÊÇÄãҪɾ³ýµÄ×Ö·û£¬±ÈÈçҪɾ³ý»»ÐоÍÊÇs.translate(tt,"\n&q ......
c´úÂë:
int x = a>b?6,6;
ÕâÖÖÓï¾äPythonÖÐÒ²ÓУº
¿ÉÒÔͨ¹ýand or×éºÏÀ´ÊµÏÖ¡£¸öÈËÈÏΪÕâÖÖÓï¾ä¾¡Á¿ÉÙµãºÃ¡£
python°ÑÕâÀàÐ͵ŦÄÜ·¢Ñï¹âÁË´ó¡£µ«ÊÇÓÉÓÚÕâÖÖ×éºÏ±È½ÏÈÆ£¬ËùÒÔÓÐʱºò±È½Ï¾À½á:
±ÈÈç:
5 or 6 and 6 ·µ»Øwhat?
0 or 5 ·µ»Øwhat?
8 and 7 ·µ»Øwhat?
ÓÚÊÇÕûÀíÁËÏ£º
1.pyhon ÖÐÓ ......
urlÅäÖÃ
ÎÒÃÇÔÚpollsÕâ¸öappÏ´´½¨Ò»¸ö
helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
ÐÞ¸Ä urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib ......
ÓõķֱðÊÇtimeºÍdatetimeº¯Êý
'''
Created on 2009-9-2
@author: jiangqh
'''
import time,datetime
# date to str
print time.strftime("%Y-%m-%d %X", time.localtime())
#str to date
t = time.strptime("2009 - 08 - 08", "%Y - %m - %d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)
Êä³öµ±Ç°Ê±¼ä ......