rootver3.py
#!/usr/bin/env python
#
# Author: Yotam Medini [email protected] -- Created: 2006/June/01
#
# print_root Version 3 - demonstarting function with default values
#
import sys
# Version 3.0
def print_root(a, order=2, f=sys.stdout):
low = 0.
high = a + 1.
progress = True
while progress:
mid = (low + high) / 2.
progress = (low < mid < high); #
if mid**order < a:
low = mid
else:
high = mid
f.write("root(%4g) = %f\n" % (a, mid))
# Demo Application
def application1():
sys.stdout.write("\nApplication-1:\n")
for n in range(0, 11):
print_root(n)
# Demo Application
def application2():
sys.stdout.write("\nApplication-2:\n")
for order in [2, 3]:
sys.stdout.write("\nRoots of order=%d\n" % order)
for n in range(0, 10):
print_root(n, order)
# Demo Application
def application3():
html_filename = "/tmp/roots.html"
html = open(html_filename, "w")
html.write("""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><title>Roots</title><body>
"""[1:]) # skip the 1st newline char.
html.write("</body></html>\n")
for order in range(2, 6):
html.write("<h2>Roots of order %d</h2>\n" % order)
for n in [10, 100, 1000, 2000, 2006]:
print_root(n, order, html)
html.write("<br>\n") # Newline
html.close()
sys.stdout.write("Now use FireFox to show %s !!\n" % html_filename)
# Main program
application1()
application2()
application3()
sys.exit(0)
Generated by GNU enscript 1.6.4.