Assignment: Programming Methodology and Data Structure
Course: BSc (1st Year)
Subject: Programming Methodology and Data Structures
Submission Date: [Your Deadline]
Introduction
This assignment is designed to strengthen your understanding of fundamental programming
principles
and data structures. It consists of two sections:
1. **Theory Questions**: Covering programming paradigms, data structures, recursion, and
sorting algorithms.
2. **Practical Coding Problems**: Hands-on coding exercises to implement arrays, stacks,
linked lists, and sorting.
By completing this assignment, you will gain essential problem-solving skills required in
software development.
Section A: Theory Questions
1. What are the different programming paradigms?
Programming paradigms define the approach used to solve problems. The main paradigms
are:
- **Procedural Programming**: Uses functions and procedures (e.g., C, Python).
- **Object-Oriented Programming (OOP)**: Uses classes and objects (e.g., Java, C++).
- **Functional Programming**: Treats computation as evaluation of mathematical functions
(e.g., Haskell, Lisp).
- **Logic Programming**: Uses logical statements and inference rules (e.g., Prolog).
2. Explain the difference between linear and non-linear data structures.
Linear structures store data sequentially, whereas non-linear structures have hierarchical
relationships.
**Examples:**
- **Linear:** Arrays, Linked Lists, Stacks, Queues.
- **Non-Linear:** Trees, Graphs.
**Advantages of Linear Structures:**
- Simpler implementation and traversal.
- Efficient for operations like searching and sorting.
**Advantages of Non-Linear Structures:**
- More efficient in complex relationships (e.g., database indexing).
- Allows hierarchical organization (e.g., file systems).
3. What is recursion? Explain with an example.
Recursion is a function calling itself to solve smaller instances of the same problem.
**Example: Factorial Calculation (Python)**
```python
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
```
**Advantages of Recursion:**
- Simplifies code for problems like tree traversal.
- Reduces the need for loops.
**Disadvantages:**
- Consumes more memory (stack calls).
- May cause stack overflow for large inputs.
Section B: Practical Coding Problems
1. Array Operations (Python)
# Insert and Delete Operations on an Array
def insert(arr, pos, val):
arr.insert(pos, val)
return arr
def delete(arr, pos):
arr.pop(pos)
return arr
# Example Usage
arr = [1, 2, 3, 4, 5]
print(insert(arr, 2, 99)) # Output: [1, 2, 99, 3, 4, 5]
print(delete(arr, 4)) # Output: [1, 2, 99, 3, 5]
2. Stack Implementation (Python)
# Stack Implementation using a List
class Stack:
def __init__(self):
self.stack = []
def push(self, val):
self.stack.append(val)
def pop(self):
return self.stack.pop() if self.stack else "Stack is empty"
def peek(self):
return self.stack[-1] if self.stack else "Stack is empty"
# Example Usage
stack = Stack()
stack.push(10)
stack.push(20)
print(stack.pop()) # Output: 20
print(stack.peek()) # Output: 10
3. Linked List Implementation (Python)
# Linked List Implementation
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_end(self, data):
if not self.head:
self.head = Node(data)
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = Node(data)
def display(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")
# Example Usage
ll = LinkedList()
ll.insert_at_end(10)
ll.insert_at_end(20)
ll.display() # Output: 10 -> 20 -> None
4. Sorting Algorithms (Python)
# Bubble Sort Implementation
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
arr = [5, 3, 8, 1, 2]
print(bubble_sort(arr)) # Output: [1, 2, 3, 5, 8]
Conclusion
This assignment reinforced key programming concepts and problem-solving techniques.
The theory section covered programming paradigms, recursion, and data structures. The
practical section
provided hands-on coding experience with arrays, stacks, linked lists, and sorting algorithms.
Mastering these topics is crucial for building efficient software solutions.