http://www.pythonchallenge.com/pc/def/equality.html One small letter, surrounded by EXACTLY three big bodyguards on each of its sides. ''' import os, re reg = re.compile('[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]') reg = re.compile('[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]') latters = '' with open (os.path.join(os.getcwd(), '0003re.html'), 'r') as html: for line in html: a = reg.findall(line) if a: for l in a: latters += l[4] #print(a) print(latters) from tools import writeData writeData(latters) #import urllib.request as ur, re #reg = re.compile('[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]') #url = 'http://www.pythonchallenge.com/pc/def/equality.html' #data = ur.urlopen(url).read().decode('utf-8') #a = reg.findall(data) #latters = '' #for i in a: #latters += i[4] ##print(a) #print(latters)
return data, errors if __name__ == "__main__": parser = OptionParser() parser.add_option("-l", "--lfile", dest="learnFile", help="Learning data (CSV file name)") parser.add_option("-t", "--tfile", dest="testFile", help="Testing data (CSV file name)") parser.add_option("-o", "--ofile", dest="outFile", help="Output file name to store testing data classification") (options, args) = parser.parse_args() learnFile = options.learnFile # e. g. vertebral_learn.csv testFile = options.testFile # e. g. vertebral_test.csv outFile = options.outFile # e. g. res learnData = readData(learnFile) c45= C45(learnData) tree = c45.constructTree() tree = c45.pruneTree(tree) printTree(tree) testData = readData(testFile) classifiedLearnData, learnErrors = classifyData(tree, learnData) classifiedTestData, testErrors = classifyData(tree, testData) writeData(classifiedTestData, outFile) print "Learning data error: %d/%d (%f)" % (learnErrors, len(learnData), float(learnErrors)/len(learnData)) print "Testing data error: %d/%d (%f)" % (testErrors, len(testData), float(testErrors)/len(testData))
self.result = {} #self.find = 'yutileaq' self.alph = '' def handle_comment(self, data): # len = 41:find rare characters in the mess below: if len(data) != 41: self.result = {} for i in data: if i in self.result: self.result[i] += 1 else: self.result[i] = 1 self.alph += i return self.result, self.alph parser = exampleHTMLParser() with open (os.path.join(os.getcwd(), '0002ocr.html'), 'r') as html: parser.feed(html.read()) result, alph = parser.result, parser.alph print(result) print(alph) name = [] for i in alph: if result[i] == 1: print(i, end = '') name.append(i) from tools import writeData writeData(''.join(name))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' http://www.pythonchallenge.com/pc/ring/bell.html repeat switch ''' from PIL import Image im = Image.open("0028bell.png") #print(im) r, g, b = im.split() #r.show() #g.show() #ring ring ring #b.show() data = list(g.getdata()) #print(data) outstand = [abs(data[i] - data[i + 1]) for i in range(0, len(data), 2) if abs(data[i] - data[i + 1]) != 42] # print(outstand) s = [] for i in outstand: s.append(chr(i)) #print(''.join(s)) import tools tools.writeData(''.join(s))
dest="testFile", help="Testing data (CSV file name)") parser.add_option( "-o", "--ofile", dest="outFile", help="Output file name to store testing data classification") (options, args) = parser.parse_args() learnFile = options.learnFile # e. g. vertebral_learn.csv testFile = options.testFile # e. g. vertebral_test.csv outFile = options.outFile # e. g. res learnData = readData(learnFile) c45 = C45(learnData) tree = c45.constructTree() tree = c45.pruneTree(tree) printTree(tree) testData = readData(testFile) classifiedLearnData, learnErrors = classifyData(tree, learnData) classifiedTestData, testErrors = classifyData(tree, testData) writeData(classifiedTestData, outFile) print "Learning data error: %d/%d (%f)" % ( learnErrors, len(learnData), float(learnErrors) / len(learnData)) print "Testing data error: %d/%d (%f)" % ( testErrors, len(testData), float(testErrors) / len(testData))