#  o p t S i e v e . p y
#
#  Chris Meyers. 10/26/2013
#
from htmlFrame import HtmlFrame
from matrix    import Matrix

htmlPage = HtmlFrame()
htmlPage.banner = "Animated Sieve of Erastosthenes"
BOLD   = "color:red;font-weight:bold;"

primes = Matrix(1,20)
primes.tableAttr = 'cellspacing="0" cellpadding="10"'
primes[0,0] = 2
nprimes = 1

for x in range(3,21) :
    primeSofar = True
    for px in range(nprimes) :
        primes.title = "Testing if %s is divisible by primes so far" % x
        primes.style[0,px] = BOLD
        htmlPage.item1 = primes.renderHtml(wrap=5)
        htmlPage.makeFrame()           #break
        primes.style[0,px] = ""
        div = primes[0,px]
        if x % div == 0 : primeSofar = False
        if not primeSofar : break

    if primeSofar :
        primes[0,nprimes] = x
        nprimes += 1
Example #2
0
# o p t F i b . p y
#
#  Chris Meyers. 10/26/2013
#
from htmlFrame import HtmlFrame
from matrix    import Matrix

htmlPage = HtmlFrame()
htmlPage.banner = "Animated Fibonacci Sequence"
BOLD   = "color:red;font-weight:bold;"

fibs = Matrix(1,20)
fibs.tableAttr = 'cellspacing="0" cellpadding="10"'
fibs[0,0] = 1
fibs[0,1] = 1

for i in range(2,100) :
    fibs.style[0,i-1] = BOLD
    fibs.style[0,i-2] = BOLD
    fibs[0,i] = fibs[0,i-1]+fibs[0,i-2]
    fibs.title = "Last 2 elements add for new one"
    htmlPage.item1 = fibs.renderHtml(wrap=10)
    htmlPage.makeFrame()    #break
    fibs.style[0,i-2] = ""  # uncolor behind

#  o p t K n a p s a c k . p y
#
#  Chris Meyers. 10/17/2013
#

from htmlFrame import HtmlFrame
from matrix    import Matrix

maxwgt = 10
vals = [0,10,40,30,50]
wgts = [0, 5, 4, 6, 3]

htmlPage = HtmlFrame()
htmlPage.banner = "Knapsack Problem"
headers=['wt'+str(i) for i in range(maxwgt+1)]

inp = Matrix(len(vals),3)
inp.title = "Sack holds weight %s" % maxwgt
inp.dftFormat = "<pre>%03s</pre>"
inp.tableAttr = 'border="1" cellspacing="0" cellpadding="4"',
inp.tableHeaders=['Item #','Weight','Value']             
for i in range(len(vals)) :
  inp.setrowVals(i, [i, wgts[i], vals[i]])

frame = Matrix(1,2)
frame[0,0] = inp.renderHtml()
nItems = len(vals)
best = Matrix(nItems,maxwgt+1)
best.dftFormat = "<pre>%03s</pre>"

for i in range(1,nItems) :
Example #4
0
#  o p t S i e v e . p y
#
#  Chris Meyers. 10/26/2013
#
from htmlFrame import HtmlFrame
from matrix import Matrix

htmlPage = HtmlFrame()
htmlPage.banner = "Animated Sieve of Erastosthenes"
BOLD = "color:red;font-weight:bold;"

primes = Matrix(1, 20)
primes.tableAttr = 'cellspacing="0" cellpadding="10"'
primes[0, 0] = 2
nprimes = 1

for x in range(3, 21):
    primeSofar = True
    for px in range(nprimes):
        primes.title = "Testing if %s is divisible by primes so far" % x
        primes.style[0, px] = BOLD
        htmlPage.item1 = primes.renderHtml(wrap=5)
        htmlPage.makeFrame()  #break
        primes.style[0, px] = ""
        div = primes[0, px]
        if x % div == 0: primeSofar = False
        if not primeSofar: break

    if primeSofar:
        primes[0, nprimes] = x
        nprimes += 1
Example #5
0
#  o p t K n a p s a c k . p y
#
#  Chris Meyers. 10/17/2013
#

from htmlFrame import HtmlFrame
from matrix import Matrix

maxwgt = 10
vals = [0, 10, 40, 30, 50]
wgts = [0, 5, 4, 6, 3]

htmlPage = HtmlFrame()
htmlPage.banner = "Knapsack Problem"
headers = ['wt' + str(i) for i in range(maxwgt + 1)]

inp = Matrix(len(vals), 3)
inp.title = "Sack holds weight %s" % maxwgt
inp.dftFormat = "<pre>%03s</pre>"
inp.tableAttr = 'border="1" cellspacing="0" cellpadding="4"',
inp.tableHeaders = ['Item #', 'Weight', 'Value']
for i in range(len(vals)):
    inp.setrowVals(i, [i, wgts[i], vals[i]])

frame = Matrix(1, 2)
frame[0, 0] = inp.renderHtml()
nItems = len(vals)
best = Matrix(nItems, maxwgt + 1)
best.dftFormat = "<pre>%03s</pre>"

for i in range(1, nItems):
#   o p t M i n p a t h . p y
#
#  Chris Meyers. 10/26/2013
#
from htmlFrame import HtmlFrame
from matrix    import Matrix

from random     import randint
htmlPage = HtmlFrame()
htmlPage.banner = "Minimum Path from top to bottom"

def renderTriangle(t) :
    t.dftFormat = "<pre>%03s</pre>"
    t.tableAttr='border="0" cellspacing="0" cellpadding="4"'
    return t.renderHtml()

def minpath(nrows) :
  mat = Matrix(1,2)
  tri = Matrix(nrows,nrows)
  org = Matrix(nrows,nrows)
  for r in range(nrows) :
    vals = [randint(1,9) for c in range(r+1)]
    tri.setrowVals(r,vals)
    org.setrowVals(r,vals)

  org.title = "Original Values"
  mat[0,0] = renderTriangle(org)
  mat.tableAttr='border="1" cellspacing="0" cellpadding="4"'

  tri.style.setrowVal(nrows-1,"background-color:lightgreen")
  for row in range(nrows-2,-1,-1) :
#   o p t M i n p a t h . p y
#
#  Chris Meyers. 10/26/2013
#
from htmlFrame import HtmlFrame
from matrix import Matrix

from random import randint
htmlPage = HtmlFrame()
htmlPage.banner = "Minimum Path from top to bottom"


def renderTriangle(t):
    t.dftFormat = "<pre>%03s</pre>"
    t.tableAttr = 'border="0" cellspacing="0" cellpadding="4"'
    return t.renderHtml()


def minpath(nrows):
    mat = Matrix(1, 2)
    tri = Matrix(nrows, nrows)
    org = Matrix(nrows, nrows)
    for r in range(nrows):
        vals = [randint(1, 9) for c in range(r + 1)]
        tri.setrowVals(r, vals)
        org.setrowVals(r, vals)

    org.title = "Original Values"
    mat[0, 0] = renderTriangle(org)
    mat.tableAttr = 'border="1" cellspacing="0" cellpadding="4"'