105k views
4 votes
Python Programming Only.

Create a function that takes a numeric input representing the degrees of a rotation and adjusts it to remove unnecessary rotations if more than a full rotation is entered. For example, an input of 100 should just return 100, but an input of 460 should return 100 to eliminate the full 360-degree rotation. Using the unittest library, create tests to check these two examples (100 -> 100, 460 -> 100).

User Ethan Fang
by
7.9k points

1 Answer

6 votes

Final answer:

To create a function in Python that removes unnecessary rotations, use the modulo operator (%) to get the remainder of the input divided by 360. This will give the adjusted value of the degrees within a range of 0 to 359. Use the unittest library to create tests that verify the function's correctness for specific inputs.

Step-by-step explanation:

Function: remove_rotations

To create a function in Python that removes unnecessary rotations, we need to use the modulo operator (%) to get the remainder of the input divided by 360. This will give us the adjusted value of the degrees within a range of 0 to 359. Here's an example:

def remove_rotations(degrees):
adjusted_degrees = degrees % 360
return adjusted_degrees

print(remove_rotations(100))
print(remove_rotations(460))

Tests:

import unittest

class TestRemoveRotations(unittest.TestCase):
def test_remove_rotations_100(self):
self.assertEqual(remove_rotations(100), 100)

def test_remove_rotations_460(self):
self.assertEqual(remove_rotations(460), 100)

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

This function takes the input degrees and calculates the remainder after dividing by 360. It then returns the adjusted degrees within the range of 0 to 359. The tests verify that the function correctly removes unnecessary rotations for the given inputs.

User Blackhex
by
8.5k points