reading-notes

Testing and Modules

Testing

Source: In Tests We Trust

Writing a Unit test

Example: Identify gender by name

def test_should_return_female_when_the_name_is_from_female_gender():
    detector = GenderDetector()
    expected_gender = detector.run(Ana)
    assert expected_gender == female

Test Driven Development

Checks design of the software first

  1. Write test and make it fail (nothing to test yet!)
  2. Write the feature and make the test pass
  3. Refactor to inprove code quality

Recursion

Source: Recursion

Example: Sum of n numbers

Non-recursive

  f(n) = 1 + 2 + 3 +……..+ n

Recursive

f(n) = 1                    n=1

f(n) = n + f(n-1)           n>1

Base case

int fact(int n)
{
    if (n < = 1) // base case
        return 1;
    else    
        return n*fact(n-1);    
}

Python example

# A Python 3 program to
# demonstrate working of
# recursion
 
 
def printFun(test):
 
    if (test < 1):
        return
    else:
 
        print(test, end=" ")
        printFun(test-1)  # statement 2
        print(test, end=" ")
        return
 
# Driver Code
test = 3
printFun(test)
 
# This code is contributed by
# Smitha Dinesh Semwal

recursion example