AL3391_AI_Answer_Key_final[1]
AL3391_AI_Answer_Key_final[1]
Third Semester
ANSWER KEY
K
-
L
PART A (10x2=20 Marks) Marks CO
e
v
el
Write an algorithm to find the sum of first ‘N’ natural 2 K CO1
numbers. 1
Algorithm:
1. Start
1. 2. Read the value of N
3. Compute sum = N * (N + 1) / 2
4. Print sum
5. Stop
8. Let list=['a', 'b', 'c', 'd', 'e', 'f']. Find the following: 2 K CO4
- List[1:3]: ['b', 'c'] 2
- List[:4]: ['a', 'b', 'c', 'd']
- List[::3]: ['a', 'd']
Define relative path and absolute path with respect to files: 2 K CO5
1
Absolute Path: Complete directory path from root.
9.
Relative Path: Path relative to the current working
directory.
Part B(5x13=65Marks)
OR
11 b (i)What is recursion? Write an algorithm to solve the 8 K3 CO1
Towers of Hanoi problem using recursion.
Recursion is a technique where a function calls itself to
solve smaller instances of a problem until it reaches a
base case. It is a fundamental concept in computer
science that simplifies problems by breaking them into
sub-problems. A recursive function has two main parts:
1. Base Case: This stops the recursion when a
specific condition is met.
2. Recursive Case: This is where the function calls
itself to solve a smaller part of the problem.
Algorithm for the Towers of Hanoi:
The Towers of Hanoi is a classic problem involving
three rods and n disks of different sizes, which must be
moved from the source rod to the destination rod,
following these rules:
1. Only one disk can be moved at a time.
2. No larger disk can be placed on top of a smaller
disk.
3. A temporary auxiliary rod can be used to
facilitate the process.
The algorithm to solve the problem using recursion:
1. Move n-1 disks from the source rod to the
auxiliary rod.
2. Move the nth disk from the source rod to the
destination rod.
3. Move n-1 disks from the auxiliary rod to the
destination rod.
Pseudocode:
ToH(n, source, destination, auxiliary):
If n == 1:
Print "Move disk 1 from source to destination"
Return
ToH(n-1, source, auxiliary, destination)
Print "Move disk", n, "from source to destination"
ToH(n-1, auxiliary, destination, source)
For example, with 3 disks, the function calls itself to
move smaller subsets of disks between the rods until the
base case is reached. Towers of Hanoi showcases the
elegance and efficiency of recursion in problem-solving,
as the recursive calls systematically break the problem
into manageable components.
11 b (ii) Draw a flowchart to check whether the string is 8 K2 CO1
palindrome or not.
A palindrome is a string that reads the same forward and
backward.
Steps for the Flowchart:
1. Start.
2. Input the string.
3. Reverse the string using a function or manual
method.
4. Compare the original string with the reversed
string.
o If both are equal, print “Palindrome.”
o Else, print “Not a Palindrome.”
5. End.
12 a Outline the data types supported by Python with an 16 K2 CO2
example.
Python supports a variety of data types, each tailored for
specific purposes. These are broadly categorized into
numeric, sequence, text, set, mapping, and boolean
types.
1. Numeric Types:
o int: Represents whole numbers.
Example: a = 10
o float: Represents decimal numbers.
Example: b = 3.14
o complex: Represents complex numbers
with real and imaginary parts.
Example: c = 3 + 4j
2. Sequence Types:
o list: A mutable collection of elements.
Example: lst = [1, 2, 3]
o tuple: An immutable collection of
elements.
Example: tup = (1, 2, 3)
o range: Represents a sequence of
numbers.
Example: r = range(5) gives 0, 1, 2, 3, 4.
3. Text Type:
o str: Represents a string of characters.
Example: s = "Hello"
4. Set Types:
o set: An unordered collection of unique
elements.
Example: st = {1, 2, 3}
o frozenset: An immutable version of a set.
5. Mapping Type:
o dict: Represents key-value pairs.
Example: d = {'a': 1, 'b': 2}
6. Boolean Type:
o bool: Represents True or False.
Example: is_active = True
Python’s flexibility with data types makes it ideal for
various applications. These data types are not only used
for storing data but also provide numerous methods for
data manipulation, such as sorting a list, accessing
dictionary values, or concatenating strings. Python’s
dynamic typing further allows variables to change their
data types during execution, enhancing its adaptability
for complex tasks.
OR
# Input
num = int(input("Enter a number: "))
print(f"The factorial of {num} is {factorial(num)}")
Example:
Input: 5
Output: The factorial of 5 is 120
13 a Describe the conditional branching statements of 16 K3 CO3
Python with an example program.
Conditional Branching in Python:
1. if Statement: Executes a block of code if the
condition is true.
if condition:
# Block of code
2. if-else Statement: Executes one block if the
condition is true and another block if it is false.
if condition:
# Block of code if true
else:
# Block of code if false
3. if-elif-else Statement: Checks multiple
conditions and executes the block corresponding
to the first true condition.
if condition1:
# Block of code for condition1
elif condition2:
# Block of code for condition2
else:
# Block of code if none are true
Example Program:
# Determine if a number is positive, negative, or zero
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Explanation of the Program:
The user inputs a number.
The program checks if the number is greater
than, less than, or equal to zero.
Based on the condition, an appropriate message
is displayed.
Output Example:
Input: 7
Output: The number is positive.
OR
2. Adding Items:
3. Accessing Items:
4. Printing a Dictionary:
Faculty-in-charge
HoD(i/c)/AI&DS