Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CSL-411: Artificial Intelligence Lab

Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercises

Exercise 1

Write a Python program to create all possible permutations from a given collection of distinct numbers.
Let the collection be [3,6,9]
Expected Output:

Python Code:
def permute(nums):
result_perms = [[]]
for n in nums:
new_perms = []
for perm in result_perms:
for i in range(len(perm)+1):
new_perms.append(perm[:i] + [n] + perm[i:])
result_perms = new_perms
return result_perms

my_nums = [3,6,9]
print("Original Cofllection: ",my_nums)
print("Collection of distinct numbers:\n",permute(my_nums))

Output:

Department of Computer Sciences 1/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab 02: Intro to Python Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercise 2

Write a Python program to print a long text, convert the string to a list and print all the words and their
frequencies. Expected Output is given below.

Python Code:
sentence = "This is the basic python programming lab and we are trying to implement the task for calculating the
frequency of each word appeared in the string"
word_list = sentence.split()
print ("\n\nstring to list: \n",word_list)

all_freq = {}

for i in word_list:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1

print ("\n\nwords and frequencies :\n "


+ str(all_freq))
Output:

Department of Computer Sciences 2/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab 02: Intro to Python Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercise 3

Write a Python program to calculate body mass index.

Python Code:
@author: atifj
height=float(input("Enter Height in feet: "))
weight=float(input("Enter Weight in kg: "))

height1=height*30.48

BMI = weight / (height1/100)**2


print("\nYour BMI is:",BMI)

if BMI<18.5:
print("Your BMI status is: underweight")

elif BMI>18.5 and BMI<24.9:


print("Your BMI status is: Normal")

elif BMI>25.0 and BMI<29.9:


print("Your BMI status is: Overweight")

elif BMI>30.0:
print("Your BMI status is: Obese")

Output:

Department of Computer Sciences 3/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab 02: Intro to Python Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercise 4

Bahria grading system.

Python Code:
print("Grading System")
sum = 0
list = ["AI", "EP", "TW", "MAD", "OTB", "IT_INF"]
for n in list:
obt = int(input("Marks of {}: ".format(n)))
sum = sum + obt
total = int(input("Enter total marks: "))
print("Obtained total: {}".format(sum))
avg = sum/6
if avg >= 85 and avg <= 100:
print("Grade: A")
print("GPA: 4.0")
elif avg >= 80 and avg <= 84:
print("Grade: A-")
print("GPA: 3.67")
elif avg >= 75 and avg <= 79:
print("Grade: B+")
print("GPA: 3.33")
elif avg >= 71 and avg <= 74:
print("Grade: B")
print("GPA: 3.0")
elif avg >= 68 and avg <= 70:
print("Grade: B-")
print("GPA: 2.67")
elif avg >= 64 and avg <= 67:
print("Grade: C+")
print("GPA: 2.33")
elif avg >= 60 and avg <= 63:

Department of Computer Sciences 4/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab 02: Intro to Python Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

print("Grade: C")
print("GPA: 2.0")
elif avg >= 57 and avg <= 59:
print("Grade: C-")
print("GPA: 1.87")
elif avg >= 53 and avg <= 56:
print("Grade: D+")
print("GPA: 1.33")
elif avg >= 50 and avg <= 52:
print("Grade: D")
print("GPA: 1.0")
elif avg < 50:
print("Grade: F")
print("GPA: 0")
else:
print("Wrong input")
Output:

Department of Computer Sciences 5/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab 02: Intro to Python Programming

You might also like