Skip to content

Instantly share code, notes, and snippets.

@enigmaticape
Created December 7, 2012 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enigmaticape/4236500 to your computer and use it in GitHub Desktop.
Save enigmaticape/4236500 to your computer and use it in GitHub Desktop.
Really awful code to count ngrams
#!/usr/bin/env python
import collections
import sys
nums = sys.argv[1:]
def factors_of( x ):
factors = []
n = x-1
while n > 1:
if x % n == 0:
factors.append( n )
n = n - 1
return factors
counts = collections.Counter()
for x in nums:
factors = factors_of( int(x) )
print x, factors
counts += collections.Counter( factors )
print "Common Factor : ",\
sorted([n for n in counts if counts[n] == len(nums)])
print "Most Common :",\
[count for count in sorted( counts.items() ) if count[1] > 1]
#!/usr/bin/env python
import sys
#cipher = "hello there here is text jk kj"
cipher_in = open( sys.argv[ 1 ], 'rb')
cipher = cipher_in.read()
# remove all spaces
cipher_text = "".join( cipher.split() )
matches = []
reverses = []
for n in range(2, (len(cipher_text) / 2) - 1 ):
p_found = []
for p in range( 0, len( cipher_text ) ):
# get the digram
test_digram = cipher_text[ p:p+n ]
# get the ranges to the left and right that
# don't impinge on the current n-gram
for cand_range in [ range(0, p-n), range(p+n, len(cipher_text) ) ]:
for cand_p in [c for c in cand_range if c not in p_found]:
candidate_digram = cipher_text[ cand_p:cand_p + n ]
# print test_digram, candidate_digram
stride = cand_p - (p + n) + 1
if candidate_digram == test_digram:
matches.append( "Matched : {} at {} is {} at {} stride {}".format( test_digram, p+1, candidate_digram, cand_p+1, stride ) )
p_found.extend( [p, cand_p] )
if candidate_digram[::-1] == test_digram:
reverses.append( "Reversed : {} at {} is {} at {} stride {}".format( test_digram, p+1, candidate_digram, cand_p+1, stride ))
p_found.extend( [p, cand_p] )
print "\n".join( matches )
#print "\n".join( reverses )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment