Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Last active June 19, 2017 16:14
Show Gist options
  • Save tyler-austin/ed243385162a6c7179d30ed1fa7338e5 to your computer and use it in GitHub Desktop.
Save tyler-austin/ed243385162a6c7179d30ed1fa7338e5 to your computer and use it in GitHub Desktop.
Code Fights - circleOfNumbers

Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighbouring numbers is equal (note that 0 and n - 1 are neighbouring, too).

Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.

Example

For n = 10 and firstNumber = 2, the output should be
circleOfNumbers(n, firstNumber) = 7.

Input/Output

  • [time limit] 4000ms (py3)

  • [input] integer n

    A positive even integer.

    Guaranteed constraints:

    4 ≤ n ≤ 20.
    
  • [input] integer firstNumber

    Guaranteed constraints:

     0 ≤ firstNumber ≤ n - 1.
    
  • [output] integer

def circle_of_numbers(n, first_num):
return int((n / 2 + first_num) % n)
import unittest
from circle_of_numbers import circle_of_numbers
class MyTestCase(unittest.TestCase):
def test_1(self):
n = 10
first_number = 2
solution = 7
result = circle_of_numbers(n, first_number)
self.assertEqual(result, solution)
def test_2(self):
n = 10
first_number = 7
solution = 2
result = circle_of_numbers(n, first_number)
self.assertEqual(result, solution)
def test_3(self):
n = 4
first_number = 1
solution = 3
result = circle_of_numbers(n, first_number)
self.assertEqual(result, solution)
def test_4(self):
n = 6
first_number = 3
solution = 0
result = circle_of_numbers(n, first_number)
self.assertEqual(result, solution)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment