Saturday, February 24, 2024
HomeCareerBA Sem 5th Programming With Python (dse) Important Questions And Answers

BA Sem 5th Programming With Python (dse) Important Questions And Answers

Q:1 Write a Python function moveOdd(L1,L2)that accepts two lists L1 and L2. List L1 consists of integers and list L2 is initially empty. The function moveOdd removes the odd integers from the list L1 and moves them to the list L2. Write python statements to apply the function moveOdd on the list L1: [31, 28, 63, 86, 15, 79, 44, 52]. What will be the content of list L1 and L2 after the execution of moveOdd function for the list L1?

Here is the Python function moveOdd(L1, L2)

def moveOdd(L1, L2):
This function moves odd integers from list L1 to list L2.

L1: A list of integers.
L2: An empty list.

for i in range(len(L1) – 1, -1, -1):
if L1[i] % 2 != 0:

# Example usage
L1 = [31, 28, 63, 86, 15, 79, 44, 52]
L2 = []
moveOdd(L1, L2)
print(“L1:”, L1)
print(“L2:”, L2)
Use code with caution. Learn more
This function iterates through the list L1 in reverse order (using a for loop with a negative stride). If it encounters an odd number (using the modulo operator % to check if the remainder of dividing by 2 is not 0), it removes that number from L1 using the pop method and appends it to L2. BA Sem 5th Programming With Python (dse) Important Questions And Answers


L1: [28, 86, 44, 52]
L2: [79, 15, 63, 31]
As you can see, the function successfully moves all the odd numbers from L1 to L2, leaving only even numbers in L1

Q:-2 Write a Python program that contains function main and functionoccurs(s,ch). Function occurs(s,ch)accepts a string s and a character ch as arguments and returns the number of occurrences of character ch in the string s. The main function reads a string and a character from the keyboard, calls function occurs and prints the number of occurrences of the character. Do not use a built-in function to find occurrence of the character

def occurs(s, ch):
This function counts the occurrences of a character in a string.

s: The string to search.
ch: The character to count.

The number of occurrences of the character in the string.
count = 0
for char in s:
if char == ch:
count += 1
return count

def main():
This function reads a string and a character from the user,
calls the occurs function, and prints the result.
s = input(“Enter a string: “)
ch = input(“Enter a character to count: “)
count = occurs(s, ch)
print(f”The character ‘{ch}’ occurs {count} times in the string ‘{s}’.”)

if __name__ == “__main__”:

This program defines two functions:

  • occurs(s, ch): This function iterates through each character in the string s and checks if it matches the character ch. If it does, it increments a counter. Finally, it returns the count.
  • main(): This function reads the string and character from the user using input, calls the occurs function with the user input, and prints the result.

Q:-3 Write a Python function smallerXY(X,Y)that accepts two integers X and Y and returns the smaller of two. Write another function smallerXYZ(X,Y,Z)that uses the function smallerXY to find minimum of three numbers X,Y,Z

def smallerXY(X, Y):
This function returns the smaller of two integers X and Y.

X: An integer.
Y: An integer.

The smaller of X and Y.
if X <= Y:
return X
return Y

def smallerXYZ(X, Y, Z):
This function uses the smallerXY function to find the minimum of three integers X, Y, and Z.

X: An integer.
Y: An integer.
Z: An integer.

The smallest of X, Y, and Z.
# Use smallerXY to find the minimum of X and Y
min_XY = smallerXY(X, Y)
# Compare the minimum of X and Y with Z and return the smallest
return smallerXY(min_XY, Z)

# Example usage
x = 5
y = 10
z = 2
smallest = smallerXYZ(x, y, z)
print(f”The smallest of {x}, {y}, and {z} is {smallest}”)

This code defines two functions

  • smallerXY(X, Y): This function checks if X is less than or equal to Y using a conditional statement. If it is, X is returned as the smaller number. Otherwise, Y is returned.
  • smallerXYZ(X, Y, Z): This function leverages the smallerXY function twice. It first finds the smaller number between X and Y using smallerXY(X, Y). Then, it compares this smaller number with Z using another call to smallerXY to determine the overall smallest number among all three.

Q:4 Apply the Bubble Sort scheme of sorting on the following list to sort it in ascending order. Show the content of the list after applying each iteration of Bubble sort: =[74, 31, 82, 25, 63, 96, 49, 11]. How many iterations are required to sort the list? What is the number of comparisons performed in each iteration? Apply Binary search to search for the item 50 in the sorted list. You should show the computation of the index at which the value is compared with 50. When will you prefer to use binary search over linear search? Justify your answer.

Bubble Sort

Original list: [74, 31, 82, 25, 63, 96, 49, 11]

  1. [74, 31, 82, 25, 63, 96, 49, 11]: 6 comparisons. Swap 74 and 31.
  2. [31, 74, 82, 25, 63, 96, 49, 11]: 5 comparisons. Swap 82 and 74.
  3. [31, 74, 82, 25, 63, 96, 49, 11]: 5 comparisons. No swaps, largest element is in place.
  4. [31, 74, 25, 63, 96, 82, 49, 11]: 5 comparisons. Swap 82 and 63.
  5. [31, 74, 25, 63, 96, 49, 82, 11]: 5 comparisons. Swap 49 and 82.
  6. [31, 74, 25, 63, 96, 49, 11, 82]: 5 comparisons. Swap 11 and 49.
  7. [31, 74, 25, 63, 96, 11, 49, 82]: 5 comparisons. No swaps, list is sorted.

Total iterations: 7

Comparisons per iteration: 6

Binary Search

Sorted list: [11, 25, 31, 49, 63, 74, 82, 96]

Searching for 50:

  1. Midpoint (initial) = (7 + 0) / 2 = 3.5 (rounded down to 3)
  2. Element at index 3: 31 (less than 50)
  3. Update search space to [4, 7]
  4. New midpoint = (7 + 4) / 2 = 5.5 (rounded down to 5)
  5. Element at index 5: 63 (greater than 50)
  6. Update search space to [4, 5]
  7. No further divisions possible, element not found.

Computation of index: 3, 5.

When to prefer Binary Search

  • List is sorted: This is crucial for binary search to work efficiently.
  • List is large: As the list size increases, the performance gap between binary and linear search widens significantly. Binary search makes significantly fewer comparisons, leading to faster search times.

BA Sem 5th Programming With Python (dse) Important Questions And Answers – In this case, with a sorted list of moderate size (8 elements), both methods would be relatively fast. However, for larger sorted lists, binary search would be significantly faster due to its logarithmic complexity compared to linear search’s linear complexity.

Therefore, the choice depends on the specific context, including the size and order of the data, and the desired level of efficiency.

Q:-5 A print application processes the print jobs in the first come, first served manner. Suggest a suitable data structure for the print application. What are the permissible operations on the suggested data structure?

A Queue is the most suitable data structure for a print application that operates on a first-come, first-served (FCFS) basis.

  • FIFO ordering: A queue maintains the order of elements added, ensuring that the first job added is processed first. This aligns perfectly with the FCFS principle.
  • Efficient operations: Adding new print jobs (enqueuing) can be done at the back of the queue in constant time (O(1)). Similarly, removing the completed job at the front (dequeuing) is also a constant-time operation.
  • Dynamic size: Queues can dynamically adjust their size to accommodate any number of pending print jobs, making them scalable for varying workloads.

Permissible Operations on the Queue

  • Enqueue(job): Adds a new print job (represented as “job”) to the back of the queue.
  • Dequeue(): Removes the completed print job from the front of the queue and returns it.
  • Front(): Returns the element at the front of the queue without removing it (useful for checking the next job in line).
  • IsEmpty(): Checks if the queue is empty.
  • Size(): Returns the current number of elements in the queue.
  • Clear(): Removes all print jobs from the queue.
  • Priority queueing: Assigning priorities to print jobs and processing them accordingly (e.g., urgent jobs first).
  • Batching: Grouping similar print jobs together for efficiency.
  • Preemption: Temporarily pausing a job to allow another higher-priority job to be processed.

BA Sem 5th Programming With Python (dse) Important Questions And Answers- These additional features can further enhance the functionality and flexibility of the print application’s data structure.

Overall, a queue provides an efficient and reliable way to manage print jobs in a first-come, first-served manner. Its simple operations and dynamic nature make it a natural choice for this type of application. BA Sem 5th Programming With Python (dse) Important Questions And Answers

5 1 vote
Article Rating
Notify of

Inline Feedbacks
View all comments

Most Popular