Final Exam Review Fall 2004: More Coding
What the heck is imperative programming?
| Well, describe what functional and object-oriented programming is and we can go from there. Angela Liang |
functional is verb oriented, focuses on what needs to be done.
object is noun oriented, focuses on what needs to be manipulated.
| well, imperative programming uses states and statements that changes those states. Imperative programs are a sequence of commands for the computer to perform. Angela Liang |
this probably isn't exactly what you were looking for with these but they work nonetheless!:
def isEven(number):
if number.endswith("0") or number.endswith("2") or number.endswith("4") or number.endswith("6") or number.endswith("8"):
print "1"
if number.endswith("1") or number.endswith("3") or number.endswith("5") or number.endswith("7") or number.endswith("9"):
print "0"
def isOdd(number):
if number.endswith("0") or number.endswith("2") or number.endswith("4") or number.endswith("6") or number.endswith("8"):
print "0"
if number.endswith("1") or number.endswith("3") or number.endswith("5") or number.endswith("7") or number.endswith("9"):
print "1"
the map one will return 0s and 1s
the filter will return 1,2,3,5,7
Remember, there's a big difference between printing and returning. "number" is also a numerical variable, not a string, so you can't use .endswith. You'll have to do this with math functions. Hint: use mod (the % symbol) -Blake O'Hare |
if you change the "print"s to "return"s, it works if you put the number in quotes when you call the function. where is the % symbol in the book? i can't seem to find it..
| The % symbol is the mod operator. It returns the remainder after integer division. (i.e 13 % 2 = 1 and 12 % 2 = 0) And it works if you put the number in quotes becuase that is inputting the number as a string. The number is not supposed to be inputted as a string. You need it to work when the number is inputted as a number (no quotes) Kelly Lyons |
how about these? :
def isEven(number):
if number % 2 == 0:
return "1"
if number % 2 == 1:
return "0"
def isOdd(number):
if number % 2 == 0:
return "0"
if number % 2 == 1:
return "1"
| Does it work when you run it in JES? Also, do you notice that in isOdd, you are returning the same thing as is in the if condition. Could you maybe simplify your function to work without any if statements? (Not necessary of course, but interesting to try) Kelly Lyons |
yep they work in jes.. maybe i'll try that if i have time
Guys, you shold do this:
def isEven(num):
a = num%2
return a==0
def isOdd(num):
b = num%2
return b==1
filter will return true or false (0)
and map will return 1,2,3,5,7
| Not bad on the code. But, can you do it with one line? For the filter, what exactly will it return? Greg Leo |
map [1,1,1,0,1,0,1,0,0,0]
filter [1,2,3,5,7]
cool...how about this: one-line-code
def isEven(num): return (num%2)==0
def isOdd(num): return (num%2)==0
| I think your isOdd is incorrect. This will return 1 if the number%2=0. That is an even number. Kelly Lyons |
| Heres another challenge, try doing it without using the "==" and without if statements. Greg Leo |
how do you do the isPrime one???
Link to this Page