Skip to content

Commit bfdc0a6

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
Synchronizing with source
2 parents 3ecb193 + a14ea99 commit bfdc0a6

File tree

104 files changed

+5472
-533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+5472
-533
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# server
2+
3+
import socket # Import socket module
4+
5+
port = 60000 # Reserve a port for your service.
6+
s = socket.socket() # Create a socket object
7+
host = socket.gethostname() # Get local machine name
8+
s.bind((host, port)) # Bind to the port
9+
s.listen(5) # Now wait for client connection.
10+
11+
print 'Server listening....'
12+
13+
while True:
14+
conn, addr = s.accept() # Establish connection with client.
15+
print 'Got connection from', addr
16+
data = conn.recv(1024)
17+
print('Server received', repr(data))
18+
19+
filename='mytext.txt'
20+
f = open(filename,'rb')
21+
l = f.read(1024)
22+
while (l):
23+
conn.send(l)
24+
print('Sent ',repr(l))
25+
l = f.read(1024)
26+
f.close()
27+
28+
print('Done sending')
29+
conn.send('Thank you for connecting')
30+
conn.close()
31+
32+
33+
# client side server
34+
35+
import socket # Import socket module
36+
37+
s = socket.socket() # Create a socket object
38+
host = socket.gethostname() # Get local machine name
39+
port = 60000 # Reserve a port for your service.
40+
41+
s.connect((host, port))
42+
s.send("Hello server!")
43+
44+
with open('received_file', 'wb') as f:
45+
print 'file opened'
46+
while True:
47+
print('receiving data...')
48+
data = s.recv(1024)
49+
print('data=%s', (data))
50+
if not data:
51+
break
52+
# write data to a file
53+
f.write(data)
54+
55+
f.close()
56+
print('Successfully get the file')
57+
s.close()
58+
print('connection closed')
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
File transfer protocol used to send and receive files using FTP server.
3+
Use credentials to provide access to the FTP client
4+
5+
Note: Do not use root username & password for security reasons
6+
Create a seperate user and provide access to a home directory of the user
7+
Use login id and password of the user created
8+
cwd here stands for current working directory
9+
"""
10+
11+
from ftplib import FTP
12+
ftp = FTP('xxx.xxx.x.x') """ Enter the ip address or the ___domain name here """
13+
ftp.login(user='username', passwd='password')
14+
ftp.cwd('/Enter the directory here/')
15+
16+
"""
17+
The file which will be received via the FTP server
18+
Enter the ___location of the file where the file is received
19+
"""
20+
21+
def ReceiveFile():
22+
FileName = 'example.txt' """ Enter the ___location of the file """
23+
LocalFile = open(FileName, 'wb')
24+
ftp.retrbinary('RETR ' + filename, LocalFile.write, 1024)
25+
ftp.quit()
26+
LocalFile.close()
27+
28+
"""
29+
The file which will be sent via the FTP server
30+
The file send will be send to the current working directory
31+
"""
32+
33+
def SendFile():
34+
FileName = 'example.txt' """ Enter the name of the file """
35+
ftp.storbinary('STOR ' + FileName, open(FileName, 'rb'))
36+
ftp.quit()

Graphs/a_star.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
grid = [[0, 1, 0, 0, 0, 0],
3+
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
4+
[0, 1, 0, 0, 0, 0],
5+
[0, 1, 0, 0, 1, 0],
6+
[0, 0, 0, 0, 1, 0]]
7+
8+
'''
9+
heuristic = [[9, 8, 7, 6, 5, 4],
10+
[8, 7, 6, 5, 4, 3],
11+
[7, 6, 5, 4, 3, 2],
12+
[6, 5, 4, 3, 2, 1],
13+
[5, 4, 3, 2, 1, 0]]'''
14+
15+
init = [0, 0]
16+
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
17+
cost = 1
18+
19+
#the cost map which pushes the path closer to the goal
20+
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
21+
for i in range(len(grid)):
22+
for j in range(len(grid[0])):
23+
heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
24+
if grid[i][j] == 1:
25+
heuristic[i][j] = 99 #added extra penalty in the heuristic map
26+
27+
28+
#the actions we can take
29+
delta = [[-1, 0 ], # go up
30+
[ 0, -1], # go left
31+
[ 1, 0 ], # go down
32+
[ 0, 1 ]] # go right
33+
34+
35+
#function to search the path
36+
def search(grid,init,goal,cost,heuristic):
37+
38+
closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
39+
closed[init[0]][init[1]] = 1
40+
action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid
41+
42+
x = init[0]
43+
y = init[1]
44+
g = 0
45+
f = g + heuristic[init[0]][init[0]]
46+
cell = [[f, g, x, y]]
47+
48+
found = False # flag that is set when search is complete
49+
resign = False # flag set if we can't find expand
50+
51+
while not found and not resign:
52+
if len(cell) == 0:
53+
resign = True
54+
return "FAIL"
55+
else:
56+
cell.sort()#to choose the least costliest action so as to move closer to the goal
57+
cell.reverse()
58+
next = cell.pop()
59+
x = next[2]
60+
y = next[3]
61+
g = next[1]
62+
f = next[0]
63+
64+
65+
if x == goal[0] and y == goal[1]:
66+
found = True
67+
else:
68+
for i in range(len(delta)):#to try out different valid actions
69+
x2 = x + delta[i][0]
70+
y2 = y + delta[i][1]
71+
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
72+
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
73+
g2 = g + cost
74+
f2 = g2 + heuristic[x2][y2]
75+
cell.append([f2, g2, x2, y2])
76+
closed[x2][y2] = 1
77+
action[x2][y2] = i
78+
invpath = []
79+
x = goal[0]
80+
y = goal[1]
81+
invpath.append([x, y])#we get the reverse path from here
82+
while x != init[0] or y != init[1]:
83+
x2 = x - delta[action[x][y]][0]
84+
y2 = y - delta[action[x][y]][1]
85+
x = x2
86+
y = y2
87+
invpath.append([x, y])
88+
89+
path = []
90+
for i in range(len(invpath)):
91+
path.append(invpath[len(invpath) - 1 - i])
92+
print "ACTION MAP"
93+
for i in range(len(action)):
94+
print action[i]
95+
96+
return path
97+
98+
a = search(grid,init,goal,cost,heuristic)
99+
for i in range(len(a)):
100+
print a[i]
101+

0 commit comments

Comments
 (0)