Skip to content

Editing base64, Adding average file, Editing find_lcm #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Maths/average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def average(nums):
sum = 0
n = 0
for x in nums:
sum += x
n += 1
avg = sum / n
print(avg)

def main():
average([2, 4, 6, 8, 20, 50, 70])

if __name__ == '__main__':
main()
7 changes: 4 additions & 3 deletions Maths/find_lcm.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
def find_lcm(num_1, num_2):
max = num_1 if num_1 > num_2 else num_2
lcm = max
while (True):
if ((max % num_1 == 0) and (max % num_2 == 0)):
if ((lcm % num_1 == 0) and (lcm % num_2 == 0)):
break
max += 1
return max
lcm += max
return lcm


def main():
Expand Down
65 changes: 59 additions & 6 deletions ciphers/base64_cipher.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
import base64
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():
inp = input('->')
encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object)
b64encoded = base64.b64encode(encoded) #b64encoded the encoded string
print(b64encoded)
print(base64.b64decode(b64encoded).decode('utf-8'))#decoded it
print(encodeBase64("WELCOME to base64 encoding"))
print(decodeBase64(encodeBase64("WELCOME to base64 encoding")))


if __name__ == '__main__':
main()