Professional Documents
Culture Documents
Assignment # 5
Submitted by
Name waqar ullah
Student ID: 13010
Course: BS (Computer Science)
Instructor
Dr. Abdus salam
Department of Computing
Abasyn University, Peshawar Campus
1) Write algorithm for each of the following problem.
i. Read in any three characters (a-z) and print them in alphabetical order.
Problem:
Read any three alphabets from (a-z) and print them in alphabetical order.
Input:
Enter any three alphabets from a-z.
Output:
Print the alphabets in Alphabetical Order.
Algorithm:
1) def isAlphabaticOrder(s):
2) n = len(s)
3) c = [s[i] for i in range(len(s))]
4) c.sort(reverse = False)
5) for i in range(n):
6) if (c[i] != s[i]):
7) return False
8) return True
iii. Write an algorithm that finds the largest number in a list (an array) of n
numbers.
Problem: Write an algorithm that finds the largest number in a list (an array)
of n numbers.
Algorithm:
a. Start
b. Input the list from user
c. Print(max(list))
d. Exit
iv. Write an algorithm that finds the m smallest numbers in a list of n
numbers.
Problem: Write an algorithm that finds the smallest number in a list (an array)
of n numbers.
Input: Enter the list.
Output: Largest should be printed in a list.
Algorithm:
a. Start
b. Input the list from user
c. Print(min(list))
d. Exit
v. Write an algorithm that prints out all the subsets of three elements of a
set of n elements.
Problem: Write an algorithm that prints out all the subsets of three elements of a set
of n elements.
Input: Input the three elements in the set.
Output: Print the subset of set.
Algorithm:
a. Start
b. Declare variable that contain three element in the set s = {1, 2, 3} c. Initialize the
variable.
d. Using import method import itertools
e. Now defining function
def findsubsets(s, n):
return list(itertools.combinations(s, n))
f. Calling the function
s = {1, 2, 3} n = 2
print(findsubsets(s, n))
2. Solve the following recurrence equations:
i. T(n) = T(n-3) + n2
Solution:
Now we know that
T(n) = T(n-3) + n2
Next It will become T(n-3)= T(n-6)+ 1n2
T(n-6)= T(n-9)+2n2
T(n-9)= T(n-12)+ 3n
Now at one time it will be equal to zero, So
T(n)= T(n-i)+ i*n2 :- i-n=0 i=n
T(n)=T(0)+nn2
Solution:
T(n) = 2T(n/2) + n2
v. T(n) = T(n/2) + 1
Solution:
T(n) = 2T(n/2) + n2
Now next time T(n/2)= T(n/22)+2
T(n/4)= T(n/23)+3
T(n/8)=T(n/24)+4
On time it will be like
T(n)= T(n/2i)+i
T(n)= log n
So the order is Θ(log n)