Created
March 22, 2010 15:34
-
-
Save radiosilence/340177 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ExampleTask( a ): | |
o = algorithms.OpCounter( len(a) ) | |
s = 0 | |
o.Add() | |
for i in range( 0, len(a) ): | |
o.Add() | |
s += a[i] | |
o.Add() | |
o.Add() | |
return (s, o.Info()) | |
def Maximum( a ): | |
o = algorithms.OpCounter( len(a) ) | |
for i in a: | |
o.Add() | |
try: | |
if i > x: | |
x = i | |
except NameError: | |
x = i | |
return (x, o.Info()) | |
def Unique( a ): | |
o = algorithms.OpCounter( len(a) ) | |
u = [] | |
for i in a: | |
o.Add() | |
for j in u: | |
o.Add() | |
if i == j: | |
return ("%s is not unique." % i, o.Info()) | |
u.append(i) | |
return ("All numbers unique.", o.Info()) | |
def Vending( inserted ): | |
o = algorithms.OpCounter( 1 ) | |
coins = [ 50, 20, 10, 5, 2, 1 ] | |
results = [] | |
for k in coins: | |
amt = ( inserted - ( inserted % k ) ) / k | |
o.Add() | |
if amt > 0: | |
results.append( "%sx%sp" % ( amt, k )) | |
inserted = inserted - ( k * amt ) | |
if inserted == 0: | |
return ( ", ".join(results), o.Info() ) | |
return ( "Failed", o.Info() ) | |
def Square( n ): | |
if n < 0: | |
return -1 | |
else: | |
return pow( n, 2 ) | |
def SqRoot( n ): | |
if n < 0: | |
return -1 | |
else: | |
return sqrt( n ) | |
def CompFG( n ): | |
res = SqRoot( Square( n ) ) | |
if res != -1: | |
return res | |
else: | |
return "Fail" | |
def CompGF( n ): | |
res = Square( SqRoot( n ) ) | |
if res != -1: | |
return res | |
else: | |
return "Fail" | |
def CryptoEncode( message, table, P, R ): | |
crypto = "" | |
for c in message: | |
try: | |
crypto += "%s" % FindKey( int( pow( table[c], P ) % R ), table ) | |
except: | |
crypto += "_" | |
return crypto | |
def FindKey( val, table ): | |
for k, v in table.items(): | |
if v == val: | |
return k | |
raise Exception( "Not Found", val ) | |
def CryptoDecode( crypto, table, Q, R ): | |
message = "" | |
for c in crypto: | |
try: | |
message += "%s" % FindKey( int( pow( table[c], Q) % R ), table ) | |
except: | |
message += "_" | |
return message | |
def Pt5Iterative( n ): | |
while n != 1: | |
if n % 2 == 0: | |
n = n / 2 | |
else: | |
n = ( 3 * n ) + 1 | |
return "Iteration works! n=%s" % n | |
def Pt5Recursive( n ): | |
if n != 1: | |
if n % 2 == 0: | |
n = n / 2 | |
else: | |
n = ( 3 * n ) + 1 | |
return Pt5Recursive( n ) | |
else: | |
return "Recursion works! n=%s" % n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment