Created
April 28, 2016 10:42
-
-
Save PlugaruT/2209b500a03eb9aa2c58c2d9e080186a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Assignment ### | |
# | |
# Your assignment is to implement the | |
# following function: `find_next_prime`. | |
# As the name states, given the number `n` the | |
# function should return the next closest prime. | |
# | |
# Examples: | |
# * `find_next_prime(6)` should return 7. | |
# * `find_next_prime(10)` should return 11. | |
# * `find_next_prime(11)` should return 13. | |
# | |
# You can use whatever you want (data structures, | |
# language features, etc). | |
# | |
# Unit tests would be a plus. | |
# | |
### End Assignment ### | |
import unittest | |
def find_prime_in_range(min, max): | |
for number in range(min, max): | |
for i in range(2, number): | |
if number % i == 0: | |
break | |
else: | |
return number | |
return None | |
def find_next_prime(n): | |
return find_prime_in_range(n + 1, 2 * (n + 1)) | |
class FindNextPrimeTestCase(unittest.TestCase): | |
def test_number_6(self): | |
self.assertEqual(find_next_prime(6), 7) | |
def test_number_10(self): | |
self.assertEqual(find_next_prime(10), 11) | |
def test_number_11(self): | |
self.assertEqual(find_next_prime(11), 13) | |
if __name__ == '__main__': | |
print find_next_prime(6) | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment