diff --git a/Maths/3n+1.py b/Maths/3n+1.py new file mode 100644 index 000000000..894820ec5 --- /dev/null +++ b/Maths/3n+1.py @@ -0,0 +1,18 @@ +def main(): + def n31(a):# a = initial number + c = 0 + l = [a] + while a != 1: + if a % 2 == 0:#if even divide it by 2 + a = a // 2 + elif a % 2 == 1:#if odd 3n+1 + a = 3*a +1 + c += 1#counter + l += [a] + print(a)#optional print + print("It took {0} steps.".format(c))#optional finish + return l , c + print(n31(43)) + +if __name__ == '__main__': + main() diff --git a/Maths/FindMin.py b/Maths/FindMin.py new file mode 100644 index 000000000..19371859c --- /dev/null +++ b/Maths/FindMin.py @@ -0,0 +1,12 @@ +def main(): + def findMin(x): + minNum = x[0] + for i in x: + if minNum > i: + minNum = i + return minNum + + print(findMin([0,1,2,3,4,5,-3,24,-56])) # = -56 + +if __name__ == '__main__': + main() diff --git a/ciphers/base64_cipher.py b/ciphers/base64_cipher.py new file mode 100644 index 000000000..975f24d6b --- /dev/null +++ b/ciphers/base64_cipher.py @@ -0,0 +1,11 @@ +import base64 + +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 + +if __name__ == '__main__': + main() diff --git a/simple_client_server/README.md b/simple_client/README.md similarity index 100% rename from simple_client_server/README.md rename to simple_client/README.md diff --git a/simple_client/client.py b/simple_client/client.py new file mode 100644 index 000000000..db162f43c --- /dev/null +++ b/simple_client/client.py @@ -0,0 +1,29 @@ +# client.py + +import socket + +HOST, PORT = '127.0.0.1', 1400 + +s = socket.socket( + + socket.AF_INET, # ADDRESS FAMILIES + #Name Purpose + #AF_UNIX, AF_LOCAL Local communication + #AF_INET IPv4 Internet protocols + #AF_INET6 IPv6 Internet protocols + #AF_APPLETALK Appletalk + #AF_BLUETOOTH Bluetooth + + + socket.SOCK_STREAM # SOCKET TYPES + #Name Way of Interaction + #SOCK_STREAM TCP + #SOCK_DGRAM UDP +) +s.connect((HOST, PORT)) + +s.send('Hello World'.encode('ascii'))#in UDP use sendto() +data = s.recv(1024)#in UDP use recvfrom() + +s.close()#end the connection +print(repr(data.decode('ascii'))) diff --git a/simple_client/server.py b/simple_client/server.py new file mode 100644 index 000000000..c23075608 --- /dev/null +++ b/simple_client/server.py @@ -0,0 +1,21 @@ +# server.py + +import socket + +HOST, PORT = '127.0.0.1', 1400 + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#refer to client.py +s.bind((HOST, PORT)) +s.listen(1)#listen for 1 connection + +conn, addr = s.accept()#start the actual data flow + +print('connected to:', addr) + +while 1: + data = conn.recv(1024).decode('ascii')#receive 1024 bytes and decode using ascii + if not data: + break + conn.send((data + ' [ addition by server ]').encode('ascii')) + +conn.close() diff --git a/simple_client_server/client.py b/simple_client_server/client.py deleted file mode 100644 index c91e6233a..000000000 --- a/simple_client_server/client.py +++ /dev/null @@ -1,14 +0,0 @@ -# client.py - -import socket - -HOST, PORT = '127.0.0.1', 1400 - -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -s.connect((HOST, PORT)) - -s.send(b'Hello World') -data = s.recv(1024) - -s.close() -print(repr(data.decode('ascii'))) diff --git a/simple_client_server/server.py b/simple_client_server/server.py deleted file mode 100644 index c6b661d99..000000000 --- a/simple_client_server/server.py +++ /dev/null @@ -1,21 +0,0 @@ -# server.py - -import socket - -HOST, PORT = '127.0.0.1', 1400 - -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -s.bind((HOST, PORT)) -s.listen(1) - -conn, addr = s.accept() - -print('connected to:', addr) - -while 1: - data = conn.recv(1024) - if not data: - break - conn.send(data + b' [ addition by server ]') - -conn.close()