Skip to content

Commit

Permalink
feat(test) create 3 test cases and modified function to pass them
Browse files Browse the repository at this point in the history
  • Loading branch information
blepati committed Apr 18, 2017
1 parent 4e6fc54 commit bfb62ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 8 additions & 5 deletions oddavg/odd_avg.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Create a function called `odd_average` that takes a list of numbers as parameter
# and returns the average value of the odd numbers in the list
# Create basic unit tests for it with at least 3 different test cases
nums = [15, 2, 3, 4, 3, 6]
nums = [1, 2, 3, 4, 5]

def odd_average(nums):
odds = 0
odd_num = []
for num in nums:
if num%2 == 1:
odds += num
odd_num.append(num)
return odds//len(odd_num)
try:
if num%2 == 1:

This comment has been minimized.

Copy link
@gygabor

gygabor Apr 18, 2017

It should be whitespaces around the operator according the style guide.

odds += num
odd_num.append(num)
return odds//len(odd_num)

This comment has been minimized.

Copy link
@gygabor

gygabor Apr 18, 2017

The loop runs only one time. After the return it returns to where it called. You should return the average outside of the loop.
In this case the function returns with integer but the average could be float.

except ZeroDivisionError:
return 0

print(odd_average(nums))
19 changes: 19 additions & 0 deletions oddavg/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from odd_avg import odd_average

class AverageMethod(unittest.TestCase):

def test_average(self):
list_of_nums = [9, 9, 10, 9, 3]
self.assertEqual(odd_average(list_of_nums), 9)

This comment has been minimized.

Copy link
@gygabor

gygabor Apr 18, 2017

The good result is 7.5 not the 9. The test doesn't break because the odd_average() function always returns with the (first element of the list) / 1 -> first element if the first element is odd.


def test_odds(self):
list_of_nums = [4, 6, 8]
self.assertEqual(odd_average(list_of_nums), 0)

def test_empty_list(self):
list_of_nums = [ ]
self.assertEqual(odd_average(list_of_nums), None)

if __name__ == '__main__':
unittest.main()

0 comments on commit bfb62ac

Please sign in to comment.