Return Functions Tests
Each example assumes the students file name is example#.py replacing # with the actual example number. You can call the functions by importing the student file name of your choice.

Example 1 - Add Five

Students write an function named add_five() that takes an integer as a parameter and returns 5 plus the parameter value.

Grading Tests:

1
import unittest
2
import sys, io
3
​
4
class CodingRoomsUnitTests(unittest.TestCase):
5
​
6
def test_default_case(self):
7
import example1
8
self.assertEqual(10, example1.add_five(5))
9
self.assertEqual(5, example1.add_five(0))
10
self.assertEqual(11, example1.add_five(6))
11
12
if __name__ == '__main__':
13
unittest.main()
Copied!

Sample Solution:

example1.py
1
def add_five(number):
2
number += 5
3
return number
Copied!

Example 2 - Average

Students write a function that takes 5 values as parameters and returns the average value of the 5 values. The function must be named average() and it must have 5 parameters.
Example: Calling average(1, 5, 7, 4, 10) would return 5.4.
Note: Almost Equal assertion is used with a delta value as tolerance when comparing values.

Grading Tests:

1
import unittest
2
import sys, io
3
​
4
class CodingRoomsUnitTests(unittest.TestCase):
5
​
6
def test_default_case(self):
7
import example2
8
self.assertAlmostEqual(5.4, example2.average(1, 5, 7, 4, 10), delta=0.001)
9
self.assertAlmostEqual(9.6, example2.average(5, 9, 24, 6, 4), delta=0.001)
10
11
if __name__ == '__main__':
12
unittest.main()
Copied!

Sample Solution:

example2.py
1
def average(a, b, c, d, e):
2
sum = a + b + c + d + e
3
return sum / 5
Copied!

Example 3 - Repeats

Students write a function that takes a parameter. If the String parameter has a double letter (i.e. contains the same letter twice in a row) then it should return true. Otherwise, it should return false.
This function must be named has_repeat() and have a parameter. This function must return a boolean.

Grading Tests:

1
import unittest
2
import sys, io
3
​
4
class CodingRoomsUnitTests(unittest.TestCase):
5
​
6
def test_default_case(self):
7
import example3
8
self.assertTrue(example3.has_repeat("mississippi"))
9
self.assertFalse(example3.has_repeat("capsized"))
10
self.assertTrue(example3.has_repeat("mazzone"))
11
self.assertFalse(example3.has_repeat("this"))
12
13
if __name__ == '__main__':
14
unittest.main()
Copied!

Sample Solution:

example3.py
1
def has_repeat(word):
2
count = 0
3
for letter in word:
4
if letter == word[count-1]:
5
return True
6
count += 1
7
return False
Copied!
Last modified 2mo ago