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

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Spring 2006: More Coding

Questions? Post them here.

why is this not working for the 1st question?

def even(number):
 if int(number)/2 == int:
  print "1"
 else:
  print "0"

Try using the mod function, %, it will work much easier.-poof #10

I tried running that as well. I think JES is too limited in its number types for this kind of check to work. You have to make n a float before using it or else the number becomes an integer. Try using this:

def isEven(n):
n = float(n)
a = float(n/2)
b = int(n/2)
if a == b:
return 1
else:
return 0

This should work.

If you remember how numbers(decimals) and integers(no decimals) work in python, you know:

5 / 2 = 2

but

5.0 /2= 2.5

So you could write:

def isEven(number):
n = number + .0
if (n/2) == (number/2):
print 1
else:
print 0

map and filter are confusing to understand from the book – is there another place we can look (and i promise it wasn't suppose to rhyme)

map(isPrime, blah blah blah) would print
0
0
0
1
0
1
0
1
1
1
No. I think you switched your 1s and 0s.-Charlie Holder
and filter(isPrime, blah blah blah) would print
4
6
8
9
10
right? (where "blah blah blah" are those numbers.)
Filter returns the ones that are true. Are 4 6 8 9 and 10 prime numbers? -
Charlie Holder: 

so yeah... i was backwards in that posting... i took "isPrime" to be "isntPrime". my bad. heres what the functions would print in real life...
map(isPrime, etc)
0
1
1
0
1
0
1
0
0
0
So, you can look at the definition of a prime number in two ways. 1) a prime number is only divisble by 1 and itself. So 1 would be prime because it satisfies that; 2) a prime numbers has exactly 2 divisors. Since 1 doesn't satisfy that it would not be prime. You can make your own decision. -
Charlie Holder: 
and filter(isPrime, etc)
2
3
5
7
better, Larry?
(once again, "etc" is the numbers 1-10 like you were supposed to input)

Much better :)Larry Olson

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

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



my understanding is that 'int' is only a function and not something that u can compare a number to in an if statement using '=='. That's it why the first one isnt working.



Link to this Page