Created
January 23, 2024 05:47
-
-
Save aaronsdevera/0370309f601c32e81ec843f85435ab31 to your computer and use it in GitHub Desktop.
base string utility in python
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
#! /usr/bin/python3 | |
import sys | |
import os | |
INPUT = sys.argv[1] | |
RAW_STRING = None | |
if os.path.isfile(INPUT): | |
RAW_STRING = open(INPUT).read().strip() | |
else: | |
RAW_STRING = INPUT | |
def to_base3(n): | |
if n == 0: | |
return '0' | |
nums = [] | |
while n: | |
n, r = divmod(n, 3) | |
nums.append(str(r)) | |
return ''.join(reversed(nums)) | |
def string_to_base3(s): | |
return ' '.join(to_base3(ord(c)) for c in s) | |
print(string_to_base3(RAW_STRING)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment