I've been making a function where a number x is outputted as a string of numbers that decrease by 0.5 as long as x is positive.
(eg. x = 3.3; output = ''3.3, 2.8, 2.3, 1.8, 1.3, 0.8, 0.3')
Here is the code.
# reductions.py
## Receives a number: x
## Outputs a string of numbers that starts with the number and
## decreases by 0.5 as long as the x value is positive
## NOTE: If it receives a negative number, it returns a 0
## Example 1:
## Input: 4
## Output: '4, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0'
## Example 2:
## Input: -3
## Output: '0'
## WARNING:
## do not include a comma and space at the end of the string
import math
# Define generator function
def reductions_gen_helper(x):
""" Generates a string starting with x that decreases by 0.5 as long as
x is positive """
# Calculate string
while x >= 0:
yield x # Keep the values
str(x).strip("")
x -= 0.5
x = math.ceil(x*10)/10
# Define used function
def reductions(x):
""" Returns a string starting with that x that decreases by 0.5 as long as
x is positive """
# Check for negative input
if x <= 0:
return('0')
else:
return ", ".join(str(x) for x in reductions_gen_helper(x))
And here is the unit test for it.
# Testreductions.py
# Import Statements
import unittest
import reductions
class KnownValues(unittest.TestCase):
# Formula for unittest method is:
# test_functionName_testDescription
def test_reductionsForEvenValues(self):
# Capture the results of the function
result = reductions.reductions(4)
# Check for expected output
self.assertEqual('4, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0',
result)
def test_reductionsForOddFloatValues(self):
# Capture the results of the function
result = reductions.reductions(3.3)
# Check for expected output
self.assertEqual('3.3, 2.8, 2.3, 1.8, 1.3, 0.8, 0.3',
result)
def test_reductionsForPositiveOddValues(self):
# Capture the results of the function
result = reductions.reductions(5)
# Check for expected output
self.assertEqual('5, 4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0',
result)
def test_reductionsForPositiveFloatValues(self):
# Capture the results of the function
result = reductions.reductions(4.4)
# Check for expected output
self.assertEqual('4.4, 3.9, 3.4, 2.9, 2.4, 1.9, 1.4, 0.9, 0.4',
result)
def test_reductionsForNegativeNumber(self):
# Capture the results of the function
result = reductions.reductions(-3)
# Check for expected output
self.assertEqual('0', result)
def test_reductionsForZero(self):
# Capture the results of the function
result = reductions.reductions(0)
# Check for expected output
self.assertEqual('0', result)
# Run the tests
if __name__ == '__main__':
unittest.main()
When I run the tests, I get these errors.
..FF..
======================================================================
FAIL: test_reductionsForOddFloatValues (__main__.KnownValues)
----------------------------------------------------------------------
Traceback (most recent call last):
File "M:\Digital Media\Winikka\Programming 1\_Student Folders\Period4\let147\Python Challenge Projects\WhileLoopsChallengeSpring2016-master\TestReductions.py", line 23, in test_reductionsForOddFloatValues
result)
AssertionError: '3.3, 2.8, 2.3, 1.8, 1.3, 0.8, 0.3' != '3.3, 2.8, 2.3, 1.8, 1.3, 0.8, 0.4, 0.0'
- 3.3, 2.8, 2.3, 1.8, 1.3, 0.8, 0.3
? ^
+ 3.3, 2.8, 2.3, 1.8, 1.3, 0.8, 0.4, 0.0
? ^^^^^^
======================================================================
FAIL: test_reductionsForPositiveFloatValues (__main__.KnownValues)
----------------------------------------------------------------------
Traceback (most recent call last):
File "M:\Digital Media\Winikka\Programming 1\_Student Folders\Period4\let147\Python Challenge Projects\WhileLoopsChallengeSpring2016-master\TestReductions.py", line 37, in test_reductionsForPositiveFloatValues
result)
AssertionError: '4.4, 3.9, 3.4, 2.9, 2.4, 1.9, 1.4, 0.9, 0.4' != '4.4, 3.9, 3.4, 2.9, 2.4, 1.9, 1.4, 0.9, 0.4, 0.0'
- 4.4, 3.9, 3.4, 2.9, 2.4, 1.9, 1.4, 0.9, 0.4
+ 4.4, 3.9, 3.4, 2.9, 2.4, 1.9, 1.4, 0.9, 0.4, 0.0
? +++++
----------------------------------------------------------------------
Ran 6 tests in 0.062s
FAILED (failures=2)
Is there any way to fix this?
Aucun commentaire:
Enregistrer un commentaire