mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
c92b02cfa3
* avrage.py calculate and print the avrage of number list. * Update base64_cipher.py encoding and decoding base64 without any module. * Update and rename avrage.py to average.py * update find_lcm algorithm I made find_lcm more efficient form O(num1*num2) to O(min{num1,num2}).
65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
def encodeBase64(text):
|
|
base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
|
|
r = "" #the result
|
|
c = 3 - len(text) % 3 #the length of padding
|
|
p = "=" * c #the padding
|
|
s = text + "\0" * c #the text to encode
|
|
|
|
i = 0
|
|
while i < len(s):
|
|
if i > 0 and ((i / 3 * 4) % 76) == 0:
|
|
r = r + "\r\n"
|
|
|
|
n = (ord(s[i]) << 16) + (ord(s[i+1]) << 8 ) + ord(s[i+2])
|
|
|
|
n1 = (n >> 18) & 63
|
|
n2 = (n >> 12) & 63
|
|
n3 = (n >> 6) & 63
|
|
n4 = n & 63
|
|
|
|
r += base64chars[n1] + base64chars[n2] + base64chars[n3] + base64chars[n4]
|
|
i += 3
|
|
|
|
return r[0: len(r)-len(p)] + p
|
|
|
|
def decodeBase64(text):
|
|
base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
s = ""
|
|
|
|
for i in text:
|
|
if i in base64chars:
|
|
s += i
|
|
c = ""
|
|
else:
|
|
if i == '=':
|
|
c += '='
|
|
|
|
p = ""
|
|
if c == "=":
|
|
p = 'A'
|
|
else:
|
|
if c == "==":
|
|
p = "AA"
|
|
|
|
r = ""
|
|
s = s + p
|
|
|
|
i = 0
|
|
while i < len(s):
|
|
n = (base64chars.index(s[i]) << 18) + (base64chars.index(s[i+1]) << 12) + (base64chars.index(s[i+2]) << 6) +base64chars.index(s[i+3])
|
|
|
|
r += chr((n >> 16) & 255) + chr((n >> 8) & 255) + chr(n & 255)
|
|
|
|
i += 4
|
|
|
|
return r[0: len(r) - len(p)]
|
|
|
|
def main():
|
|
print(encodeBase64("WELCOME to base64 encoding"))
|
|
print(decodeBase64(encodeBase64("WELCOME to base64 encoding")))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|