Skip to content

Commit

Permalink
Adding tests for the recursive power function
Browse files Browse the repository at this point in the history
  • Loading branch information
nbro committed Jan 18, 2017
1 parent 84c47c1 commit 680d09b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
14 changes: 7 additions & 7 deletions ands/algorithms/recursion/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"""


def _count(o: object, ls, index: int) -> int:
def _count(elem: object, ls, index: int) -> int:
if index < len(ls):
if ls[index] == o:
return 1 + _count(o, ls, index + 1)
if ls[index] == elem:
return 1 + _count(elem, ls, index + 1)
else:
return _count(o, ls, index + 1)
return _count(elem, ls, index + 1)
return 0


def count(o: object, ls):
"""Counts how many times `o` appears in the list or tuple `ls`."""
return _count(o, ls, 0)
def count(elem: object, ls) -> int:
"""Counts how many times `elem` appears in the list or tuple `ls`."""
return _count(elem, ls, 0)
31 changes: 13 additions & 18 deletions ands/algorithms/recursion/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
"""
Author: Nelson Brochado
Raising `a` to the `k` using recursion, i.e., a<sup>k</sup> = b.
Created: 2015
Updated: 18/01/2017
Raising an integer `a` to the `k >= 0` using recursion, i.e., a<sup>k</sup> = b.
"""


def power_r(base, power, show_steps=False):
"""Base case: a<sup>0</sup> = 1.
def power(base: int, p: int) -> int:
"""Assumes inputs are integers and that the power `p >= 0`.
Base case: a<sup>0</sup> = 1.
Recursive step: a<sup>n + 1</sup> = a<sup>n</sup> * a."""
if power == 0: # Base case
if show_steps:
print(base, "^{0} = 1", sep="")
assert p >= 0

if p == 0:
return 1
else: # recursive step
if show_steps:
print(base, "^{", power, "} = ", base, " * ",
base, "^{", power - 1, "}", sep="")
return base * power_r(base, power - 1, show_steps)


if __name__ == "__main__":
for i in range(5):
for j in range(5):
print(power_r(i, j))
print("-" * 18)
else:
return base * power(base, p - 1)
50 changes: 50 additions & 0 deletions tests/algorithms/recursion/test_power.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Author: Nelson Brochado
Created: 18/01/2017
"""

import unittest
from random import randint

from ands.algorithms.recursion.power import power


class TestRecursivePower(unittest.TestCase):
# Testing raising to power of 0.

def test_base_0_power_0(self):
self.assertEqual(power(0, 0), 1)

def test_base_minus_1_power_0(self):
self.assertEqual(power(-1, 0), 1)

def test_base_1_power_0(self):
self.assertEqual(power(1, 0), 1)

def test_base_random_base_power_0(self):
self.assertEqual(power(randint(2, 100), 0), 1)
self.assertEqual(power(randint(-100, -2), 0), 1)

def test_base_0_power_1(self):
self.assertEqual(power(0, 1), 0)

def test_base_1_power_1(self):
self.assertEqual(power(1, 1), 1)

def test_base_minus_1_power_1(self):
self.assertEqual(power(-1, 1), -1)

def test_random_base_power_1(self):
a = randint(2, 100)
b = randint(-100, -2)
self.assertEqual(power(a, 1), a)
self.assertEqual(power(b, 1), b)

def test_random_base_random_positive_power(self):
b = randint(-100, 100)
p = randint(2, 100)
self.assertEqual(power(b, p), b ** p)

0 comments on commit 680d09b

Please sign in to comment.