Tuesday, April 17, 2018

Panagram Checking Python

Practical 10:

Write a Python function to check whether a string is pangram or not. For example:
“the quick brown fox jumps over the lazy dog”. Import the package to see whether
the input string is pangram or not.
Code :
#Pangram checking
string=input("Enter String : ");
string1="The quick brown fox jumps over the lazy dog";


alphabets='abcdefghijklmnopqrstuvwxyz';
isPangram=True;
for i in alphabets:
    if(string1.lower().__contains__(i)):
        continue;
    else:
        isPangram=False;
        break;


if(isPangram):
    print("Panagram");
else:
    print("Not Panagram");
 
 
Output:
 
 
Next Practical 11:
 
Water Jug Problem 
 

A * Search Python

Practical 9 :
Write a program to implement A*algorithm.

Code:

import heapq


hfun={
    'A':7,
    'B':6,
    'C':2,
    'D':1,
    'E':0}

gfun={
    'A':{
        'B':1,
        'C':4    },
    'B':{
        'C':2,
        'D':5,
        'E':12    },
    'C':{
        'D':2    },
    'D':{
        'E':3    },
    'E':{

    }
}


class PriorityQueue:
    def __init__(self):
        self.elements = []

    def empty(self):
        return len(self.elements) == 0
    def put(self, item, priority):
        heapq.heappush(self.elements, (priority, item))

    def get(self):
        return heapq.heappop(self.elements)[1]

class SimpleGraph:
    def __init__(self):
        self.edges = {}

    def neighbors(self, id):
        return self.edges[id]

    def cost(self,current,next):
        return gfun[current][next]


def a_star_search(graph, start, goal):
    frontier = PriorityQueue();
    frontier.put(start, 0)
    came_from = {}
    cost_so_far = {}
    came_from[start] = None    cost_so_far[start] = hfun[start];

    while not frontier.empty():
        current = frontier.get()

        if current == goal:
            break
        for next in graph.neighbors(current):
            new_cost = cost_so_far[current] + graph.cost(current, next)
            if next not in cost_so_far or new_cost < cost_so_far[next]:
                cost_so_far[next] = new_cost
                priority = new_cost + hfun[next];
                frontier.put(next, priority)
                came_from[next] = current

    return came_from


example_graph = SimpleGraph()
example_graph.edges = {
    'A': ['B','C'],
    'B': [ 'C', 'D','E'],
    'C': ['D'],
    'D': ['E'],
    'E': []
}

pathMatrix=a_star_search(example_graph,'A','E');
print ("Path is:")
c='E'while c!='A':
    print(c, end='<-')
    c=pathMatrix[c]
Output:



Next Practical 10
http://gndecprogramming.blogspot.in/2018/04/panagram-checking-python.html

Image Resolution Python

Practical 8: Write a program to find resolution of JPEG image.

Code :

def jpeg_res(filename):
    with open(filename,'rb') as img_file:

        img_file.seek(163)

        a = img_file.read(2)

        height = (a[0] << 8) + a[1]

        a = img_file.read(2)

        width = (a[0] << 8) + a[1]

    print("The resolution of the image is",width,"x",height)

jpeg_res("image.jpg");
 
 
Output:
 

 


 
Code Thanks :
We thank Programiz for this code 
https://www.programiz.com/python-programming/examples/resolution-image 


Next :
Practical 9 A* Search


Saturday, February 24, 2018

Artificial Intelligence Python

2.
Write a program to find the sum of all numbers stored in a list.
Code:
 
list=map(int,input("Enter List : ").strip().split());
s=0;
for j in list:
    s=s+j;
print("Sum is : ",s);

Output: 
 


 3.
Write a program to find factorial
of a number.
Code:
fact=1;
n=int(input("Enter Number : "));
for j in range(2,n+1):
    fact=fact*j;
print("Factorial is : ",fact);
 
Output:
 
 
 4.
Write a program to display formatted text calendar and entire calendar for a year
 
Code :
import calendar;
year=int(input("Enter Year"));
for month in range(1,13):
    print(calendar.month(year,month));
 
Output:
 
 
7.
Code:
celsius=int(input("Enter Temperatue in Celsius "))
f=celsius*1.8+32;
print('Temperatue in Farenheit is : ',f);
 
Output:
 
 

Wednesday, February 21, 2018

Program Classes and Objects

Write a program by creating objects to display student information


Code:

class Student:
    def __init__(self,name,c,rollno,phone,address):
        self.name=name
        self.c=c
        self.rollno=rollno
        self.phone=phone
        self.address=address

    def display(self):
        print('Name : ',self.name)
        print('Class : ',self.c)
        print('Roll No : ',self.rollno)
        print('Phone : ',self.phone)
        print('Address : ',self.address)
        print()


def main():
    john=Student('John','D3CSEA1',1507567,9023074222,'Ludhiana')
    john.display()
    print('********************','\n')
    jennie=Student('Jennie','D3CSEA2',1507542,8725053420,'Patiala')
    jennie.display()


main()

Output: