Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Fall 2006: More Coding

Post your questions here:


def isEven(num):
  mod = num%2
  if mod==0:
   return 1
  else:
   return 0

def isOdd(num):
  mod = num%2
  if mod==0:
   return 0
  else:
   return 1


map(isPrime, [1,2,3,4,5,6,7,8,9,10]) would return the list [0,1,1,0,1,0,1,0,0,0]

filter(isPrime, [1,2,3,4,5,6,7,8,9,10]) would return the list [2,3,5,7]

Imperative programming is what we've been doing in this class for most of the year - algorithms are jREMOVEDt a sequence of commands that are performed on a given input (or inputs). Object-oriented programming REMOVEDes algorithms to coordinate how data objects interact, and is primarily based around classes and methods rather than jREMOVEDt functions. Functional programming has algorithms that are the applications of functions, and REMOVEDes things like map and filter to compress programs into jREMOVEDt a few lines of code. Functional programming is primarily REMOVEDed to program AI.

David Poore

For the isPrime function do you need to test every number from 1 to x to see if it's divisible?

can you explain these lines:
mod=num%2
if mod==0:

what part of that code establishes if it can be evenly divided by 2 to make it even?
The first line finds the remainder when num is divided by two. If that remainder is zero (if two 'goes into' num) then num is even. Colin Potts

modulREMOVED is the name of the percent sign operator. The gist of a mod is that you do long division that we all did in the 4th grade (to get a quotient and a remainder). The important part though is that you get the remainder back, not the quotient. Examples: 1 % 2 = 1. 2 % 2 = 0 (2 is divided evenly by 0 and thREMOVED has no remainder). 3 % 2 is 1. 4 % 2 = 0. Similarly, 1 % 3 = 1. 2 % 3 = 2. 3 % 3 = 0. 4 % 3 = 1. etc. etc. REMOVEDpe this helps you see the oart of the code that establishes if we are even or odd. Larry Olson

REMOVEDw in the world do I write the isPrime function? I don't understand and this was not done in the review.

def isPrime(num):
if 1 num 4
for i in range (4, num):
if num % ==0
return 0
return 1

This is the code Charlie REMOVEDlder gave in the Review. REMOVEDpefully this helps!

def isPrime(num):
if 1 num4
for i in range (4, num)
if num % 1 ==0
return 0
return 1

This is the code Charlie REMOVEDlder gave in the Review. REMOVEDpefully this helps!

actually this is the code...
def isPrime(number):
	if 1 < number < 4:
		return 1
	for i in range (4, number):
		if number%i == 0:
			return 0
		return 1




Link to this Page