@ -0,0 +1,63 @@ | |||||
#!/usr/bin/env python3 | |||||
''' PortScan v3 | |||||
----------- | |||||
This application scans for open ports on the designated system. It uses | |||||
multiprocessing to speed up this process. | |||||
''' | |||||
import socket | |||||
import subprocess | |||||
import sys | |||||
from datetime import datetime | |||||
from multiprocessing import Pool | |||||
def scan(port): | |||||
try: | |||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||||
result = sock.connect_ex((target_ip, port)) | |||||
if result == 0: | |||||
print("Port {}:\tOpen".format(port)) | |||||
sock.close() | |||||
except socket.gaierror: | |||||
print('Hostname could not be resolved.') | |||||
sys.exit(0) | |||||
except socket.error: | |||||
print("Couldn't connect to server.") | |||||
sys.exit(0) | |||||
except: | |||||
return | |||||
if __name__ == '__main__': | |||||
ports = list(range(1,4096)) | |||||
target = '' | |||||
try: | |||||
target = sys.argv[1] | |||||
except: | |||||
print("\nUsage:\t{} [target]\n\n\tScan for open ports on target machine.\n".format(sys.argv[0])) | |||||
sys.exit(0) | |||||
# Clear the screen | |||||
subprocess.call('clear', shell=True) | |||||
target_ip = socket.gethostbyname(target) | |||||
# Print a nice banner with information on which host we are about to scan | |||||
print("-" * 60) | |||||
print("Please wait, scanning remote host", target_ip) | |||||
print("-" * 60) | |||||
# Check what time the scan started | |||||
t1 = datetime.now() | |||||
with Pool(processes = 8) as p: | |||||
p.map(scan, ports) | |||||
# Checking the time again | |||||
t2 = datetime.now() | |||||
# Calculates the difference of time, to see how long it took to run the script | |||||
total = t2 - t1 | |||||
# Printing the information to screen | |||||
print('Scanning Completed in: ', total) |
@ -0,0 +1,46 @@ | |||||
class Stack: | |||||
def __init__ (self): | |||||
self.items = list() | |||||
def push (self, item): | |||||
self.items.append (item) | |||||
def pop (self): | |||||
if len(self.items) > 0: | |||||
return self.items.pop() | |||||
return ('Stack is empty') | |||||
def isEmpty (self): | |||||
return self.items == list() | |||||
def size (self): | |||||
return len(self.items) | |||||
def peek (self): | |||||
return self.items[0] | |||||
def printStack (self): | |||||
print(self.items) | |||||
if __name__ == '__main__': | |||||
my_stack = Stack() | |||||
my_stack.push(1) | |||||
my_stack.printStack() | |||||
my_stack.push(5) | |||||
my_stack.push(3) | |||||
my_stack.printStack() | |||||
print('Pop {} from stack'.format(my_stack.pop())) | |||||
my_stack.printStack() | |||||
print('Now stack size is {}'.format(my_stack.size())) | |||||
print('First element in stack is {}'.format(my_stack.peek())) | |||||
print('Pop {} from stack'.format(my_stack.pop())) | |||||
my_stack.printStack() | |||||
print('Pop {} from stack'.format(my_stack.pop())) | |||||
my_stack.printStack() | |||||
print('Now stack size is {}'.format(my_stack.size())) | |||||
print(my_stack.pop()) | |||||
@ -0,0 +1,37 @@ | |||||
from collections import defaultdict | |||||
class Graph: | |||||
def __init__(self): | |||||
self.graph = defaultdict(list) | |||||
def addEdge(self,u,v): | |||||
self.graph[u].append(v) | |||||
def DFSUtil(self,v,visited): | |||||
visited[v]= True | |||||
print v, | |||||
for i in self.graph[v]: | |||||
if visited[i] == False: | |||||
self.DFSUtil(i, visited) | |||||
def DFS(self,v): | |||||
visited = [False]*(len(self.graph)) | |||||
self.DFSUtil(v,visited) | |||||
g = Graph() | |||||
edges = input("input the number of edges : ") | |||||
print "enter nodes with zero based indexing : " | |||||
for i in range(edges): | |||||
a, b = map(int, raw_input().split()) | |||||
g.addEdge(a, b) | |||||
check = input("DFS check from node : ") | |||||
g.DFS(check) |
@ -0,0 +1,12 @@ | |||||
def binary_search(arr,element): | |||||
lower = 0 | |||||
upper = len(arr) - 1 | |||||
while(lower < upper): | |||||
mid = (lower + (upper - 1)) // 2 | |||||
if arr[mid] > element: | |||||
upper = mid-1 | |||||
elif arr[mid] < element: | |||||
lower = mid + 1 | |||||
else: | |||||
return True | |||||
return False |
@ -0,0 +1,6 @@ | |||||
def linear_search(arr, element): | |||||
for i in arr: | |||||
if i == element: | |||||
return True | |||||
return False | |||||
@ -0,0 +1,14 @@ | |||||
def combsort(numbers_list): | |||||
ordered = numbers_list.copy() | |||||
gap = len(numbers_list) # initial gap (first and last element) | |||||
swaps = True | |||||
while swaps or gap!=1: | |||||
swaps = False | |||||
for i in range(len(numbers_list)-gap): | |||||
if ordered[i] > ordered[i+gap]: # swaps without extra variable | |||||
ordered[i] = ordered[i+gap] - ordered[i] | |||||
ordered[i+gap] = ordered[i+gap] - ordered[i] | |||||
ordered[i] = ordered[i+gap] + ordered[i] | |||||
swaps = True | |||||
gap = max(gap-1, 1) # update gap, minimum gap is 1 | |||||
return ordered |
@ -0,0 +1,172 @@ | |||||
class Node(): | |||||
def __init__(self,key): | |||||
self.key = key | |||||
self.left = None | |||||
self.right = None | |||||
self.parent = None | |||||
class Tree(): | |||||
def __init__(self): | |||||
self.root = None | |||||
def add_node(self,key,node=None): | |||||
if node is None: | |||||
node = self.root | |||||
if self.root is None: | |||||
self.root = Node(key) | |||||
else: | |||||
if key <= node.key : | |||||
if node.left is None: | |||||
node.left = Node(key) | |||||
node.left.parent = node | |||||
print "left" | |||||
return | |||||
else: | |||||
# return self.add_node(key,node = self.root.left) | |||||
return self.add_node(key,node = node.left) | |||||
else: | |||||
if node.right is None: | |||||
node.right = Node(key) | |||||
node.right.parent = node | |||||
print "right" | |||||
return | |||||
else: | |||||
# return self.add_node(key,node = self.root.right) | |||||
return self.add_node(key,node = node.right) | |||||
def search(self,key,node = None): | |||||
if node is None: | |||||
node = self.root | |||||
if self.root.key == key: | |||||
print "key is at the root" | |||||
return self.root | |||||
else: | |||||
if node.key == key : | |||||
print "key exists" | |||||
return node | |||||
elif key < node.key and node.left is not None: | |||||
print "left" | |||||
return self.search(key,node = node.left) | |||||
elif key > node.key and node.right is not None: | |||||
print "right" | |||||
return self.search(key,node = node.right) | |||||
else: | |||||
print "key does not exist" | |||||
return None | |||||
def delete_node(self,key,node=None): | |||||
#search for the node to be deleted in tree | |||||
if node is None: | |||||
node = self.search(key)#return the node to be deleted | |||||
#root has no parent node | |||||
if self.root.key == node.key: #if it is root | |||||
parent_node = self.root | |||||
else: | |||||
parent_node = node.parent | |||||
'''case 1: The node has no chidren''' | |||||
if node.left is None and node.right is None: | |||||
if key <= parent_node.key: | |||||
parent_node.left = None | |||||
else: | |||||
parent_node.right = None | |||||
return | |||||
'''case 2: The node has children''' | |||||
''' if it has a single left node''' | |||||
if node.left is not None and node.right is None : | |||||
if node.left.key < parent_node.key : | |||||
parent_node.left = node.left | |||||
else: | |||||
parent_node.right = node.left | |||||
return | |||||
'''if it has a single right node''' | |||||
if node.right is not None and node.left is None: | |||||
if node.key <= parent_node.key: | |||||
parent_node.left = node.right | |||||
else: | |||||
parent_node.right = node.right | |||||
return | |||||
'''if it has two children''' | |||||
'''find the node with the minimum value from the right subtree. | |||||
copy its value to thhe node which needs to be removed. | |||||
right subtree now has a duplicate and so remove it.''' | |||||
if node.left is not None and node.right is not None: | |||||
min_value = self.find_minimum(node) | |||||
node.key = min_value.key | |||||
min_value.parent.left = None | |||||
return | |||||
def find_minimum(self,node = None): | |||||
if node is None: | |||||
node = self.root | |||||
'''find mimimum value from the right subtree''' | |||||
'''case when there is only a root node''' | |||||
if node.right is not None: | |||||
node = node.right | |||||
else: | |||||
return node | |||||
if node.left is not None: | |||||
return self.find_minimum(node = node.left) | |||||
else: | |||||
return node | |||||
def tree_data(self,node=None): | |||||
if node is None: | |||||
node = self.root | |||||
stack = [] | |||||
while stack or node: | |||||
if node is not None: | |||||
stack.append(node) | |||||
node = node.left | |||||
else: | |||||
node = stack.pop() | |||||
yield node.key | |||||
node = node.right | |||||
t=Tree() | |||||
t.add_node(10) | |||||
t.add_node(13) | |||||
t.add_node(14) | |||||
t.add_node(8) | |||||
t.add_node(9) | |||||
t.add_node(7) | |||||
t.add_node(11) | |||||
''' | |||||
10---8---7 | |||||
| | | |||||
| ---9 | |||||
---13---11 | |||||
| | |||||
---14 | |||||
''' |
@ -0,0 +1,27 @@ | |||||
def merge_sort(a): | |||||
if len(a) < 2: | |||||
return a | |||||
l = a[0:len(a)//2] | |||||
r = a[len(a)//2:] | |||||
return merge(merge_sort(l), merge_sort(r)) | |||||
def merge(a, b): | |||||
out = [] | |||||
i = 0 | |||||
j = 0 | |||||
while i < len(a) or j < len(b): | |||||
if i >= len(a): | |||||
out.append(b[j]) | |||||
j += 1 | |||||
elif j >= len(b): | |||||
out.append(a[i]) | |||||
i += 1 | |||||
else: | |||||
if a[i] <= b[j]: | |||||
out.append(a[i]) | |||||
i += 1 | |||||
else: | |||||
out.append(b[j]) | |||||
j += 1 | |||||
return out |
@ -0,0 +1,44 @@ | |||||
class Queque: | |||||
def __init__(self): | |||||
self.queque = [] | |||||
def enqueue(self, data): | |||||
if data not in self.queque: | |||||
self.queque.append(data) | |||||
return True | |||||
return False | |||||
def dequeue(self): | |||||
if len(self.queque) > 0: | |||||
return self.queque.pop(0) | |||||
return ('Queque empty') | |||||
def sizeQueque(self): | |||||
return len(self.queque) | |||||
def printQueque(self): | |||||
return self.queque | |||||
if __name__ == '__main__': | |||||
myqueque = Queque() | |||||
data = myqueque.enqueue(5) | |||||
data = myqueque.enqueue(6) | |||||
data = myqueque.enqueue(7) | |||||
data = myqueque.enqueue(10) | |||||
print(myqueque.sizeQueque()) | |||||
print(myqueque.printQueque()) | |||||
dequeue = myqueque.dequeue() # delete 5 | |||||
dequeue = myqueque.dequeue() # delete 6 | |||||
print(myqueque.printQueque()) | |||||
print(myqueque.sizeQueque()) |
@ -0,0 +1,14 @@ | |||||
def radix_sort(lista): | |||||
max_len = max([len(numero) for numero in lista]) | |||||
padded = list([str(num).rjust(max_len, "0") for num in lista]) | |||||
for pos in reversed(range(max_len)): | |||||
buckets = [list() for x in range(0, 10)] | |||||
for num in padded: | |||||
bucket = int(num[pos]) | |||||
buckets[bucket] += [num] | |||||
padded = sum(buckets, []) | |||||
return padded | |||||
if __name__ == "__main__": | |||||
print(radix_sort(["13", "105", "10", "150"])) |
@ -0,0 +1,16 @@ | |||||
#selection sorting | |||||
import sys | |||||
A = [6, 2, 1, 3, 4] | |||||
for i in range(len(A)): | |||||
min_index = i | |||||
for j in range(i+1, len(A)): | |||||
if A[min_index] > A[j]: | |||||
min_index = j | |||||
A[i], A[min_index] = A[min_index], A[i] | |||||
print ("Sorted array") | |||||
for i in range(len(A)): | |||||
print("%d" %A[i]) | |||||