Homework 1: Welcome to Python

Due: Part I due at 11pm on June 20 (nothing to turn in).
Due: Part II due at 11pm on June 22. Submit via Catalyst CollectIt (a.k.a. Dropbox).

This assignment will familiarize you with the Python programming language and the IDLE editor, which you will use throughout the quarter.

Python is an easy-to-use but powerful programming language that is particularly well-suited to quickly creating small programs.

IDLE is more than just an editor; it is a simple but powerful IDE, or "integrated development environment", which lets you edit, run, navigate, and debug Python code.

Part I: Get started with Python

Follow the CSE 331 instructions for installing Python.

Read (and follow along with!) a short tutorial for IDLE. (You could also try another short tutorial for ILDE, but that is optional.)

Optionally, follow the first parts of a Python tutorial.

There is nothing to turn in for Part I of the assignment.

Part II: Programming exercises

Download the file hw1.py.

For problems in this homework, you will edit your local copy of the hw1.py file. You may do your work by editing and running hw1.py (we recommend this approach), or by typing code at the Python interpreter and copying it into the appropriate part of hw1.py when you are satisfied with it. When you have completed this homework, running the hw1.py file should compute and print the answers to all the problems.

Don't forget to include documentation (source code comments, on a line starting with #) as appropriate in file hw1.py.

Problem 1: Roots

Compute and print both roots of the quadratic equation x2-5.86 x+ 8.5408.

Hint: recall that the roots of a quadratic equation ax2+bx+c are x=

−b ± √ b2 − 4ac 
          2a          

Hint: use the math.sqrt function to compute the square root. (If you are using the Python interpreter, you need to first do import math. This already appears in the hw1.py file that you will be editing, so everything is already set up for you if you are working within that file.)

Problem 2: Reciprocals

Use a for loop to print the decimal representations of 1/2, 1/3, ..., 1/10.

Problem 3: Triangular numbers

Use a for loop to compute the 10th triangular number. The nth triangular number is defined as 1+2+3+...+n. (You can also compute the nth triangular number as n*(n+1)/2. Use this forumla to double-check that your loop is correct.)

Hint: This outline is an almost-complete solution. You only have to replace each ellipsis by an expression.

n = 10
triangular = 0
for i in ...:
    triangular = ...
print "Triangular number", n, "via loop:", triangular
print "Triangular number", n, "via formula:", n*(n+1)/2

Your solution must not mention the number 10, except in the n = 10 line. Your solution can use n, but not 10. In other words, your code (after the n = 10 line) should work for any value of n.

Problem 4: Factorial

Use a for loop to compute 10!, the factorial of 10. Recall that the factorial of n is 1*2*3*...*n.

The first line of your solution will be n = 10. After that, your solution should not use 10 again, though your solution will use n. In other words, your code (after the n = 10 line) should work for any value of n.

Hint: Your answer will be similar to your answer to "Problem 3: Triangular numbers".

Problem 5: Multiple factorials

Write code to print the first 10 factorials, in reverse order. In other words, write code that prints 10!, then prints 9!, then prints 8!, ..., then prints 1!. Its literal output will be:

3628800
362880
40320
5040
720
120
24
6
2
1

The first line of your solution should assign a variable numlines to 10, and then the rest of your solution must not use 10 anywhere.

Hint: Use two nested for loops.
The outer loop sets the value of n to the values numlines, numlines-1, numlines-2, ..., 1, in succession.
Then, the body of that loop is itself a loop — exactly your solution to "Problem 4: Factorial", without the first line n = 10 that hard-codes the value of n.

Problem 6: Sums of reciprocals of factorials

Compute the following value:
  1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/10!
The value should be close to e, the base of the natural logarithms.

Hint: The easiest way to solve this is with two nested for loops. It is possible, but tricky, to compute this using only one for loop. That is not necessary for this assignment.

Hint: Copy your solution to "Problem 5: Multiple factorials", then modify it. Rather than printing the factorials, you will add their reciprocals to a running total, then print that total at the end.

Hint: don't try to work the very first "1 +" into your loop; do it outside the loops (either at the very beginning or the very end of the outer loop).

Submit your work

You are almost done!

Double-check that running your entire file produces output for each problem in the assignment, and that you have commented out any extraneous output (which you may have used for debugging, for example).

At the bottom of your hw1.py file, state which students or other people (besides the course staff) helped you with the assignment, or that no one did.

At the bottom of your hw1.py file, in the "Reflection" part, state how many hours you spent on this assignment. Also state what you or the staff could have done to help you with the assignment.

Submit the following files via Catalyst CollectIt (a.k.a. Dropbox):