Validate an IP address using ReGex

import re

def Validate_It(IP):

regex = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|"\

"2[0-4][0-9]|25[0-5])\\.){3}"\

"([0-9]|[1-9][0-9]|1[0-9][0-9]|"\

"2[0-4][0-9]|25[0-5])"

regex1 = "((([0-9a-fA-F]){1,4})\\:){7}"\

"([0-9a-fA-F]){1,4}"

p = re.compile(regex)

p1 = re.compile(regex1)

if (re.search(p, IP)):

return "Valid IPv4"

elif (re.search(p1, IP)):

return "Valid IPv6"

return "Invalid IP"

IP = "203.120.223.13"

print(Validate_It(IP))

IP = "fffe:3465:efab:23fe:2235:6565:aaab:0001"

print(Validate_It(IP))

IP = "2F33:12a0:3Ea0:0302"

print(Validate_It(IP))


Creating map using python

import folium

my_map4 = folium.Map(location = [28.5011226,77.4099794],zoom_start =12)

folium.Marker([28.704059, 77.102490],popup = 'Delhi').add_to(my_map4)

folium.Marker([28.5011226, 77.4099794],popup = 'Python For Engineers').add_to(my_map4)

folium.PolyLine(locations = [(28.704059, 77.102490), (28.5011226, 77.4099794)],line_opacity =0.5).add_to(my_map4)

my_map4.save("my_map4.html")

my_map4

Program for Cocktail Sort

def cocktailSort(a):

n = len(a)

swapped = True

start = 0

end = n-1

while (swapped==True):

swapped = False

for i in range (start, end):

if (a[i] > a[i+1]) :

a[i], a[i+1]= a[i+1], a[i]

swapped=True

if (swapped==False):

break

swapped = False

end = end-1

for i in range(end-1, start-1,-1):

if (a[i] > a[i+1]):

a[i], a[i+1] = a[i+1], a[i]

swapped = True

start = start+1

a = [5, 1, 4, 2, 8, 0, 2 ,6,7]

cocktailSort(a)

print("Sorted array is:")

for i in range(len(a)):

print ("%d" %a[i]),


Program for Gnome Sort

def gnomeSort( arr, n):

index = 0

while index < n:

if index == 0:

index = index + 1

if arr[index] >= arr[index - 1]:

index = index + 1

else:

arr[index], arr[index-1] = arr[index-1], arr[index]

index = index - 1

return arr

arr = [ 34, 2, 10, -9]

n = len(arr)

arr = gnomeSort(arr, n)

print ("Sorted seqquence after applying Gnome Sort :")

for i in arr:

print (i)

Program to find the maximum and minimum value node from a circular linked list

class Node:    

    def __init__(self,data):    

        self.data = data;    

        self.next = None;    

class CreateList:    

    def __init__(self):    

        self.head = Node(None);    

        self.tail = Node(None);    

        self.head.next = self.tail;    

        self.tail.next = self.head;      

    def add(self,data):    

        newNode = Node(data);    

        if self.head.data is None:    

            self.head = newNode;    

            self.tail = newNode;    

            newNode.next = self.head;    

        else:    

            self.tail.next = newNode;    

            self.tail = newNode;    

            self.tail.next = self.head;                 

    def minNode(self):    

        current = self.head;    

        minimum = self.head.data;    

        if(self.head == None):    

            print("List is empty");    

        else:    

            while(True):       

                if(minimum > current.data):    

                    minimum = current.data;    

                current= current.next;    

                if(current == self.head):    

                    break;    

        print("Minimum value node in the list: "+ str(minimum));    

    def maxNode(self):    

        current = self.head;    

        maximum = self.head.data;    

        if(self.head == None):    

            print("List is empty");    

        else:    

            while(True):       

                if(maximum < current.data):    

                    maximum = current.data;    

                current= current.next;    

                if(current == self.head):    

                    break;    

        print("Maximum value node in the list: "+ str(maximum));    

class CircularLinkedList:    

    cl = CreateList();    

    cl.add(5);    

    cl.add(20);    

    cl.add(10);    

    cl.add(1);    

    cl.minNode();    

    cl.maxNode();