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


Vertical concatenation in matrix using loop

sample_list = [["Hi", "python"], ["welcome", "for"], ["to","engineers"]]

print("The original list : " + str(sample_list))

res = []

N = 0

while N != len(sample_list):

temp = ''

for idx in sample_list:

try: temp = temp + idx[N]

except IndexError: pass

res.append(temp)

N = N + 1

res = [ele for ele in res if ele]

print("List after column Concatenation : " + str(res))


Find number of times every day occurs in a Year

import datetime

import calendar

def day_occur_time(year):

days = [ "Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday",

"Sunday" ]

L = [52 for i in range(7)]

pos = -1

day = datetime.datetime(year, month = 1, day = 1).strftime("%A")

for i in range(7):

if day == days[i]:

pos = i

if calendar.isleap(year):

L[pos] += 1

L[(pos+1)%7] += 1

else:

L[pos] += 1

for i in range(7):

print(days[i], L[i])

year = 2020

day_occur_time(year)


Program to count uppercase, lowercase, special character and numeric values using Regex

import re

string = "Pythonforengineers !,* 12345"

uppercase_characters = re.findall(r"[A-Z]", string)

lowercase_characters = re.findall(r"[a-z]", string)

numerical_characters = re.findall(r"[0-9]", string)

special_characters = re.findall(r"[, .!?*]", string)

print("The no. of uppercase characters is", len(uppercase_characters))

print("The no. of lowercase characters is", len(lowercase_characters))

print("The no. of numerical characters is", len(numerical_characters))

print("The no. of special characters is", len(special_characters))


Program to find LCM

def calculate_lcm(x, y):  

    if x > y:  

        greater = x  

    else:  

        greater = y  

    while(True):  

        if((greater % x == 0) and (greater % y == 0)):  

            lcm = greater  

            break  

        greater += 1  

    return lcm    

num1 = int(input("Enter first number: "))  

num2 = int(input("Enter second number: "))  

print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2))  


Program to find Smallest K digit number divisible by X

 def answer(X, K):

MIN = pow(10, K-1)

if(MIN%X == 0):

return (MIN)

else:

return ((MIN + X) - ((MIN + X) % X))

X = 83;

K = 5;

print(answer(X, K));