Tuesday, April 24, 2018

Tic Tac Toe Python

Tags
Program :Write a Program For tic tac toe game for 0 and X.

Code :

Link:   https://inventwithpython.com/chapter10.html

Output:




Next Animated Banner Program

Animated Banner Program Python

Tags
Program: Write a Program to create an animated banner program.

Code :
import os

import time

WIDTH = 79

message = "Dishant".upper()
printedMessage = [ "","","","","","","" ]

characters = { " " : [ " ",
                       " ",
                       " ",
                       " ",
                       " ",
                       " ",
                       " " ],

               "I" : [ "*******",
                       "   *   ",
                       "   *   ",
                       "   *   ",
                       "   *   ",
                       "   *   ",
                       "*******" ],

               "H" : [ "*     *",
                       "*     *",
                       "*     *",
                       "*******",
                       "*     *",
                       "*     *",
                       "*     *" ],

               "S" : [ "*******",
                       "*      ",
                       "*      ",
                       "*******",
                       "      *",
                       "      *",
                       "*******" ],

               "D" : [ "********",
                       "*      *",
                       "*      *",
                       "*      *",
                       "*      *",
                       "*      *",
                       "********" ],

               "N" : [ "*      *",
                       "**     *",
                       "* *    *",
                       "*  *   *",
                       "*    * *",
                       "*     **",
                       "*      *" ],

               "T" : [ "********",
                       "    *   ",
                       "    *   ",
                       "    *   ",
                       "    *   ",
                       "    *   ",
                       "    *   " ],


               "A" : [ "*******",
                       "*     *",
                       "*     *",
                       "*******",
                       "*     *",
                       "*     *",
                       "*     *" ]


               }

for row in range(7):
    for char in message:
        printedMessage[row] += (str(characters[char][row]) + "  ")

offset = WIDTH
while True:
    os.system("cls")
    for row in range(7):
        print(" " * offset + printedMessage[row][max(0,offset*-1):WIDTH - offset])
    offset -=1    if offset <= ((len(message)+2)*6) * -1:
        offset = WIDTH
    time.sleep(0.05)
 
 
Output:

 


 

Tuesday, April 17, 2018

Water Jug Simple Python

Tags
Practical 11: Write a program to solve water jug problem.

Code:
class Waterjug:


    def __init__(self,am,bm,a,b,g):
        self.a_max = am;
        self.b_max = bm;
        self.a = a;
        self.b = b;
        self.goal = g;


    def fillA(self):
        self.a = self.a_max;
        print ('(', self.a, ',',self.b, ')')


    def fillB(self):
        self.b = self.b_max;
        print ('(', self.a, ',', self.b, ')')


    def emptyA(self):
        self.a = 0;
        print ('(', self.a, ',', self.b, ')')


    def emptyB(self):
        self.b = 0;
        print ('(', self.a, ',', self.b, ')')


    def transferAtoB(self):
        while (True):

            self.a = self.a - 1            self.b = self.b + 1
            if (self.a == 0 or self.b == self.b_max):
                break
        print ('(', self.a, ',', self.b, ')')


    def main(self):
        while (True):

            if (self.a == self.goal or self.b == self.goal):
                break            if (self.a == 0):
                self.fillA()
            elif (self.a > 0 and self.b != self.b_max):
                self.transferAtoB()
            elif (self.a > 0 and self.b == self.b_max):
                self.emptyB()



waterjug=Waterjug(5,3,0,0,4);
waterjug.main();


Output:


Next :
Coming Soon Stay Tuned......


 

Panagram Checking Python

Tags
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

Tags
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

Tags
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