Demo entry 6362215

Ex Machina

   

Submitted by David Johnston on May 07, 2017 at 17:30
Language: Python. Code size: 3.3 kB.

import sys
import operator
import unittest
import urllib
import re

def accumulate(iterable, func=operator.add):
    it = iter(iterable)
    total = next(it)
    yield total
    for element in it:
        total = func(total, element)
        yield total

class Sieve():
    def __init__(self, n):
        self.x = sieve(10000)
        self.y = list(accumulate(self.x))
        pass

    def prime(self, n):
        return prime(n,self.x)

    def prime_above(self, n):
        if ( self.x[n] ):
            return n
        return self.prime_above(n + 1)
    def prime_to_nth(self,p):
        return self.y[p]

def sieve(n):
    x      = [1] * n
    x[0:1] = [0, 0]        # 0 is not a prime !
    for i in range(2,n/2): # multiples of 2, 3,
        j = 2 * i
        while j < n:
            x[j] = 0
            j = j + i
    return x

def prime(n,x):
# Find nth prime
    i = 1
    j = 1
    while j <= n:
        if x[i] == 1:
            j = j + 1
        i = i + 1
    return i - 1

def decode_with_sieve(sieve, code, key):
    packets = [ str(sieve.prime(code[i])-key[i])for i in range(0,4)]
    return "".join(packets)

def display_decode(isbn):
    sys.stdout.write("".join(chr(i)for i in [73,83,66,78,32,61,32]))
    sys.stdout.write(isbn)
    print

def encode_with_sieve(sieve, ibsn):
    n       = 4
    g       = ( len(ibsn) + n  - 1 ) / n
    packets = [ ibsn[i * n: (i + 1) * n] for i in range(g) ]
    sieve = Sieve(10000)
    primes  = [ sieve.prime_above(int(packet)) for packet in packets]
    code    = [ sieve.prime_to_nth(prime) for prime in primes]
    key     = [ primes[i] - int(packets[i]) for  i in range(g)]
    return code, key

def add_sieve(*args,**kwards):
    sieve = Sieve(10000)
    def inner_function(f):
        return f(sieve, *args,**kwards)
    return inner_function

def encode(ibsn):
    sieve = Sieve(10000)
    return encode_with_sieve(sieve, ibsn)

def decode(code,key):
    sieve = Sieve(10000)
    return decode_with_sieve(sieve, code,key)

class TestCodex(unittest.TestCase):

    def test_decode(self):
        self.assertEquals( decode( [1206,301,384,5],[1,1,2,2] ),"9780199226559" )

    def test_encode(self):
        self.assertEquals( encode("9780199226559"), ([1206,301,384,5],[1,1,2,2]) )

    def test_application(self):
        find_more_remarkable_book()

def find_more_remarkable_book():
        display_decode(decode([1206,301,384,5],[1,1,2,2])) # outputs ISBN = 9780199226559
        """ Embodiment and the inner life: Cognition and Consciousness in the Space of Possible Minds """

        code,key = encode("9780199226559")
        print(code,key)
        url = "http://www.moneyforbooks.com/textbook-isbns.html"
        text = urllib.urlopen(url).read()
        l = re.findall("(\d{10})",text)
        encoded = [ (e,encode(e)) for e in set(l) ]
        closest = min( encoded, key=  lambda (e,(c,k)) : sum(k))
        print ("most remarkable of {} books is {}".format(len(encoded),closest))
        """
        helloTitle: Abnormal Psychology (paperback)
        Author: Lyons
        Date of Copyright: 2014
        ISBN: 1618826379
        """

if __name__ == "__main__":
    find_more_remarkable_book()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).