-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMod62.py
More file actions
109 lines (97 loc) · 2.94 KB
/
Mod62.py
File metadata and controls
109 lines (97 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import math
class Mod62:
"""
URL Shortener like functions.
"""
def __init__(self):
self.anahtar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
# kodlama fonksiyonu tanımla
def encode(self,girdi):
"""
Encode a value using the mod62 algorithm.
:param girdi: The value to encode. Must be an integer.
:return: The encoded value. Return type string.
"""
# bölüneni tanımla
if type(girdi) != int:
girdi = 46999
bolunen = girdi
# böleni tanımla
bolen = len(self.anahtar)
# kalanları tanımla
kalanlar = []
if bolunen ==0:
kalanlar.append(bolunen)
else:
while(bolunen > 0):
kalan = bolunen % bolen
bolum = math.floor(bolunen / bolen)
kalanlar.append(kalan)
bolunen = bolum
# karakterleri tanımla
i = kalanlar.__len__()
degerA = ""
cikti = ""
while(i > 0):
i -= 1
degerA = self.anahtar[kalanlar[i]]
# karşılık gelen değerş çıktıya ekle
if cikti =="":
cikti = degerA
else:
cikti = cikti + degerA
# çıktıyı döndür
return cikti
# çözümleme fonsiyonu tanımla
def decode(self,girdi):
"""
Decode a string using the mod62 algorithm.
:param girdi: The string to decode. Must be a string.
:return: The decoded value. Return type integer.
"""
cikti = ""
# karakter setini objeye döndür
# böleni tanımla
bolen = len(self.anahtar)
# karakter seti dizisini döndür
anahtar = self._flip(self.anahtar)
# karakterleri tanımla
if type(girdi) != str:
girdi = "ba"
girdi = girdi[::-1]
# sayıyı hesapla
i = 0
while(i < girdi.__len__()):
kalan = anahtar[girdi[i]]
taban = int(math.pow(bolen,i))
# karşılık gelen değeri çıktıya ekle
bolunen = kalan * taban
if cikti == "":
cikti = bolunen
else:
cikti = cikti + bolunen
i += 1
# çıktıyı döndür
return cikti
def _flip(self,yazi):
"""
Flip a string.
:param yazi: The string to flip. Must be a string.
:return: The flipped string. Return type dictionary.
"""
i = len(yazi)
sozluk = dict()
while i > 0:
i -=1
sozluk[yazi[i]]= i
return sozluk
# örnek kullanım
if __name__ == "__main__":
# obje oluşturumu
obje = Mod62()
# kodlama işlevi kullanımı
print(obje.encode(46999))
print(type(obje.encode(46999)))
# çözümleme işlevi kullanımı
print(obje.decode("ba"))
print(type(obje.decode("ba")))