1. How to Program, Part I
  2. How to Program, Part II
  3. How to Program, Part III
  4. How to Program, Part IV
  5. How to Program, Part V
  6. How to Program, Part VI
  7. exercises
  8. pyMPI tutorial
  9. Calculating PI, Part I
  10. Calculating PI, Part II
  11. Calculating PI, Part III
  12. Dividing Work
  13. More MPI
  14. Poogle - Web Search
  15. Mandelbrot Sets
  16. Mandelbrot, The Code
  17. Mandelbrot, The Images
  18. Mandelbrot In CUDA
  19. Conway's Life, Part I
  20. Life Code Listing
  21. Conway's Life, Part II
  22. MPI Life Code Listing

exercises

  1. Write a function that finds the minimum element of an array, and returns its value. Thus
    findmin.py
    1def findmin(a):
    2  # Your code goes here
    3 
    4b = [1,4,-1,2]
    5n = findmin(b)
    6print n # prints "-1"
  2. Write a function that finds the minimum element of an array, and returns its index. Thus
    findmin2.py
    1def findmin(a):
    2  # Your code goes here
    3 
    4b = [1,4,-1,2]
    5n = findmin(b)
    6print n,b[n] # prints "2, -1"
  3. You can swap any two elements of an array by using tuples:
    swapelem.py
    1a = [1,2,3,4,5]
    2a[0], a[1] = a[1], a[0]
    3print a
    $ python ./swapelem.py
    [2, 1, 3, 4, 5]
    
    Write a function that finds the minimum element of an array, and swaps it with whatever element is at position 0. Thus, given the array [5,1,3,0,2,4], it should swap the first (array index 0) and fourth (array index 3) elements. The answer should be [0,1,3,5,2,4].
    swapminfirst.py
    1def swaplo(a):
    2  # Your code goes here
    3 
    4b = [5,1,3,0,2,4]
    5swaplo(b) # swaps index 0 and 4
    6print b # Prints [0,1,3,5,2,4]
  4. Change the function in the preceding example to take a second argument "n", the first index of the array. Instead of finding the minimum, it will find the minumum that occurs after "n", and swaps its position with n.
    swapminn.py
    1def swaplo(a,n):
    2  # Your code goes here
    3 
    4b = [-5,1,3,0,2,4]
    5swaplo(b,1) # swaps index 1 and 4
    6print b # Prints [-5,0,3,1,2,4]
  5. Write a sort function by calling swaplo() from the preceeding example a bunch of times.
    mysort.py
    1import random
    2 
    3def swaplo(a,n):
    4  # Your code goes here
    5 
    6def sort(a):
    7  for i in range(len(a)):
    8    swaplo(a,i)
    9 
    10b = []
    11for i in range(10):
    12  b += [random.randint(1,100)]
    13print b # unsorted
    14sort(b)
    15print b # sorted
    Why does this work?
  6. 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.
    remain.py
    1import random
    2 
    3create a list of 10 random integers from 1 to 100
    4define a function that returns a new array that is
    5the same as your randomly generated one, except that
    6the even numbers are removed.
    7a = []
    8for i in range(10):
    9    a.append(random.randint(1,100))
    10print a
    11 
    12Here's how you calculate remainders:
    13print "remainder of 15 / 2 =",15 % 2
    14print "remainder of 39 / 3 =",39 % 3
    $ python ./remain.py
    [78, 14, 89, 54, 82, 11, 32, 56, 80, 24]
    remainder of 15 / 2 = 1
    remainder of 39 / 3 = 0
    
  7. Write a program to convert an integer to an array of digits. In other words, make a function that converts 1298 to [1,2,9,8]. Note that you can convert a number to a string using the str() function, and you can use int() to convert a string to a number.
  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.
  9. 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. In case you don't remember, that looks like this: r = (-b+sqrt(b**2-4*a*c))/(2*a) and r = (-b-sqrt(b**2-4*a*c))/(2*a).
    quad.py
    1import math
    2 
    3def quad(a,b,c):
    4  # Your code goes here
    5 
    6r1, r2 = quad(1.0,7.0,12.0)
    7print r1, r2
  10. Write a program to determine whether a number is a perfect square. A number is a perfect square if its square root is an integer. Note that you can convert a value to a real number with float(), and convert it to an integer with int(). The program should print "true" if it is a perfect square and "false" otherwise.
  11. 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:
    fib.py
    1def 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 
    8print 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)
Further research: Simple Python Programs.