you need to give the input text files in the command line...

``````#! /usr/bin/python

import re
import sys
import math
import os

os.system('clear')
print "\$\$\$\$\$ THE DOCUMENT-DISTANCE PROBLEM \$\$\$\$\$"
print "\n\nTHE NAMES OF THE SCRIPT & TWO DOCUMENTS :",(str(sys.argv))

for i in range(1,len(sys.argv)):
for j in range(1,len(sys.argv)):
text_1 = open(sys.argv[i],'r')
text_2 = open(sys.argv[j],'r')

freqs_1 = {}
for word in words_1:
freqs_1[word] = freqs_1.get(word, 0) + 1

freqs_2 = {}
for word in words_2:
freqs_2[word] = freqs_2.get(word, 0) + 1

def square(x):
return x*x

def norm_function(a):
return math.sqrt(sum(map(square,a)))

norm_1 = norm_function(freqs_1.values())
norm_2 = norm_function(freqs_2.values())

diction = dict( (n, freqs_1.get(n, 0)*freqs_2.get(n, 0)) for n in set(freqs_1) & set(freqs_2) )
s = sum(diction.values())
x = round(s/(norm_1*norm_2), 5)
angle = math.acos(x)

print "THE NORM OF DOCUMENT",sys.argv[i],"is :", norm_1
print "THE NORM OF DOCUMENT",sys.argv[j],"is :", norm_2
print "THE ANGLE BETWEEN TWO DOCUMENTS :", angle
print "\n\$\$\$\$\$ THE-END \$\$\$\$\$\n"``````
``````#  binary search tree
#  with operations: INSERT,PREORDER,POSTORDER,INORDER,DELETE

import os
import sys

class Node:
def __init__(self, v):
self.left = None
self.right = None
self.value = v

# INSERT
def insert(root, node):
if root is None:
root = node
else:
if root.value > node.value:
if root.left == None:
root.left = node
else:
insert(root.left, node)
else:
if root.right == None:
root.right = node
else:
insert(root.right, node)

# DELETE
def delete(x,root):
if root is None:
print "NO SUCH ELEMENT FOUND"
elif x<root.value:
root.left = delete(x,root.left)
elif x>root.value:
root.right = delete(x,root.right)
elif root.left and root.right:
TempNode = findMin(root.right)
root.value = TempNode.value
root.right = delete(TempNode.value,root.right)
else:
if root.left is None:
root = root.right
elif root.right is None:
root = root.left
return root

# FIND MINIMUM
def min_element(root):
if root is not None:
if root.left is not None:
return min_element(root.left)
else:
return root.value

# FIND MAXIMUM
def max_element(root):
if root is not None:
if root.right is not None:
return max_element(root.right)
else:
return root.value

# PREORDER
def pre_order(root):
if not root:
return
print root.value,
in_order(root.left)
in_order(root.right)

# POSTORDER
def post_order(root):
if not root:
return
post_order(root.left)
post_order(root.right)
print root.value,

# INORDER
def in_order(root):
if not root:
return
in_order(root.left)
print root.value,
in_order(root.right)

# PRINTING THE TRAVERSES
os.system('clear')
i=0
r=None
while (i != 999):
i = int( input("ENTER VALUE (999 to stop)") )
if r is None:
r = Node(i)
else:
insert(r,Node(i))

print "\nTHANK YOU\n"

do = "y"
while do == "y":
os.system('clear')

print "\n\nTHE BINARY_SEARCH_TREE\n\nOPTIONS\n\n1 --> IN-ORDER TRAVERSAL\n2 --> PRE-ORDER TRAVERSAL\n3 --> POST-ORDER TRAVERSAL\n4 --> DELETE\n5 --> MAX ELEMENT\n6 --> MIN ELEMENT\n\n"
inp = int(input("ENTER YOUR CHOICE :"))

if inp == 1:
print "\n\nTHE INORDER TRAVERSE IS GIVEN BY:"
in_order(r)

if inp == 2:
print "\n\nTHE PRE-ORDER TRAVERSE IS GIVEN BY:"
pre_order(r)

if inp == 3:
print "\n\nTHE POST-ORDER TRAVERSE IS GIVEN BY:"
post_order(r)

if inp == 4:
d = int(input("ENTER THE NODE VALUE :"))
delete(d,r)

if inp == 5:
ele = max_element(r)
print ele

if inp == 6:
ele = min_element(r)
print ele

do = raw_input("\n\nDO U WANNA CONTINUE(y/n) :")

print "\n\nTHE_END\n\n"
``````

this is a python code for binary search tree....

commented: doing some model hacking for a game, where the format requires a BST, this could be useful. :) +2
``````#! /usr/bin/python

from numpy import random,argsort,sqrt
from pylab import plot,show

def nearest_search(x, D, K):
inp = D.shape[1]
K = K if K < inp else inp
dist = sqrt(((D - x[:,:inp])**2).sum(axis=0))                  # euclidean distances from the other points
index = argsort(dist)                              # sorting
return index[:K]                               # return the indexes of K nearest neighbours

num = int(input("HOW MANY NEAREST NEIGHBOURS YOU WANT :"))
input_data = random.rand(2,1000)                        # nearest_search test
x = random.rand(2,1)                                # query point
ngbh_index = nearest_search(x,input_data,num)                   # performing the search
plot(input_data[0,:],input_data[1,:],'ob',x[0,0],x[1,0],'or')           # plotting the input_data and the input point
plot(input_data[0,ngbh_index],input_data[1,ngbh_index],'o',
markerfacecolor='None',markersize=15,markeredgewidth=1)           # highlighting the neighbours
show()
``````

this is a code for k-nearest neighbour algorithm

``````#! /usr/bin/python
import heapq
import math as m

def quick(lst):
l1 = [x for x in lst[1:] if x <= lst[0]]
l2 = [y for y in lst if y > lst[0]]
if len(lst) == 0:
return lst
elif len(lst) == 1:
return lst
else:
return quick(l1) + [lst[0]] + quick(l2)

def heap(lst):
heap = []
for i in lst:
heapq.heappush(heap, i)
return [heapq.heappop(heap) for i in range(len(heap))]

def intro_sort(lst,first,last,depth):
if len(lst) == 0:
return lst
elif len(lst) == 1:
return lst
elif last-first > 0:
if depth == 0:
return heap(lst)
else:
return quick(lst)

inp = []
inp = list(input("ENTER THE INPUT :"))
print intro_sort( inp,0,len(inp)-1,(int(2*m.log(len(inp)))) )
``````

this is a code for introspective sort in python...

If you find these helpfull am happy...!!!!

If you would use the 4 space indent, you would have more chance for people to like them. Also code snippet belongs to code snippet, not question.

@pyTony (and others) btw, how to copy source code examples without indent being destroyed?
@this website title-attributes for buttons (resulting in hover-explanation) help newbies like me.

If you have a PC, take the mouse and double click in the code. This will highlight the code. Now right click on the highlighted code and use copy from the pop up menu. If you have a touch pad like the iPad, you are plain out of luck!

Thanks, Sneekula. Yesterday sourcecode was numbered which resulted in tabs being removed from code when copypasting. Looks like they already fixed that (reported to an admin). Now it works.

Forget that, that was my proxy. It is the Javascript, which adds line numbers to the source code. It messes up copypaste.