Merge branch 'master' of git://github.com/gerroo/Python

This commit is contained in:
Harshil 2018-10-31 08:19:15 +01:00
commit 485f93fc26
8 changed files with 91 additions and 35 deletions

18
Maths/3n+1.py Normal file
View File

@ -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()

12
Maths/FindMin.py Normal file
View File

@ -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()

11
ciphers/base64_cipher.py Normal file
View File

@ -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()

29
simple_client/client.py Normal file
View File

@ -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')))

21
simple_client/server.py Normal file
View File

@ -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()

View File

@ -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')))

View File

@ -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()