0% found this document useful (0 votes)
44 views1 page

Data Structures & Algorithms in Python

ab

Uploaded by

Farhan Kazi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views1 page

Data Structures & Algorithms in Python

ab

Uploaded by

Farhan Kazi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DR.

BABASAHEB AMBEDKAR TECHNOLOGICAL UNIVERSITY, LONERE


Winter Examination – 2022
Course: B. Tech. Branch: Artificial Intelligence & Data Science Semester: III
Subject Code & Name: BTAIC303 – Data Structure and Algorithm using Python
Max Marks: 60 Date: Duration: 3 Hr.
Instructions to the Students:
1. All the questions are compulsory.
2. The level of question/expected answer as per OBE or the Course Outcome (CO)
on which the question is based is mentioned in ( ) in front of the question.
3. Use of non-programmable scientific calculators is allowed.
4. Assume suitable data wherever necessary and mention it clearly.
(Level/CO) Marks
Q. 1 Solve Any Two of the following. 12
A) Explain the use of break and continue statement with suitable example. (BT2/CO1) 6
B) What is list? Explain any 5 operation on list with suitable example. (BT1/CO1) 6
C) Write a program to take one string as input and print total number of words (BT3/CO1) 6
in given string.

Q.2 Solve Any Two of the following. 12


A) What is class and object? Write a program to initialize and display details of (BT3/CO2) 6
two student using classes and objects?
B) What is Exception Handling? Explain how to handle exceptions? (BT3/CO2) 6
C) Write a code to define a function which will take list as parameter and return (BT3/CO2) 6
the count of even number in list. Call function with appropriate parameter.

Q. 3 Solve Any Two of the following. 12


A) Write algorithms for deleting nodes from singly linked list. (BT3/CO3) 6
B) Differentiate between array and linked list? Explain the node structure for (BT2/CO3) 6
singly linked list and doubly linked list with suitable example.
C) Write a program to perform push(), pop(), isEmpty(), isFull() and display() (BT3/CO1) 6
operations on stack.

Q.4 Solve Any Two of the following. 12


A) What is BST? Create a BST for 60, 56, 40, 34, 70, 80, 50, 45. (BT3/CO4) 6
After creation of BST perform deletion on node 34.
B) Define Hashing? Explain collision with suitable example? (BT2/CO4) 6
C) Define Tree Data Structure? Explain several Tree Terminologies? (BT3/CO4) 6

Q. 5 Solve Any Two of the following. 12


A) Write algorithm for insertion sort? Sort following element using insertion (BT3/CO5) 6
sort 40,30,20,50,45,60,10.
B) What is an algorithm? Explain time complexity and space complexity? (BT4/CO5) 6
C) Write python program for Binary Search algorithm. (BT3/CO5) 6

*** End ***

Common questions

Powered by AI

The 'break' statement terminates the current loop and resumes execution at the next statement following the loop. The 'continue' statement skips the rest of the code inside the loop for the current iteration and jumps to the next iteration of the loop. For example, in a 'for' loop iterating over numbers 1 to 5, using 'break' when a number equals 3 will stop the loop entirely, while 'continue' will skip printing number 3 and continue with 4 and 5 .

Arrays are fixed-size and provide constant-time access to elements using indices, whereas linked lists are dynamic and have variable size, but require linear time to access elements. A singly linked list's node contains data and a reference to the next node, while a doubly linked list's node contains data and references to both next and previous nodes, allowing traversal in both directions .

Hashing is a technique to map data to a fixed size using hash functions, crucial for data retrieval efficiency. Collisions occur when two values hash to the same key. Strategies to handle collisions include open addressing, where subsequent slots are checked, and chaining, where each slot retains a list of elements that hash to the same slot. For instance, with chaining, if keys 3 and 8 both hash to index 0, they are stored in a list at that index .

In Python, a class is a blueprint for creating objects, which are instances of the class. A class defines attributes and methods that its objects will have. For demonstration, you can create a Student class with attributes like name and age, and methods to initialize and display these details. Two student objects can then be created and their details displayed. Example: 'class Student: def __init__(self, name, age): self.name = name; self.age = age; def display(self): print("Name:", self.name, "Age:", self.age)' .

To count the number of words in a string in Python, you can use the split() method on the string, which separates the words based on spaces and returns a list. The length of this list corresponds to the number of words. Example: 'input_string = input("Enter a string: "); word_count = len(input_string.split())' .

Exception handling in Python involves managing errors using 'try', 'except', and optionally 'finally', and 'else' blocks. 'Try' contains the code that might throw an exception, and 'except' captures the error if it occurs. 'Finally' is used for code that should run regardless of whether an exception occurred. For example, using 'try: result = 10 / x except ZeroDivisionError: print("Can't divide by zero")', where 'x' is a variable, handles division by zero errors .

A list in Python is a mutable, ordered sequence of elements. Five operations on lists include: 1) append() which adds an element to the end of the list, e.g., list.append(4); 2) remove() which removes the first occurrence of a value, e.g., list.remove(2); 3) pop() which removes and returns the last item, e.g., list.pop(); 4) insert() which inserts a value at a specified position, e.g., list.insert(2, 'Python'); and 5) sort() which sorts the list in place, e.g., list.sort().

Time complexity reflects the computation time of an algorithm relative to input size, crucial for efficiency. Space complexity indicates the memory usage relative to input size, affecting resource demands. Important to optimize both for performance, time complexity guides on speed, space complexity on memory usage constraints. Balancing them impacts an algorithm's practicality and feasibility in practice .

A BST is created by inserting elements while maintaining the property that left children are smaller and right children larger than the parent node. Inserting 60 first, followed by 56, 40, 34, 70, 80, 50, and 45 results in a specific structure. Deleting node 34 involves removing the node and reconnecting its parent node 40 directly to the node following 34's left subtree, which is 50 in this case .

Binary search divides the list and discards half during each comparison, enabling log(n) efficiency, unlike linear search's O(n), searching element by element. In Python, implement as: 'def binary_search(arr, x): low, high = 0, len(arr) - 1; while low <= high: mid = (high + low) // 2; if arr[mid] < x: low = mid + 1; elif arr[mid] > x: high = mid - 1; else: return mid'. Thus, efficiency stems from halving search domain each step .

You might also like