Created
March 19, 2021 10:30
-
-
Save ahojukka5/201fe5194d6415a10c6d4953262bbab6 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
import ctypes | |
import os | |
import subprocess | |
import tempfile | |
__code__ = """ | |
#include <stdbool.h> | |
bool check(unsigned long long int n) { | |
int sum = 0; | |
while (n > 0) { | |
sum += n % 10; | |
n /= 10; | |
} | |
return sum % 7 == 0; | |
} | |
""" | |
path = tempfile.mkdtemp() | |
cfile = os.path.join(path, "lib.c") | |
sofile = os.path.join(path, "lib.so") | |
open(cfile, "w").write(__code__) | |
__cmd__ = "gcc -O3 -shared -o %s %s" % (sofile, cfile) | |
if os.environ.get("DEBUG"): | |
print(__cmd__) | |
subprocess.run(__cmd__.split(" ")) | |
lib = ctypes.CDLL(sofile) | |
lib.check.restype = ctypes.c_bool | |
lib.check.argtypes = [ctypes.c_ulonglong] | |
check = lib.check | |
if __name__ == "__main__": | |
print(check(14)) # False | |
print(check(16)) # True | |
print(check(123)) # False | |
print(check(777)) # True | |
print(check(9999999)) # True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment