ARTIFICIAL INTELLIGENCE LAB

==============================================================

PROGRAM 1: SIMPLE CALCULATOR & CALENDAR

AIM:
To perform arithmetic operations and generate a calendar.

DESCRIPTION:
This program performs basic arithmetic operations such as addition,
subtraction, multiplication, and division. It also displays a calendar
for a given month and year using Python's calendar module.

PROGRAM:

# Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operator (+,-,*,/): ")

if op == '+':
    print("Result =", num1 + num2)
elif op == '-':
    print("Result =", num1 - num2)
elif op == '*':
    print("Result =", num1 * num2)
elif op == '/':
    print("Result =", num1 / num2)
else:
    print("Invalid operator")

# Calendar
import calendar
year = int(input("Enter year: "))
month = int(input("Enter month: "))
print(calendar.month(year, month))

OUTPUT:

Enter first number: 10
Enter second number: 5
Enter operator (+,-,*,/): +

Result = 15.0

Enter year: 2025
Enter month: 4

    April 2025
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

==============================================================

PROGRAM 5: SUDOKU SOLVER

AIM:
To solve Sudoku using backtracking.

DESCRIPTION:
This program fills empty cells (represented as 0) in a Sudoku grid
such that each row, column, and 3x3 subgrid contains numbers from 1 to 9.

PROGRAM:

def print_board(b):
    for row in b:
        print(row)

def is_safe(row, col, b, num):
    for i in range(9):
        if b[row][i] == num or b[i][col] == num:
            return False

    start_row = (row//3)*3
    start_col = (col//3)*3

    for i in range(3):
        for j in range(3):
            if b[start_row+i][start_col+j] == num:
                return False
    return True

def solve(b):
    for i in range(9):
        for j in range(9):
            if b[i][j] == 0:
                for num in range(1,10):
                    if is_safe(i,j,b,num):
                        b[i][j] = num
                        if solve(b):
                            return True
                        b[i][j] = 0
                return False
    return True

b = []
print("Enter Sudoku:")
for i in range(9):
    b.append(list(map(int,input().split())))

solve(b)
print("\nSolved Sudoku:")
print_board(b)

OUTPUT:

Enter Sudoku:
5 3 0 0 7 0 0 0 0
6 0 0 1 9 5 0 0 0
0 9 8 0 0 0 0 6 0
8 0 0 0 6 0 0 0 3
4 0 0 8 0 3 0 0 1
7 0 0 0 2 0 0 0 6
0 6 0 0 0 0 2 8 0
0 0 0 4 1 9 0 0 5
0 0 0 0 8 0 0 7 9

Solved Sudoku:
[5, 3, 4, 6, 7, 8, 9, 1, 2]
[6, 7, 2, 1, 9, 5, 3, 4, 8]
[1, 9, 8, 3, 4, 2, 5, 6, 7]
[8, 5, 9, 7, 6, 1, 4, 2, 3]
[4, 2, 6, 8, 5, 3, 7, 9, 1]
[7, 1, 3, 9, 2, 4, 8, 5, 6]
[9, 6, 1, 5, 3, 7, 2, 8, 4]
[2, 8, 7, 4, 1, 9, 6, 3, 5]
[3, 4, 5, 2, 8, 6, 1, 7, 9]

==============================================================

PROGRAM 8: PERCEPTRON (LOGIC GATES)

AIM:
To implement AND, OR and NOT gates using perceptron.

DESCRIPTION:
This program uses weighted sum and step activation function
to simulate logic gates.

PROGRAM:

import numpy as np

def step(x):
    return 1 if x >= 0 else 0

def perceptron(x, weights, bias):
    return step(np.dot(x, weights) + bias)

inputs = np.array([[0,0],[0,1],[1,0],[1,1]])

print("AND Gate:")
weights = np.array([1,1])
bias = -1.5
for x in inputs:
    print(x, "->", perceptron(x, weights, bias))

print("\nOR Gate:")
bias = -0.5
for x in inputs:
    print(x, "->", perceptron(x, weights, bias))

print("\nNOT Gate:")
weights = np.array([-1])
bias = 0.5
for x in [[0],[1]]:
    print(x, "->", perceptron(x, weights, bias))

OUTPUT:

AND Gate:
[0 0] -> 0
[0 1] -> 0
[1 0] -> 0
[1 1] -> 1

OR Gate:
[0 0] -> 0
[0 1] -> 1
[1 0] -> 1
[1 1] -> 1

NOT Gate:
[0] -> 1
[1] -> 0

==============================================================

PROGRAM 9: NLTK (a,b,c)

AIM:
To perform tokenization, POS tagging and stopword removal.

DESCRIPTION:
This program processes text using NLTK to extract words,
identify parts of speech and remove stopwords.

PROGRAM:

import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk import pos_tag
import string

nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('stopwords')

text = input("Enter sentence: ")

words_with_punct = word_tokenize(text)
words_without_punct = [w for w in words_with_punct if w not in string.punctuation]

pos_tags = pos_tag(words_without_punct)

stop_words = set(stopwords.words('english'))
filtered = [w for w in words_without_punct if w.lower() not in stop_words]

print("Words with punctuation:", words_with_punct)
print("Words without punctuation:", words_without_punct)
print("POS Tags:", pos_tags)
print("After removing stopwords:", filtered)

OUTPUT:

Enter sentence: Hello! This is a simple example.

Words with punctuation:
['Hello', '!', 'This', 'is', 'a', 'simple', 'example', '.']

Words without punctuation:
['Hello', 'This', 'is', 'a', 'simple', 'example']

POS Tags:
[('Hello', 'NNP'), ('This', 'DT'), ('is', 'VBZ'),
 ('a', 'DT'), ('simple', 'JJ'), ('example', 'NN')]

After removing stopwords:
['Hello', 'simple', 'example']

==============================================================

PROGRAM 10: TOKENIZATION & CORPUS

AIM:
To perform tokenization and corpus analysis.

DESCRIPTION:
This program splits text into tokens and calculates total and distinct words.

PROGRAM (a):

import nltk
from nltk.tokenize import word_tokenize, sent_tokenize

nltk.download('punkt')

text = input("Enter text: ")

print("Word Tokens:", word_tokenize(text))
print("Sentence Tokens:", sent_tokenize(text))
print("Paragraph Tokens:", text.split("\n\n"))

PROGRAM (b):

from nltk.corpus import gutenberg
nltk.download('gutenberg')

words = gutenberg.words('austen-emma.txt')

print("Total words:", len(words))
print("Distinct words:", len(set(words)))

OUTPUT:

Enter text: Hello world. This is NLP.

Word Tokens:
['Hello', 'world', '.', 'This', 'is', 'NLP', '.']

Sentence Tokens:
['Hello world.', 'This is NLP.']

Paragraph Tokens:
['Hello world. This is NLP.']

Total words: 192427
Distinct words: 7811

==============================================================

PROGRAM 13: MAGIC SQUARE

AIM:
To generate odd and even magic squares.

DESCRIPTION:
Magic square ensures equal sum in rows, columns and diagonals.

PROGRAM:

def odd_magic_square(n):
    magic = [[0]*n for _ in range(n)]
    i, j = 0, n//2
    for num in range(1,n*n+1):
        magic[i][j] = num
        ni, nj = (i-1)%n, (j+1)%n
        if magic[ni][nj]:
            i = (i+1)%n
        else:
            i, j = ni, nj
    return magic

def even_magic_square(n):
    magic = [[(i*n)+j+1 for j in range(n)] for i in range(n)]
    for i in range(n):
        for j in range(n):
            if (i%4 == j%4) or ((i%4 + j%4)==3):
                magic[i][j] = n*n+1 - magic[i][j]
    return magic

choice = input("Enter type (odd/even): ")
n = int(input("Enter n: "))

if choice == "odd":
    res = odd_magic_square(n)
else:
    res = even_magic_square(n)

print("Magic Square:")
for row in res:
    print(row)

OUTPUT:

Enter type (odd/even): even
Enter n: 4

Magic Square:
[16, 2, 3, 13]
[5, 11, 10, 8]
[9, 7, 6, 12]
[4, 14, 15, 1]

==============================================================