Skip to content

Instantly share code, notes, and snippets.

@sina-programer
Created June 26, 2025 09:31
Show Gist options
  • Save sina-programer/7eb41480fc9b2e50075e968638a78464 to your computer and use it in GitHub Desktop.
Save sina-programer/7eb41480fc9b2e50075e968638a78464 to your computer and use it in GitHub Desktop.
project numbers in [a, b] as a cycle. (a-1 => b, b+1 => a)
def transform(x, a, b):
''' projects x in [a, b] like a cycle. a-1=>b, b+1=>a '''
n = b - a + 1
return (x - a) % n + a
if __name__ == "__main__":
a = 1
b = 10
assert all(map(lambda x: x == transform(x, a, b), range(a, b+1)))
assert transform(a-1, a, b) == b
assert transform(a-2, a, b) == b-1
assert transform(b+1, a, b) == a
assert transform(b+2, a, b) == a+1
print('All tests passed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment