Skip to content

Instantly share code, notes, and snippets.

@mka142
Created April 5, 2022 15:04
Show Gist options
  • Save mka142/e836118e5c38befcfb2a17923dbfbd3a to your computer and use it in GitHub Desktop.
Save mka142/e836118e5c38befcfb2a17923dbfbd3a to your computer and use it in GitHub Desktop.
encode and decode from decimal system to alphabet numeric system and reverse
class numCode:
characters = list('abcdefghijklmnopqrstuvwxyz')
def __init__(self,**kwargs):
self.characters = list('abcdefghijklmnopqrstuvwxyz')
if 'characters' in kwargs.keys():
self.characters = kwargs['characters']
@classmethod
def encode(cls,i):
calosc = int(i / len(cls.characters))
reszta = i % len(cls.characters)
if calosc > 0:
return cls.encode(calosc) + cls.characters[reszta]
else:
return cls.characters[reszta]
@classmethod
def decode(cls,i):
number = 0
array = list(i)
array.reverse()
for index,char in enumerate(array):
number += len(cls.characters)**index *(cls.characters.index(char))
return number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment