# exercises

1. Write a function that finds the minimum element of an array, and returns its value. Thus
 findmin.py 1 def findmin(a): 2 # Your code goes here 3 4 b = [1,4,-1,2] 5 n = findmin(b) 6 print n # prints "-1"
2. Write a function that finds the minimum element of an array, and returns its index. Thus
 findmin2.py 1 def findmin(a): 2 # Your code goes here 3 4 b = [1,4,-1,2] 5 n = findmin(b) 6 print n,b[n] # prints "2, -1"
3. You can swap any two elements of an array by using tuples:
 swapelem.py 1 a = [1,2,3,4,5] 2 a[0], a[1] = a[1], a[0] 3 print 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 1 def swaplo(a): 2 # Your code goes here 3 4 b = [5,1,3,0,2,4] 5 swaplo(b) # swaps index 0 and 4 6 print 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 1 def swaplo(a,n): 2 # Your code goes here 3 4 b = [-5,1,3,0,2,4] 5 swaplo(b,1) # swaps index 1 and 4 6 print 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 1 import random 2 3 def swaplo(a,n): 4 # Your code goes here 5 6 def sort(a): 7 for i in range(len(a)): 8 swaplo(a,i) 9 10 b = [] 11 for i in range(10): 12 b += [random.randint(1,100)] 13 print b # unsorted 14 sort(b) 15 print 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 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 [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 1 import math 2 3 def quad(a,b,c): 4 # Your code goes here 5 6 r1, r2 = quad(1.0,7.0,12.0) 7 print 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 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)
Further research: Simple Python Programs.
 prev next