Lab 1: Python programs

Your assignment is to complete the following three functions and email the file to me (pld@cs.luc.edu).

1. Factorial limit

Here is a definition of the factorial function. This version does not use recursion, so it should work for n>1000:

def fact(n):
prod = 1
for factor in range(1,n):
prod = prod * factor
return prod
Paste this into your python session. Then define a python function f(n) = n/fact(n)**(1/n) and see what happens to f(n) as you make n large. Does it seem to approach a limit? Note that you'll have the same integer-division problem with the exponent (1/n) as you had in the preceding example; try 1.0/n. You also have another problem as n gets large. How large an n will work here?

2: temperance

Write a function to convert from Fahrenheit (or Celsius, your choice, but be sure to make your choice clear to me) to words (that is, strings): You are to do this using a big if statement, with elif clauses, as illustrated on page 25 of the Miller & Ranum python booklet. Note that elif is a special shorthand for else if; if you try to use else if, though, you have to keep indenting more and more which becomes unreadable.
if temp >=90:
print "too darn hot"
elif temp >= 70:
print "just right"
elif temp >= 40:
print ...
else:
print "cold as this program gets"
Note that in the else case, above, occurs when temp < 40, and that in all cases only one option is printed. Try to cover at least five temperature ranges (three elif's). Watch where the colons go! And try to be seasonally appropriate.

3: boldify

Web pages are based on html-format files. Html is a text-based format for representing text that may contain font changes. For example, to express bold:

      These words are bold.

you put "<b>" before the bold words and "</b>" after:

      These <b>words</b> are <b>bold</b>.

What you are to do is to write boldify(str, word) that takes a string str and a word word occuring within it, and and adds the <b>/</b> tags above around the first occurrence of the given word within str. That is,

       lab1.boldify("here is a long line", "long")

should return the string

      "here is a <b>long</b> line"

This is pretty easy in python using the following string tools:

Test your boldify using a word in the middle of str and also the first and last word of str. You may ignore the case where word does not occur in str.