My talk on Python as a learning language got selected. Excited to meet many pythonistas there at the conference. :)
Link to my proposal - Link
Link to my proposal - Link
Mostly About Programming
import scipy.ndimage as ndi import scipy import numpy import Image import math
sigma = 1.4 f = 'lena_std.jpg.jpg' img = Image.open(f).convert('L') #grayscale imgdata = numpy.array(img, dtype = float) G = ndi.filters.gaussian_filter(imgdata, sigma)
sobelout = Image.new('L', img.size) #empty image gradx = numpy.array(sobelout, dtype = float) grady = numpy.array(sobelout, dtype = float) sobel_x = [[-1,0,1], [-2,0,2], [-1,0,1]] sobel_y = [[-1,-2,-1], [0,0,0], [1,2,1]] width = img.size[1] height = img.size[0] for x in range(1, width-1): for y in range(1, height-1): px = (sobel_x[0][0] * G[x-1][y-1]) + (sobel_x[0][1] * G[x][y-1]) + \ (sobel_x[0][2] * G[x+1][y-1]) + (sobel_x[1][0] * G[x-1][y]) + \ (sobel_x[1][1] * G[x][y]) + (sobel_x[1][2] * G[x+1][y]) + \ (sobel_x[2][0] * G[x-1][y+1]) + (sobel_x[2][1] * G[x][y+1]) + \ (sobel_x[2][2] * G[x+1][y+1]) py = (sobel_y[0][0] * G[x-1][y-1]) + (sobel_y[0][1] * G[x][y-1]) + \ (sobel_y[0][2] * G[x+1][y-1]) + (sobel_y[1][0] * G[x-1][y]) + \ (sobel_y[1][1] * G[x][y]) + (sobel_y[1][2] * G[x+1][y]) + \ (sobel_y[2][0] * G[x-1][y+1]) + (sobel_y[2][1] * G[x][y+1]) + \ (sobel_y[2][2] * G[x+1][y+1]) gradx[x][y] = px grady[x][y] = py sobeloutmag = scipy.hypot(gradx, grady) sobeloutdir = scipy.arctan2(grady, gradx)
for x in range(width): for y in range(height): if (sobeloutdir[x][y]<22.5 and sobeloutdir[x][y]>=0) or \ (sobeloutdir[x][y]>=157.5 and sobeloutdir[x][y]<202.5) or \ (sobeloutdir[x][y]>=337.5 and sobeloutdir[x][y]<=360): sobeloutdir[x][y]=0 elif (sobeloutdir[x][y]>=22.5 and sobeloutdir[x][y]<67.5) or \ (sobeloutdir[x][y]>=202.5 and sobeloutdir[x][y]<247.5): sobeloutdir[x][y]=45 elif (sobeloutdir[x][y]>=67.5 and sobeloutdir[x][y]<112.5)or \ (sobeloutdir[x][y]>=247.5 and sobeloutdir[x][y]<292.5): sobeloutdir[x][y]=90 else: sobeloutdir[x][y]=135STEP: Non Maximum Suppression
for x in range(1, width-1): for y in range(1, height-1): if sobeloutdir[x][y]==0: if (sobeloutmag[x][y]<=sobeloutmag[x][y+1]) or \ (sobeloutmag[x][y]<=sobeloutmag[x][y-1]): mag_sup[x][y]=0 elif sobeloutdir[x][y]==45: if (sobeloutmag[x][y]<=sobeloutmag[x-1][y+1]) or \ (sobeloutmag[x][y]<=sobeloutmag[x+1][y-1]): mag_sup[x][y]=0 elif sobeloutdir[x][y]==90: if (sobeloutmag[x][y]<=sobeloutmag[x+1][y]) or \ (sobeloutmag[x][y]<=sobeloutmag[x-1][y]): mag_sup[x][y]=0 else: if (sobeloutmag[x][y]<=sobeloutmag[x+1][y+1]) or \ (sobeloutmag[x][y]<=sobeloutmag[x-1][y-1]): mag_sup[x][y]=0
m = numpy.max(mag_sup) th = 0.2*m tl = 0.1*m gnh = numpy.zeros((width, height)) gnl = numpy.zeros((width, height)) for x in range(width): for y in range(height): if mag_sup[x][y]>=th: gnh[x][y]=mag_sup[x][y] if mag_sup[x][y]>=tl: gnl[x][y]=mag_sup[x][y] gnl = gnl-gnh def traverse(i, j): x = [-1, 0, 1, -1, 1, -1, 0, 1] y = [-1, -1, -1, 0, 0, 1, 1, 1] for k in range(8): if gnh[i+x[k]][j+y[k]]==0 and gnl[i+x[k]][j+y[k]]!=0: gnh[i+x[k]][j+y[k]]=1 traverse(i+x[k], j+y[k]) for i in range(1, width-1): for j in range(1, height-1): if gnh[i][j]: gnh[i][j]=1 traverse(i, j)The output I get after this is:
len(nums & newset)
len(nums.intersection(newset))
import sys if __name__ == '__main__': f = sys.stdin if len(sys.argv) >= 2: fn = sys.argv[1] if fn != '-': f = open(fn) output = open('tongout.out', 'w') t = int(f.readline()) for test in xrange(1, t+1): str1 = "Case #%d: " %(test) output.write(str1) string = f.readline().strip() ans = [] for i in string: if i == 'a': ans.append('y') elif i == 'b': ans.append('h') elif i == 'c': ans.append('e') elif i == 'd': ans.append('s') elif i == 'e': ans.append('o') elif i == 'f': ans.append('c') elif i == 'g': ans.append('v') elif i == 'h': ans.append('x') elif i == 'i': ans.append('d') elif i == 'j': ans.append('u') elif i == 'k': ans.append('i') elif i == 'l': ans.append('g') elif i == 'm': ans.append('l') elif i == 'n': ans.append('b') elif i == 'o': ans.append('k') elif i == 'p': ans.append('r') elif i == 'q': ans.append('z') elif i == 'r': ans.append('t') elif i == 's': ans.append('n') elif i == 't': ans.append('w') elif i == 'u': ans.append('j') elif i == 'v': ans.append('p') elif i == 'w': ans.append('f') elif i == 'x': ans.append('m') elif i == 'y': ans.append('a') elif i == 'z': ans.append('q') elif i == ' ': ans.append(' ') out = ''.join(ans) output.write(out+"\n") output.close()Dancing With the Googlers:
import sys if __name__ == '__main__': f = sys.stdin if len(sys.argv) >= 2: fn = sys.argv[1] if fn != '-': f = open(fn) output = open('danout.out', 'w') t = int(f.readline()) for test in xrange(1, t+1): c = 0 str1 = "Case #%d: " %(test) output.write(str1) arr = map(int, f.readline().strip().split()) n = arr[0] s = arr[1] p = arr[2] goog = arr[3:] goog.sort() fl=0 y = 0 for k in goog: temp = k-p temp/=2 if fl==1 and k>=y: c+=1 elif temp >= p-1 and temp>=0: c+=1 fl=1 y=k elif temp >= p-2 and s>=1 and temp>=0: c+=1 s-=1 output.write(str(c)+"\n") output.close()Recycled Numbers:
import sys if __name__ == '__main__': f = sys.stdin if len(sys.argv) >= 2: fn = sys.argv[1] if fn != '-': f = open(fn) output = open('recout1.out', 'w') t = int(f.readline()) for test in xrange(1, t+1): str1 = "Case #%d: " %(test) output.write(str1) a, b = f.readline().strip().split() inta = int(a) intb = int(b) values = [str(i) for i in range(int(a), int(b)+1)] c = 0 for i in values: arr = [] for j in range(len(i), 0, -1): ans = i[j:]+i[:j] ans = int(ans) if ans > int(i) and ans >= inta and ans <= intb and ans not in arr: arr.append(ans) c+=1 output.write(str(c)+"\n") output.close()