- How to Program, Part I
- How to Program, Part II
- How to Program, Part III
- How to Program, Part IV
- How to Program, Part V
- exercises
- pyMPI tutorial
- Calculating PI, Part I
- Calculating PI, Part II
- Calculating PI, Part III
- Poogle - Web Search
- Mandelbrot Sets
- Mandelbrot, The Code
- Mandelbrot, The Images
- Conway's Life, Part I
- Life Code Listing
- Conway's Life, Part II
- MPI Life Code Listing
| exercises
- Write a program to take the sum of all integers from 2 to 17 excluding 13.
- The Fibonacci sequence is a sequence of numbers that proceeds as
follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. Each number is the sum of
the previous two. We can write a function to compute a number in this
sequence using Python:
file: fib.py |
1 | def fib(n): |
2 | if n == 0: |
3 | return 0 |
4 | elif n == 1: |
5 | return 1 |
6 | return fib(n-1)+fib(n-2) |
7 | |
8 | print fib(7) |
> python fib.py
13
|
When a function calls itself (the way fib() does) it is called recursion.
Use recursion to write a program to compute a factorial (4! = 4*3! = 4*3*2*1, and 1!=1)
- Write a program to make a copy of an array with
the even integers removed. You can
test for whether a number is even by using the % operator.
file: remain.py |
1 | import random |
2 | |
3 | # create a list of 10 random integers from 1 to 100 |
4 | # define a function that returns a new array that is |
5 | # the same as your randomly generated one, except that |
6 | # the even numbers are removed. |
7 | a = [] |
8 | for i in range(10): |
9 | a.append(random.randint(1,100)) |
10 | print a |
11 | |
12 | # Here's how you calculate remainders: |
13 | print "remainder of 15 / 2 =",15 % 2 |
14 | print "remainder of 39 / 3 =",39 % 3 |
> python remain.py
[45, 60, 45, 100, 18, 10, 82, 83, 35, 63] remainder of 15 / 2 = 1 remainder of 39 / 3 = 0
|
- Write a program to convert a decimal number to an array of digits.
In other words, make a function that converts 1298 to [1,2,9,8].
- Write a program to convert an array of digits to a number. In other
words, one that converts [3,9,5,2] to 3952.
- Write a program to convert an array of decimal digits to octal digits.
- Write a program to convert an array of octal digits to decimal digits.
- Write a program to compute the answer to the quadratic formula. In
other words, create a function that takes three values: a,b, and c,
and returns the two solutions of a*x**2+b*x+c==0.
- Write a program to determine whether a number is a perfect square.
The program should print "true" if it is a perfect square and
"false" otherwise.
- Using the fact that string is an array of characters, write a find()
program that works like string's find method.
- Write a program to sort an array. First, create a random array
as follows:
file: sort.py |
1 | import random |
2 | |
3 | a = [] |
4 | for i in range(10): |
5 | a.append(random.randint(1,100)) |
6 | print a |
7 | |
8 | # put code here that sorts array a |
> python sort.py
[40, 64, 13, 24, 57, 68, 83, 11, 1, 31]
|
Further research:
Simple Python Programs.
| |