Homework 1: Welcome to Python

Due: Part I due at 11pm on Wednesday, January 6, 2016 (nothing to turn in).
Due: Part II due at 11pm on Friday, January 8, 2016. Submit via this turnin page.

Learning Objectives:

You will make use of all of these skills throughout the quarter.

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

Canopy is more than just a code 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 160 instructions for installing Python. Do this even if you already have a version of Python installed on your computer — for CSE 160, you will want a specific Python installation, namely Enthought Python Distribution and the Canopy IDE.

Optionally, you might want to follow the first parts of the Python track on codecademy.

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

Part II: Programming exercises

Download the file hw1.py [Tip: To download, right click this link and select "Save link as" or if you click through to see the file in your browser, just right click and select "Save as" or "Save Page as" or whatever wording your browser uses.]

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 the answers to all the problems, and print the following output (with the ____ replaced by the values you calculated). Please make your output match the following exactly.

Problem 1 solution follows:
Root 1: ____
Root 2: ____

Problem 2 solution follows:
1/2: ____
1/3: ____
1/4: ____
1/5: ____
1/6: ____
1/7: ____
1/8: ____
1/9: ____
1/10: ____

Problem 3 solution follows:
Triangular number 10 via loop: ____
Triangular number 10 via formula: ____

Problem 4 solution follows:
10!: ____

Problem 5 solution follows:
10!: ____
9!: ____
8!: ____
7!: ____
6!: ____
5!: ____
4!: ____
3!: ____
2!: ____
1!: ____

Problem 6 solution follows:
e: ____

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

Tips for Formatting Output

Let's say you're trying to print the following line of output:
1 plus 1 equals 2
Here's some Python code that will do that:
n = 1
print str(n) + " plus " + str(n) + " equals " str(n + n)
You can also use:
n = 1
print str(n), "plus", str(n), "equals", str(n + n)
Note that in the first case, I'm explicitly inserting spaces when I want them, but in the second case, the commas add spaces for me automatically.

You can print a blank line by calling just print (and giving it nothing to print).

Problem 1: Roots

Compute and print both roots of the quadratic equation with the smaller root first: x2-5.86 x+ 8.5408.

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

−b ± √ b2 − 4ac 

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, one on each line.

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 formula 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 code should correctly calculate the 11th, 12th, or any other triangular number just by changing the first line to set n to 11, 12, or any other number. Using the range function should help you accomplish this.

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. [Note: you may not use the math.factorial() function for this problem or any other problem in this homework.]

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

As in Problem 3, your code should calculate 11!, 12!, or any other number's factorial just by changing the first line to set n to 11, 12, or any other number.

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:

10!: 3628800
9!: 362880
8!: 40320
7!: 5040
6!: 720
5!: 120
4!: 24
3!: 6
2!: 2
1!: 1

The first line of your solution should assign a variable numlines to 10. Then, as in Problems 3 and 4, the rest of the code should print the correct number of lines and correct factorial on each line just by setting numlines to a different value.

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 (≈ 2.71828), 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!

Now you're done!