Tuesday, April 17, 2018

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:
 


Tuesday, February 20, 2018

Best First Search AI

Code :

alist={
    'A':['B','C','D'],
    'B':['H'],
    'C':['G','F'],
    'D':['E','P'],
    'E':[],
    'F':[],
    'G':[],
    'H':['J','I'],
    'I':['M','L','K'],
    'J':[],
    'K':[],
    'L':[],
    'M':[],
    'P':[]

}

hfun={
    'A':0,
    'B':5,
    'C':6,
    'D':3,
    'E':8,
    'F':12,
    'G':14,
    'H':7,
    'I':5,
    'J':6,
    'K':1,
    'L':10,
    'M':2,
    'P':9}

def fsort(v):
    for j in range(0,len(v)):
        for i in range(0,len(v)-j-1):
            if(hfun[v[i]]>hfun[v[i+1]]):
                temp=v[i+1];
                v[i+1]=v[i];
                v[i]=temp;


oqueue=[];
cqueue=[];
goal='I'
oqueue.append('A');
success=Falsewhile len(oqueue)!=0:
    node= oqueue.pop(0)
    if node is goal:
        success=True        break    for i in alist[node]:
        oqueue.append(i)

    cqueue.append(node)

    fsort(oqueue)
    print(cqueue)

if(success):
    print("Found")
else:
    print("Not Found")

Output:
Please try at your IDE