<commit_before>import unittest
import utils

rotate_to_self = {0, 1, 8}
rotate_to_other = {2, 5, 6, 9}
invalid = {3, 4, 7}


def is_good(num):
    valid = False
    q = num
    while q:
        q, r = divmod(q, 10)
        if r in invalid:
            return False
        if r in rotate_to_other:
            valid = True
    return valid


# O(n) time. O(1) space. Math, hash table.
class Solution:
    def rotatedDigits(self, n):
        """
        :type n: int
        :rtype: int
        """
        count = 0
        for num in range(1, n + 1):
            if is_good(num):
                count += 1
        return count


class Test(unittest.TestCase):
    def test(self):
        cases = utils.load_test_json(__file__).test_cases

        for case in cases:
            args = str(case.args)
            actual = Solution().rotatedDigits(**case.args.__dict__)
            self.assertEqual(case.expected, actual, msg=args)


if __name__ == '__main__':
    unittest.main()<commit_msg>788. Rotated Digits<commit_after>import unittest

import utils

rotate_to_other = [False, False, True, False, False, True, True, False, False, True]
invalid = [False, False, False, True, True, False, False, True, False, False]


def is_good(num):
    valid = False
    while num:
        num, r = divmod(num, 10)
        if invalid[r]:
            return False
        if rotate_to_other[r]:
            valid = True
    return valid


# O(n) time. O(1) space. Math.
class Solution:
    def rotatedDigits(self, n: int) -> int:
        return sum(is_good(num) for num in range(1, n + 1))


class Test(unittest.TestCase):
    def test(self):
        utils.test(self, __file__, Solution)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.11536231884057971, 'fraction_numerical': 0.014492753623188406, 'mean_word_length': 2.196296296296296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5071722', 'n_tokens_mistral': 595, 'n_tokens_neox': 566, 'n_words': 173}