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

Department of Computer Science CSL-411: Artificial Intelligence Lab

Bahria University, Karachi Campus Semester 06 (Fall 2020)

Lab03: Python Programming 2

Designing and implementing Python programs that deal with:


1. Functions
2. Recursion

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
CODE:
#task1

def reverse_string():
stringOriginal = input("Enter a string please: ")
string = stringOriginal.split(" ")
string = string[::-1]
stringReversed = " ".join(string)
print("Original String: {}\nReversed String: {}".format(stringOriginal,stringReversed))

reverse_string()
Output:

Exercise 2

Write a function to check whether a string is a pangram or not. Note : Pangrams are words or
sentences containing every letter of the alphabet at least once.
For example : "The quick brown fox jumps over the lazy dog"
Hint: import the library “String”
Screen Shot:
CS Department, BUKC 1/4 Semester Spring 2020
CSL-411: Artificial Intelligence Lab Lab03: Functions and Recursion
CODE:
#Task2
import string

def ispangram():
str1 = input("Enter a string please: ")
alphabet=string.ascii_lowercase #this will return string of smallcase alphabet
alphaset = set(alphabet) # this will trun alphabet string into set
flag = alphaset <= set(str1.lower()) # boolean will be returned

if flag:
print("Yes!! it is a Panagram")
else:
print("No! it is not a panagram")

ispangram()
#sample
#1 The quick brown fox jumps over the lazy dog
#2 i know nothing
Output:

Exercise 3 (MatrixTest.py)

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:
CODE:
#Task 3

def hypenSeparated():
CS Department, BUKC 2/4 Semester Spring 2020
CSL-411: Artificial Intelligence Lab Lab03: Functions and Recursion
stringOriginal = input("Enter a hypen\(-\) separated string please: ")
string = stringOriginal.split("-")
string = sorted(string)
sortstring = "-".join(string)
print("Original String: {}\nSorted String: {}".format(stringOriginal,sortstring))

hypenSeparated()
Output:

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:

CODE:
#task4
def leapYear():
year = int(input("Enter any year please: "))
if (year%4 == 0):
if (year%100 == 0):
if( year%400 == 0):
print("{} is a leap year".format(year))
else:
print("{} is not leap year".format(year))
else:
print("{} is not leap year".format(year))
CS Department, BUKC 3/4 Semester Spring 2020
CSL-411: Artificial Intelligence Lab Lab03: Functions and Recursion
leapYear()

Output:

Exercise 5 (Recursion)

Write a recursive function to compute Ntn Fibonacci number. Test and trace for N = 6 is 8. We
remember that a Fibonacci number can be recursively defined as:
F ( n )=F ( n−1 )+ F ( n−2 ) for n≥ 2, where F ( 0 )=0 , F ( 1 )=1.

CODE:
#task5

def fibonacci(num):
if num <= 1:
return num
else:
return(fibonacci(num-1) + fibonacci(num-2))
print("--------------------Fibonacci Series \(using recursion\)----------------------")
number = int(input("Enter the number till you want to print the series of fibonacci: "))
for i in range(number+1):
print(fibonacci(i),end=" ")
Output:

CS Department, BUKC 4/4 Semester Spring 2020


CSL-411: Artificial Intelligence Lab Lab03: Functions and Recursion

You might also like