Skip to content

Instantly share code, notes, and snippets.

@lufeihaidao
lufeihaidao / python_challenge_q1_1.py
Created April 9, 2014 16:01
Q1 of python challenge in python
# 这是我的 python 解法,相当的 C 呢
astring='''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.'''
import re
bstring=''
for a in astring:
if re.match('\w',a):
a=chr((ord(a)-95)%26+97)
bstring=bstring+a
@lufeihaidao
lufeihaidao / python_challenge_q1_2.py
Created April 9, 2014 16:14
Q1 of python challenge in python
# 推荐的做法
import string
text='''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.'''
table = string.maketrans(string.ascii_lowercase,
string.ascii_lowercase[2:]+string.ascii_lowercase[:2])
text.translate(table)
@lufeihaidao
lufeihaidao / python_challenge_q4.sh
Created April 13, 2014 15:19
Q4 of python challenge
ruby -wle 'n = 12345; print n = $1 while
`GET "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=#{n}"`
=~ /(\d+)$/'
@lufeihaidao
lufeihaidao / python_challenge_q6.rb
Created April 13, 2014 15:28
Q6 of python challenge
require "zip/zip"
zipfilename = "/home/haidao/Downloads/channel.zip"
Zip::ZipFile.open(zipfilename) do |zipfile|
s = zipfile.get_input_stream("readme.txt").read
num = s.match(/\d{2,6}/)
output = Array.new
while num do
entry = zipfile.find_entry("#{num}.txt")
s = entry.get_input_stream.read
num = s.match(/\d{2,6}/)
@lufeihaidao
lufeihaidao / python_challenge_q6.py
Created April 13, 2014 15:27
Q6 of python challenge
f = "channel.zip"
z = zipfile.ZipFile(f)
name = '90052.txt'
while 1:
print z.getinfo(name).comment,
name = z.read(name).split()[-1] + '.txt'
@lufeihaidao
lufeihaidao / python_challenge_q5.py
Created April 13, 2014 15:25
Q5 of python challenge
# 推荐的做法
import urllib2, sys, pickle
pickle_data = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/banner.p').read()
data = pickle.loads(pickle_data)
for line in data:
for char, count in line: sys.stdout.write(char * count)
sys.stdout.write("\n")
@lufeihaidao
lufeihaidao / python_challenge_q4.rb
Created April 13, 2014 15:16
Q4 of python challenge
require "open-uri"
n=12345
loop do
flag = ''
open("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=#{n}") do |f|
s=f.read
flag = s if s=~/htm/
n=(s=~/div/ ? n/2 : s.split[-1].to_i)
end
if !flag.empty?
@lufeihaidao
lufeihaidao / python_challenge_q4.py
Created April 13, 2014 15:15
Q4 of python challenge
import re
from urllib import urlopen
astring='http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
temp='12345'
f = urlopen(astring+temp)
a=f.read()
restring=re.compile('and the next nothing is (\d{3,5})')
for i in range(400):
temp=re.findall(restring,a)[0]
print(temp)
@lufeihaidao
lufeihaidao / python_challenge_q3.py
Last active August 29, 2015 13:59
Q3 of python challenge
# 我的代码比较笨,但是方法是一样的,就不贴上来了。
import re
s = '''
copy from the source page
'''
print ''.join(re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', s))
@lufeihaidao
lufeihaidao / python_challenge_q1.rb
Created April 13, 2014 14:58
Q1 of python challenge in ruby
gets.tr "a-z", "c-zab"
# 就这么简单!然后输入字符串就好了!真让人喜欢!