Back to Freecodecamp

Challenge 238: Digit Rotation Escape

curriculum/challenges/english/blocks/daily-coding-challenges-python/69b559d2903b9e4afe9075f8.md

latest1.4 KB
Original Source

--description--

Given a positive integer, determine if it, or any of its rotations, is evenly divisible by its digit count.

A rotation means to move the first digit to the end. For example, after 1 rotation, 123 becomes 231.

  • Check rotation 0 (the given number) first.
  • Given numbers won't contain any zeros.
  • Return the first rotation number if one is found, or "none" if not.

--hints--

get_rotation(123) should return 0.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(123), 0)`)
}})

get_rotation(13579) should return 3.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(13579), 3)`)
}})

get_rotation(24681) should return "none".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(24681), "none")`)
}})

get_rotation(84138789345) should return 6.

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(84138789345), 6)`)
}})

--seed--

--seed-contents--

py
def get_rotation(n):

    return n

--solutions--

py
def get_rotation(n):
    s = str(n)
    digit_count = len(s)
    current = s

    for i in range(digit_count):
        if int(current) % digit_count == 0:
            return i
        current = current[1:] + current[0]

    return "none"