#!/usr/bin/env python # # Author: Yotam Medini yotam.medini@gmail.com -- 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(""" Roots """[1:]) # skip the 1st newline char. html.write("\n") for order in range(2, 6): html.write("

Roots of order %d

\n" % order) for n in [10, 100, 1000, 2000, 2006]: print_root(n, order, html) html.write("
\n") # Newline html.close() sys.stdout.write("Now use FireFox to show %s !!\n" % html_filename) # Main program application1() application2() application3() sys.exit(0)