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 program (using functions!) that asks the user for a long string containing multiple words. Print back to the
user the same string, except with the words in backwards order. For example,
Input: I live in Pakistan.
Output: Pakistan in live I.
Here reverse_string() is function name

Python Code:

sentence = "i live in pakistan!"


word_list = sentence.split()
reversed_list =list(reversed(word_list))
reversed_sentence = " ".join(reversed_list)
print("original string:",sentence)
print("reversed string",reversed_sentence)

Output:

Department of Computer Sciences 1/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab03: Functions and Object-Oriented Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercise 2

MyPython Coffee Outlet runs a catalog business. It sells only one type of coffee beans. The company
sells the coffee in 2-lb bags only and the price of a single 2-lb bag is $5.50. when a customer places an
order, the company ships the order in boxes. The boxes come in 3 sizes with 3 different costs:

Large box Medium box Small box


capacity 20 bags 10 bags 5 bags
cost $1.80 $1.00 $0.60

The order is shipped using the least number boxes. For example, the order of 52 bags will be shipped in
2 boxes: 2 large boxes, 1medium and 1 small.

Develop an application by using the above that computes the total cost of an order.

Sample output:

Number of Bags Ordered: 52


The Cost of Order: $ 286.00

Boxes Used:
2 Large - $3.60
1 Medium - $1.00
1 Small - $0.60

Your total cost is: $ 291.20

Python Code:
noOfBags,large,medium,small,n,largecost,mediumcost,smallcost,total=0,0,0,0,0,0,0,0,0
noOfBags=int(input("Enter the number of bags ordered: "))
large = noOfBags/20
n = noOfBags%20
medium = n/10
n=n%10
if n<=5:
small=1
else:
small=2

largecost = large * 1.80


mediumcost = medium * 1
smallcost = small * 0.60
total = noOfBags * 5.50

print("Total number of bags ordered: ",noOfBags," $",(total))

Department of Computer Sciences 2/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab03: Functions and Object-Oriented Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

total = total + largecost + mediumcost + smallcost


print("Boxes Used :")
print(int(large) , " Large - $",(largecost))
print(int(medium) , " Medium - $",(mediumcost))
print(int(small) , " Small - $",(smallcost))
print("Your Total Cost is: $",(total))
Output:

Exercise 3

Write a function that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-
separated sequence after sorting them alphabetically.
Sample Items : green-red-yellow-black-white
Expected Result : black-green-red-white-yellow
Screen Shot:

Python Code:
list = [i for i in input("Enter Hyphen Sequence to sort: ").split('-')]
list.sort()
print("Sorted Sequence = ", end='')
print('-'.join(list))
Output:

Department of Computer Sciences 3/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab03: Functions and Object-Oriented Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercise 4

We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add
it to the shortest month of the year, February.
In the Gregorian calendar three criteria must be taken into account to identify leap years:
 The year can be evenly divided by 4, is a leap year, unless:
 The year can be evenly divided by 100, it is NOT a leap year, unless:
 The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200,
2300 and 2500 are NOT leap years.
You are given the year, and you have to write a function to check if the year is leap or not.
Screen Shots:

OR

Python Code:
year = int(input("Enter year: 199"))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))

Output:

Department of Computer Sciences 4/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab03: Functions and Object-Oriented Programming
CSL-411: Artificial Intelligence Lab
Semester BS (IT) – 06
Atif Jalal
02-235191-027

Exercise 5

In number theory a Perfect number is said to be the number that is a positive integer and is equal to the sum of its
proper positive divisor (excluding the number itself), or another definition is the perfect number is the number that
is half the sum of itself and its proper positive divisors for example:
1+2+3=6, 6 is the sum of the proper positive divisors of itself that is 1,2 and 3 or half of the sum of itself and its
said divisors e.g. (1+2+3+6)/2  12/6  6.

Write a python function named as PerfectNumber(n) takes n as argument and returns whether the number is
perfect or not.

Python Code:
def perfect_number(n):
sum = 0
for i in range(1, n):
if n % i == 0:
sum += i
return sum == n
print(perfect_number(496))
Output:

Department of Computer Sciences 5/5 Semester BSIT 06


CSL-411: Artificial Intelligence Lab Lab03: Functions and Object-Oriented Programming

You might also like