Tuesday, February 22, 2011

Feb 22

This is the fixed hypotenuse program:

def hypotenuse(a, b):
"""
>>> hypotenuse(3, 4)
5.0
>>> hypotenuse(12, 5)
13.0
>>> hypotenuse(7, 24)
25.0
>>> hypotenuse(9, 12)
15.0

"""
return ((a**2) + (b**2))**0.5

if __name__== '__main__':
import doctest
doctest.testmod()

Friday, February 18, 2011

Feb 17- 18

This is a fix on the factor program:

def is_factor(f, n):
"""
>>> is_factor(3, 12)
True
>>> is_factor(5, 12)
False
>>> is_factor(7, 14)
True
>>> is_factor(2, 14)
True
>>> is_factor(7, 15)
False
"""
return n % f == 0
if __name__ == '__main__':
import doctest
doctest.testmod()

This is a fix on the is divisible by 2 or 5 program:
def is_divisible_by_2_or_5(n):
    """
      >>> is_divisible_by_2_or_5(8)
      True
      >>> is_divisible_by_2_or_5(7)
      False
      >>> is_divisible_by_2_or_5(5)
      True
      >>> is_divisible_by_2_or_5(9)
      False
    """
    return n % 2 == 0 or n % 5 == 0

if __name__ == '__main__':
    import doctest
    doctest.testmod()

This is a fix on the multiple program:
def is_multiple(m, n):
    """
      >>> is_multiple(12, 3)
      True
      >>> is_multiple(12, 4)
      True
      >>> is_multiple(12, 5)
      False
      >>> is_multiple(12, 6)
      True
      >>> is_multiple(12, 7)
      False
    """
    return m % n == 0
if __name__== '__main__':
import doctest
doctest.testmod()

Chp 6:
When starting out using multiple assignments, I noticed that when you first put in a number like lets say a = 3 and then u change it to where a = 4. When you tell python to print a you'll get 4 instead of 3 because sense you changed a value to being 4 last, that would be the output of the number. Also when you try to assign the variable a number you must always have the variable going first before the number. Ex: a = 7 correct, 7 = a incorrect. When you put 7 = a, that is illegal meaning it will not work. 
On updating variables, I've found out that when you first put a variable in like x = 99 then if u want to improve the variable to where its a different number you can just do x = a different number but you can also use x = x -/+* a number(you can use all equation symbols). So lets say I wanted to change x = 99 to x = 100, so what I would do is ether do x = 100 or i can do x = x + 1. After putting the second x = x + 1 and you say print x, you'll get 100. 
Using the countdown program helped me understand that when you use a variable like n and then put n > 0:   print n then n = n - 1. When you put n = n -1 and n > 0, this basically will continue a count down until it hits 0 then will print or do what ever you tell it to print.  

Wednesday, February 16, 2011

Feb 16

This is the fix program of ch05 program:

def compare(a, b):
"""
>>> compare(5, 4)
1
>>> compare(7, 7)
0
>>> compare(2, 3)
-1
>>> compare(2, 1)
1
"""
return a-b

if __name__ == '__main__':
import doctest
doctest.testmod()

this will now pass the doctest
Fix on the hypotenuse program

def hypotenuse(a, b):
"""
>>> hypotenuse(3, 4)
5.0
>>> hypotenuse(12, 5)
13.0
>>> hypotenuse(7, 24)
25.0
>>> hypotenuse(9, 12)
15.0
"""
return

if __name__== '__main__':
import doctest
doctest.testmod()
This is not the completed program yet, im still working on fixing the return so that the program passes the doctest. So far I no that the number needs to have .0 in it to allow the results to have .0 to them. I've ruled out that a**2 + b**2 will not work. I've also tried a - b + 4 which also came out to fail.

Tuesday, February 15, 2011

Feb 14-15

Fix on #4:

def is_even(n):
"""
>>> is_even(1)
False
>>> is_even(2)
True
>>> is_even(3)
False
>>> is_even(4)
True
>>> is_even(5)
False
>>> is_even(6)
True
>>> is_even(7)
False
>>> is_even(8)
True
>>> is_even(9)
False
"""
return n % 2 == 0
if __name__== '__main__':
import doctest
doctest.testmod()

and which now will allow for the program to run and pass. 
Fix on #5:
def is_odd(n):
"""
>>> is_odd(1)
True
>>> is_odd(2)
False
>>> is_odd(3)
True
>>> is_odd(100)
False
>>> is_odd(9999)
True
>>> is_odd(1000000)
False
>>> is_odd(300957)
True
>>> is_odd(298745230)
False
"""
return not n % 2 == 0
if __name__== '__main__':
import doctest
doctest.testmod()
This then allows the program to change from having to check to see if the number is even to check to see if the number is odd. What I had to change from the is_even program was the return n % 2 == 0 to return not n % 2 == 0. Which this is meaning that if the number(n) is divisible by 2 and has nothing left over, it is not a odd number, but sense for the is_odd program I applied not to the body to tell that if the numbers do not fight the divisible property of 2 = 0 then it will be a odd number. 
There was not to much put on for these too days due to I was trying to figure out what I had to do to change the output of the is_even program so that it outputs the right figures for is_odd program.

Friday, February 11, 2011

Feb 11

OK when working on questions #4 and #5, I've been having some difficulty with it. What I'm suppose 2 do is for question #4 is make a program that determines if a number is even and if it is even then it will out put True. If the number is an odd number then the out put would come out to be False. Then see for question #5 I have to do the same thing but instead for only odd numbers. Ill continue on this program and see how far I can get.
So far this hasn't given me any problem or said that the test has failed for question #4 so this is the program:

def is_even(n):
"""
>>> is_even(1)
False
>>> is_even(2)
True
>>> is_even(3)
False
>>> is_even(4)
True
>>> is_even(5)
False
>>> is_even(6)
True
>>> is_even(7)
False
>>> is_even(8)
True
>>> is_even(9)
False
"""
if __name__== ' __main__':
import doctest
doctest.testmod()

#5: def is_odd(n):
"""
>>> is_even(1)
True
>>> is_even(2)
False
>>> is_even(3)
True

>>> is_even(4)
False

>>> is_even(5)
True

>>> is_even(6)
False

>>> is_even(7)
True

>>> is_even(8)
False

>>> is_even(9)
True

"""
if __name__== ' __main__':
import doctest
doctest.testmod()
#6:

Thursday, February 10, 2011

Feb 10

********************************************************************************
File "intercept.py", line 5, in __main__.intercept
Failed example
      intercept(6, 1, 1, 6)
Expected:
      7.0
Got nothing
*******************************************************************************
File "intercept.py", line 7, in __main__.intercept
Failed example:
      intercept(4, 6, 12, 8)
Expected:
       5.0
Got nothing
*******************************************************************************
1 items had failures:
     3 of     3 in __main__.intercept
***Test Failed*** 3 failures.
#4:

Wednesday, February 9, 2011

Feb 7-9

Alright on Monday I wasn't able to get on due to computer issues. This continued to at least half of the class on Tuesday. So today i'll continue on Chapter 5 questions #1.
#1: def compare(a, b)
"""
>>> compare(5, 4)
1
>>> compare(7, 7)
0
>>> compare(2, 3)
-1
>>> compare(42, 1)
1
"""


"""
>>>compare(a > b)
1
>>>compare(a == b)
0
>>>compare(a < b)
-1
"""


if __name__== ' __main__':
import doctest
doctest.testmod()
This program really got me for awhile. When I first started out I had changed the def compare(a, b) to def compare( a > b). Which was giving me an error saying that > was an invalid syntax. But the spur of the moment thought make me think to instead add a > b, a == b, and a < b, to next to the >>>compare. After one try i got this right away. This was a pretty easy program to make after I found out exactly what I had to do.
#2: def hypotenuse(a, b): """ >>> hypotenuse(3, 4) 5.0 >>> hypotenuse(12, 5) 13.0 >>> hypotenuse(7, 24) 25.0 >>> hypotenuse(9, 12) 15.0 """ if __name__== '__main__': import doctest doctest.testmod()
Which I then got an output of:

*****************************************************************************
File "hypotenuse.py", line 7, in __main__.hypotenuse
Failed example:
hypotenuse(7, 24)
Expected:
25.0
Got nothing
*****************************************************************************
File "hypotenuse.py", line 9, in __main__.hypotenuse
Failed example:
hypotenuse(9, 12)
Expected:
15.0
Got nothing
*****************************************************************************
1 items had failures:
4 of 4 in __ main__.hypotenuse
***Test Failed*** 4 failures.
#3: def slope(x1, y1, x2, y2): """ >>> slope(5, 3, 4, 2) 1.0 >>> slope(1, 2, 3, 2) 0.0 >>> slope(1, 2, 3, 3) 0.5 >>> slope(2, 4, 1, 2) 2.0 """ if __name__== '__main__': import doctest doctest.testmod()
 
With an end result of:
*****************************************************************************
File "slope.py", line 7, in __main__.slope
Failed example:
      slope(1, 2, 3, 3)
Expected:
      0.5
Got nothing
*****************************************************************************
File "slope.py", line 9, in __main__.slope
Failed example:
      slope(2, 4, 1, 2)
Expected:
       2.0
Got nothing
*****************************************************************************
1 items had failures:
    4 of      4 in __main__.slope
***Test Failed*** 4 failures.
ef intercept(x1, y1, x2, y2):
    """
      >>> intercept(1, 6, 3, 12)
      3.0
      >>> intercept(6, 1, 1, 6)
      7.0
      >>> intercept(4, 6, 12, 8)
      5.0
    """
if __name__== '__main__':
import doctest
doctest.testmod()
With a end result of:
*****************************************************************************
Ill have continue this tomorrow.