Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

NAME-SUNALI ROLL NO-102103375 Artificial Intelligence (UCS 521)

Lab Assignment-1

Ques 1 A class with 10 students wants to produce some information from the results of the four

standard tests in Maths, Science, English and IT. Each test is out of 100 marks. The information

output should be the highest, lowest and average mark for each test and the highest, lowest

and average mark overall. Write a program in Python to complete this task. SOLUTION:-

CODE:-

def calculate_statistics(scores):

highest = max(scores)

lowest = min(scores)

average = sum(scores) / len(scores)

return highest, lowest, average

def calculate_class_statistics(test_scores):

# Calculate statistics for each test

maths_stats = calculate_statistics(test_scores['Maths'])

science_stats = calculate_statistics(test_scores['Science'])

english_stats = calculate_statistics(test_scores['English'])

it_stats = calculate_statistics(test_scores['IT'])

# Calculate overall statistics

all_scores = test_scores['Maths'] + test_scores['Science'] + test_scores['English'] + test_scores['IT']

overall_stats = calculate_statistics(all_scores)

# Print the results

print("Maths - Highest: {}, Lowest: {}, Average: {}".format(*maths_stats))

print("Science - Highest: {}, Lowest: {}, Average: {}".format(*science_stats))


print("English - Highest: {}, Lowest: {}, Average: {}".format(*english_stats))

print("IT - Highest: {}, Lowest: {}, Average: {}".format(*it_stats))

print("Overall - Highest: {}, Lowest: {}, Average: {}".format(*overall_stats))


# Test scores for each student

test_scores = {

'Maths': [80, 90, 70, 95, 85, 75, 60, 92, 88, 79],

'Science': [70, 85, 90, 78, 82, 93, 65, 88, 95, 76],

'English': [75, 88, 92, 68, 80, 85, 90, 79, 83, 77],

'IT': [88, 92, 70, 80, 85, 90, 75, 83, 79, 72]

calculate_class_statistics(test_scores)

OUTPUT:

QUES 2 Write a Python Program to input basic salary of an employee and calculate its Gross salary

according to following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary <= 20000 :

HRA = 25%, DA = 90% Basic Salary > 20000 : HRA = 30%, DA = 95%. SOLUTION:-

CODE:-

def calculate_gross_salary(basic_salary):

if basic_salary <= 10000:

hra = 0.2 * basic_salary

da = 0.8 * basic_salary

elif basic_salary <= 20000:

hra = 0.25 * basic_salary

da = 0.9 * basic_salary
else:

hra = 0.3 * basic_salary

da = 0.95 * basic_salary

gross_salary = basic_salary + hra + da


return gross_salary

# Input basic salary from the user

basic_salary = float(input("Enter the basic salary: "))

# Calculate gross salary

gross_salary = calculate_gross_salary(basic_salary)

# Print the gross salary

print("Gross Salary: ", gross_salary)

OUTPUT:-

QUES 3 Write a Python program to check the validity of password input

by users. Validation:

∙ At least 1 letter between [a-z] and 1 letter between

[A-Z]. ∙ At least 1 number between [0-9].

∙ At least 1 character from [$#@].

∙ Minimum length 6 characters.

∙ Maximum length 16 characters.

SOLUTION:-

CODE:-

import re

def validate_password(password):

# Check length
if len(password) < 6 or len(password) > 16:

return False
# Check for lowercase letter

if not re.search(r"[a-z]", password):

return False

# Check for uppercase letter

if not re.search(r"[A-Z]", password):

return False

# Check for number

if not re.search(r"\d", password):

return False

# Check for special character

if not re.search(r"[$#@]", password):

return False

return True

# Input password from the user

password = input("Enter the password: ")

# Validate the password

valid = validate_password(password)

# Print the result

if valid:

print("Password is valid.")

else:
print("Password is invalid.")

OUTPUT:
QUES 4 Create a List L that is defined as= [10, 20, 30, 40, 50, 60, 70, 80].

(i) WAP to add 200 and 300 to L.

(ii) WAP to remove 10 and 30 from L.

(iii) WAP to sort L in ascending order.

(iv) WAP to sort L in descending order.

SOLUTION:-

CODE:-

# Create the initial list

L = [10, 20, 30, 40, 50, 60, 70, 80]

print("Initial List:", L)

# (i) Add 200 and 300 to L

L.append(200)

L.append(300)

print("(i) List after adding 200 and 300:", L)

# (ii) Remove 10 and 30 from L

L.remove(10)

L.remove(30)

print("(ii) List after removing 10 and 30:", L)

# (iii) Sort L in ascending order

L.sort()

print("(iii) List sorted in ascending order:", L)


# (iv) Sort L in descending order

L.sort(reverse=True)

print("(iv) List sorted in descending order:", L)


OUTPUT:-

QUES 5 D is a dictionary defined as D= {1:”One”, 2:”Two”, 3:”Three”, 4:

“Four”, 5:”Five”}. (i) WAP to add new entry in D; key=6 and value is “Six”

(ii) WAP to remove key=2.

(iii) WAP to check if 6 key is present in D.

(iv) WAP to count the number of elements present in D.

(v) WAP to add all the values present D.

SOLUTION:-

CODE:-

# Create the initial dictionary

D = {1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five"}

print("Initial Dictionary:", D)

# (i) Add new entry in D: key=6, value="Six"

D[6] = "Six"

print("(i) Dictionary after adding key=6 and value='Six':", D)

# (ii) Remove key=2 from D

del D[2]

print("(ii) Dictionary after removing key=2:", D)

# (iii) Check if key=6 is present in D


if 6 in D:

print("(iii) Key=6 is present in the dictionary.")

else:
print("(iii) Key=6 is not present in the dictionary.")

# (iv) Count the number of elements present in D

num_elements = len(D)

print("(iv) Number of elements in the dictionary:", num_elements)

# (v) Add all the values present in D

values_sum = sum(D.values())

print("(v) Concatenated values in the dictionary:", values_sum)

OUTPUT:-

QUES 6 WAP to create a list of 100 random numbers between 100 and 900. Count and

print the: (i) All odd numbers

(ii) All even numbers

(iii) All prime numbers

SOLUTION

CODE:-

import random

# Create a list of 100 random numbers between 100 and 900

random_numbers = [random.randint(100, 900) for _ in range(100)]

# Initialize lists for odd numbers, even numbers, and prime numbers

odd_numbers = []

even_numbers = []
prime_numbers = []
# Iterate through the random_numbers

list for number in random_numbers: #

Check for odd numbers

if number % 2 != 0:

odd_numbers.append(number)

# Check for even numbers

elif number % 2 == 0:

even_numbers.append(number)

# Check for prime numbers

is_prime = True

if number > 1:

for i in range(2, int(number**0.5) + 1): if

number % i == 0:

is_prime = False

break

else:

is_prime = False

if is_prime:

prime_numbers.append(number)

# Print the odd numbers

print("(i) Odd Numbers:", odd_numbers)

# Print the even numbers

print("(ii) Even Numbers:", even_numbers)


# Print the prime numbers
print("(iii) Prime Numbers:", prime_numbers)

OUTPUT:-

QUES 7 (i) Write a function which takes principal amount, interest rate and time. This

function returns compound interest. Call this function to print the output.

SOLUTION:-

CODE:-

def calculate_compound_interest(principal, interest_rate, time):

# Calculate compound interest

compound_interest = principal * (1 + interest_rate) ** time - principal

return compound_interest

# Example usage

principal_amount = 1000

interest_rate = 0.05

time_period = 3

# Call the function to calculate compound interest

result = calculate_compound_interest(principal_amount, interest_rate, time_period)

# Print the output

print("Compound Interest:", result)

(ii) Save this function (as a module) in a python file and call it in another python

file. from interest_calculation import calculate_compound_interest

# Example usage

principal_amount = 1000
interest_rate = 0.05

time_period = 3

# Call the function to calculate compound interest

result = calculate_compound_interest(principal_amount, interest_rate, time_period)

# Print the output

print("Compound Interest:", result)

OUTPUT:-

QUES 8 A) Make a class called Restaurant. The __init__() method for Restaurant should store

two attributes: a restaurant_name and a cuisine_type. Make a method called

describe_restaurant() that prints these two pieces of information, and a method called

open_restaurant() that prints a message indicating that the restaurant is open. Make an instance

called restaurant from your class. Print the two attributes individually, and then call both

methods. SOLUTION:-

CODE:-

class Restaurant:

def __init__(self, restaurant_name, cuisine_type):

self.restaurant_name = restaurant_name

self.cuisine_type = cuisine_type

def describe_restaurant(self):

print("Restaurant Name:", self.restaurant_name)

print("Cuisine Type:", self.cuisine_type)

def open_restaurant(self):
print("The restaurant is open.")

# Create an instance of the Restaurant class

restaurant = Restaurant("Italiano's", "Italian")

# Print the individual attributes

print("Restaurant Name:", restaurant.restaurant_name)

print("Cuisine Type:", restaurant.cuisine_type)

# Call the describe_restaurant() method

restaurant.describe_restaurant()

# Call the open_restaurant() method

restaurant.open_restaurant()

OUTPUT:-

B) Make a class called User. Create two attributes called first_name and last_name, and then

create several other attributes that are typically stored in a user profile. Make a method

called describe_user() that prints a summary of the user’s information. Make another method

called greet_user() that prints a personalized greeting to the user. Create several instances

representing different users, and call both method for each user.

CODE:-

class User:

def __init__(self, first_name, last_name, age, location, occupation):

self.first_name = first_name

self.last_name = last_name

self.age = age
self.location = location
self.occupation = occupation

def describe_user(self):

print("User Information:")

print("First Name:", self.first_name)

print("Last Name:", self.last_name)

print("Age:", self.age)

print("Location:", self.location)

print("Occupation:", self.occupation)

def greet_user(self):

print("Hello,", self.first_name, self.last_name + "! Welcome to our platform.")

# Create instances of the User class

user1 = User("John", "Doe", 25, "New York", "Engineer")

user2 = User("Alice", "Smith", 32, "London", "Teacher")

user3 = User("Bob", "Johnson", 45, "Paris", "Business Owner")

# Call the describe_user() and greet_user() methods for each

user user1.describe_user()

user1.greet_user()

print()

user2.describe_user()

user2.greet_user()

print()

user3.describe_user()

user3.greet_user()
OUTPUT:-

You might also like