Unit Tests
pieces ofcode to test the input, output, and behavior of your primary codeTest Driven Development
strategy to think and write tests firstExample: 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’
Checks design of the software first
Recursion
- Process of a function calling itself directly or indirectlyBase case
- Provided solution to the simplest case. Continue solving until base case is reached.Stack overflow
- Error occurs when base case never reached or not definedDirect
- Function explicitly calls itselfIndrect
- Function calls another function (which may call another) before it is called againTailed
- When function calls itself as the last thing in the functionDisadvantages
- Larger space and time requirementsAdvantages
- Simple, clean, best for tree traversalsExample: 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