Friday, December 26, 2025

📘 PAPER 2: OBJECT ORIENTED PROGRAMMING WITH PYTHON UNIT 2 – Python Data Structures (university of allahabad)

 

🔴 UNIT 2 – Python Data Structures


1️⃣ Introduction to Python Data Structures

✅ What is a Data Structure?

A data structure is a way to store, organize, and manage data efficiently so that operations can be performed easily.

Python provides built-in data structures, such as:


2️⃣ Python Data Types (Revisited)

🔹 Mutable Data Types

Data that can be changed after creation.

  • List

  • Dictionary

  • Set

🔹 Immutable Data Types

Data that cannot be changed.

  • Integer

  • Float

  • String

  • Tuple


3️⃣ Python Lists

✅ Definition

A list is an ordered, mutable collection of elements.

Example:

numbers = [10, 20, 30, 40]

🔹 List Characteristics

✔ Ordered
✔ Mutable
✔ Allows duplicate values
✔ Indexed


🔹 List Operations

Accessing elements

print(numbers[0])

Slicing

numbers[1:3]

Adding elements

numbers.append(50)

Removing elements

numbers.remove(20)

4️⃣ List Slicing

Syntax:

list[start : end : step]

Example:

a = [1,2,3,4,5] print(a[1:4])

Output:

[2, 3, 4]

5️⃣ Indexing

  • Positive indexing → Left to right

  • Negative indexing → Right to left

a = [10, 20, 30] print(a[-1]) # 30

6️⃣ List Concatenation

a = [1,2] b = [3,4] c = a + b

Output:

[1,2,3,4]

7️⃣ Searching in Python

🔹 Linear Search

Searches element one by one.

def linear_search(lst, key): for i in lst: if i == key: return True return False

✔ Simple
❌ Slow for large data


🔹 Binary Search

Works only on sorted lists

def binary_search(arr, key): low = 0 high = len(arr)-1 while low <= high: mid = (low+high)//2 if arr[mid] == key: return True elif arr[mid] < key: low = mid+1 else: high = mid-1 return False

✔ Fast
✔ Efficient


8️⃣ Inductive Function Definition

Meaning:

A function defined using:

  1. Base case

  2. Recursive case

Example:

Factorial

def fact(n): if n == 0: return 1 return n * fact(n-1)

9️⃣ Sorting Techniques

🔹 Selection Sort

Steps:

  1. Find minimum

  2. Swap with first element

  3. Repeat

def selection_sort(arr): for i in range(len(arr)): min = i for j in range(i+1, len(arr)): if arr[j] < arr[min]: min = j arr[i], arr[min] = arr[min], arr[i]

🔹 Insertion Sort

def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and key < arr[j]: arr[j+1] = arr[j] j -= 1 arr[j+1] = key

🔟 In-Place Sorting

Definition:

Sorting without using extra memory.

✔ Selection Sort
✔ Insertion Sort


1️⃣1️⃣ Tuples

Definition:

Tuple is an ordered but immutable collection.

t = (10, 20, 30)

✔ Faster than list
✔ Used for fixed data


1️⃣2️⃣ Mutability Concept

Data TypeMutable
ListYes
TupleNo
StringNo
DictionaryYes

1️⃣3️⃣ Programs Using Lists

Find Maximum Element

lst = [4, 8, 2, 9] print(max(lst))

Find Minimum Element

print(min(lst))

Find Mean

mean = sum(lst) / len(lst)

📘 PAPER 2: OBJECT ORIENTED PROGRAMMING WITH PYTHON (MCA547) UNIT -1 Object Oriented Programming & Python Basics (university of allahabad)

 

🔴 UNIT 1 Object Oriented Programming & Python Basics


1️⃣ Introduction to Object-Oriented Programming (OOP)

✅ What is OOP?

Object-Oriented Programming is a programming approach that organizes software using objects and classes instead of functions and logic.

✅ Objective of OOP


2️⃣ Basic Concepts of OOP

🔹 1. Object

An object is a real-world entity that has:

  • Properties (data)

  • Behavior (methods)

📌 Example:

car = Car()

🔹 2. Class

A class is a blueprint for creating objects.

📌 Example:

class Car: def start(self): print("Car started")

🔹 3. Encapsulation

Wrapping data and methods together.

Data hiding
✔ Security

Example:

class Student: def __init__(self): self.__marks = 90

🔹 4. Abstraction

Showing only essential details and hiding internal implementation.

✔ Achieved using abstract classes


🔹 5. Inheritance

One class acquires properties of another.

class Child(Parent): pass

🔹 6. Polymorphism

Same function behaves differently.

Example:

print(len("Hello")) print(len([1,2,3]))

3️⃣ Benefits of OOP

✔ Code reusability
✔ Modularity
✔ Easy debugging
✔ Security
✔ Real-world modeling


4️⃣ Applications of OOP

  • Software development

  • Web applications

  • Game development

  • AI & ML

  • Mobile apps

  • GUI applications


5️⃣ Algorithms and Programming

✅ Algorithm

A finite set of steps to solve a problem.

Characteristics:

  • Finite

  • Unambiguous

  • Effective

  • Input & Output defined


6️⃣ Introduction to Python

✅ Features of Python


7️⃣ Structure of Python Program

# Comment print("Hello World")

Components:

  1. Comments

  2. Statements

  3. Indentation

  4. Functions


8️⃣ Variables in Python

Definition:

A variable is a container to store data.

x = 10 name = "Python"

9️⃣ Data Types in Python

Built-in Data Types

TypeExample
int10
float10.5
str"Hello"
boolTrue
list[1,2,3]
tuple(1,2)
set{1,2}
dict{"a":1}

🔟 Operators in Python

Types of Operators:

  1. Arithmetic (+, -, *, /)

  2. Relational (>, <, ==)

  3. Logical (and, or, not)

  4. Assignment (=, +=)

  5. Bitwise (&, |)


1️⃣1️⃣ Control Flow in Python

🔹 Conditional Statements

if a > b: print("A is greater") else: print("B is greater")

🔹 Looping Statements

For Loop

for i in range(5): print(i)

While Loop

i = 0 while i < 5: print(i) i += 1

📘 PAPER 1: THEORY OF COMPUTATION (MCA546) UNIT 5: TURING MACHINES & UNDECIDABILITY (university of allahabad )

 

🔴 UNIT 5: TURING MACHINES & UNDECIDABILITY


1️⃣ Turing Machine (TM)

✅ Definition

A Turing Machine is the most powerful abstract machine used to model computation.

It can solve any problem that is algorithmically computable.


✅ Components of Turing Machine

A Turing Machine is a 7-tuple:

M = (Q, Σ, Γ, δ, q₀, B, F)

Where:

  • Q → Finite set of states

  • Σ → Input alphabet

  • Γ → Tape alphabet

  • δ → Transition function

  • q₀ → Initial state

  • B → Blank symbol

  • F → Final (accepting) states


✅ Structure of TM

A Turing Machine has:

  • Infinite tape

  • Read/Write head

  • Finite control

  • Move Left / Right / Stay


2️⃣ Instantaneous Description (ID)

✅ Definition

An Instantaneous Description (ID) represents the current state of the Turing Machine.

Format:

αqβα q β

Where:

  • α → Tape content to left of head

  • q → Current state

  • β → Tape content under and right of head


✅ Example

101q011

Means:

  • Head is reading 0

  • Current state = q

  • Tape contains 101011


3️⃣ Language Accepted by Turing Machine

A language L is said to be accepted by a TM if:

  • TM halts in accepting state for every string in L

  • TM may loop for strings not in L


4️⃣ Turing Computability of Functions

A function is Turing computable if:

  • There exists a TM that computes it

  • TM halts for all valid inputs


Important Fact

All recursive functions are Turing computable


5️⃣ Equivalence of Turing Machine and Recursive Functions

Theorem:

A function is Turing computable if and only if it is partial recursive.

This proves:

➡ All are equivalent models of computation


6️⃣ Recursively Enumerable (RE) Languages

✅ Definition

A language L is recursively enumerable if:

  • There exists a Turing Machine that accepts L

  • TM may not halt for strings not in L


✅ Types of Languages

Language TypeDescription
RecursiveTM halts for all inputs
Recursively EnumerableTM may loop
Non-RENot accepted by any TM

7️⃣ Recursively Decidable Languages

✅ Definition

A language is recursive (decidable) if:

  • TM halts for all inputs

  • Gives YES or NO answer


Relationship

Recursive ⊂ Recursively Enumerable

8️⃣ Undecidability

✅ Definition

A problem is undecidable if:

  • No algorithm exists to solve it for all inputs

  • TM cannot decide it


9️⃣ Undecidability of Type-0 Grammar

Type-0 grammars generate:

  • Recursively enumerable languages

Problem:

Whether a given string belongs to a Type-0 grammar?

Result:

Undecidable

Because:


🔟 Church–Turing Thesis

✅ Statement

Any function that can be computed by an algorithm can be computed by a Turing Machine.


⚠ Important Points

  • Not a theorem (cannot be proved)

  • Based on strong evidence

  • Foundation of computer science


1️⃣1️⃣ Halting Problem

✅ Problem Statement

Given a program P and input I, determine:

Will P halt or run forever?


✅ Result

🚫 Halting Problem is Undecidable


✅ Proof Idea (Simple)

  • Assume halting problem is decidable

  • Construct a contradictory machine

  • Leads to logical contradiction

  • Hence undecidable


1️⃣2️⃣ Importance of Halting Problem

📘 PAPER 1: THEORY OF COMPUTATION (MCA546) UNIT 4: PUSH DOWN AUTOMATA (PDA) (university of allahabad)

 

🔴 UNIT 4: PUSH DOWN AUTOMATA (PDA)

This unit is very important because it connects CFG and Automata, and questions often come for 5–10 marks.


1️⃣ Pushdown Automata (PDA)

✅ Definition

A Pushdown Automaton (PDA) is a computational model that:


✅ Formal Definition

A PDA is a 7-tuple:

M = (Q, Σ, Γ, δ, q₀, Z₀, F)

Where:

  • Q → Finite set of states

  • Σ → Input alphabet

  • Γ → Stack alphabet

  • δ → Transition function

  • q₀ → Initial state

  • Z₀ → Initial stack symbol

  • F → Final states


✅ Transition Function

δ:Q×(Σ{ε})×ΓP(Q×Γ)\delta : Q × (Σ ∪ \{ε\}) × Γ → P(Q × Γ^*)

Meaning:

  • Read input symbol or ε

  • Pop top of stack

  • Push new symbol(s)


2️⃣ Instantaneous Description (ID)

✅ Definition

An Instantaneous Description (ID) describes the current status of a PDA at any moment.

Format:

(q,w,γ)(q, w, γ)

Where:

  • q → Current state

  • w → Remaining input

  • γ → Stack content


✅ Example

(q₀, 1011, Z₀)

Means:

  • Current state = q₀

  • Input remaining = 1011

  • Stack contains = Z₀


3️⃣ Acceptance by PDA

A PDA can accept a language in two ways:


🔹 1. Acceptance by Final State

A string is accepted if:

✔ Stack content does not matter


🔹 2. Acceptance by Empty Stack

A string is accepted if:

  • Input is fully consumed

  • Stack becomes empty

✔ Final state not necessary


⚠ Important Theorem

👉 Both methods are equivalent in power


4️⃣ Deterministic Pushdown Automata (DPDA)

✅ Definition

A PDA is deterministic if:


✅ Key Points


✅ Example

Language:
L = { aⁿbⁿ | n ≥ 1 }

✔ Can be accepted by DPDA


❌ Example

L = { wwʳ | w ∈ {a,b}* }

✖ Cannot be accepted by DPDA


5️⃣ Relationship Between PDA and CFG

🔹 Important Theorem

A language is context-free if and only if it is accepted by a PDA


🔹 CFG → PDA

For every CFG, there exists a PDA that:


🔹 PDA → CFG

For every PDA, a CFG can be constructed that:

  • Generates same language

  • Uses transitions to define productions


🔁 Relationship Diagram

CFG ⇄ PDA

6️⃣ PDA for Language aⁿbⁿ

Language:

L = { aⁿbⁿ | n ≥ 1 }


Working:

  1. Push a onto stack for each input a

  2. Pop one a for each b

  3. Accept when stack is empty


Stack Behavior:

InputStack
apush a
apush a
bpop a
bpop a

7️⃣ Difference: FA vs PDA

Finite AutomataPushdown Automata
No stackHas stack
Recognizes regular languagesRecognizes CFL
Limited memoryInfinite memory (stack)
DFA/NFAPDA

8️⃣ Applications of PDA

📘 PAPER 1: THEORY OF COMPUTATION (MCA546) UNIT -3 Context Free Grammars (CFG) (university of allahabad)


🔴 UNIT 3: Context Free Grammars (CFG)

(EXTREMELY IMPORTANT – Long answers + conversions often asked)


1️⃣ Context Free Grammar (CFG)

Definition

A Context Free Grammar is a grammar in which each production rule has a single non-terminal on the left-hand side.

Formal Definition

A CFG is a 4-tuple:

G = (V, Σ, P, S)

Where:

  • V → Set of non-terminals

  • Σ → Set of terminals

  • P → Production rules

  • SStart symbol


Form of Productions

A → α
Where:


Example

Grammar for L = { aⁿbⁿ | n ≥ 1 }

Productions:


2️⃣ Derivation in CFG

Derivation

Process of generating strings from the start symbol using production rules.


Types of Derivation

Leftmost Derivation

Rightmost Derivation


Example

S → aSb → aab b


3️⃣ Derivation Trees (Parse Trees)

Definition

A derivation tree (or parse tree) is a tree representation of how a string is generated in a CFG.


Properties

  • Root → Start symbol

  • Internal nodes → Non-terminals

  • Leaves → Terminals

  • Reading leaves left to right gives the string


Importance

  • Shows structure of language

  • Used in compilers

  • Common exam question


4️⃣ Ambiguity in CFG

Ambiguous Grammar

A grammar is ambiguous if a string has more than one parse tree.


Example

Grammar:
E → E + E | E * E | id

Expression: id + id * id

➡ Two different parse trees possible


Key Point


5️⃣ Simplification of Context Free Grammars

Simplification means removing unnecessary productions without changing the language.


Steps of Simplification

(a) Removal of ε-productions

Productions of the form:
A → ε


(b) Removal of Unit Productions

Productions of the form:
A → B


(c) Removal of Useless Symbols


Result

Simplified grammar that generates the same language


6️⃣ Chomsky Normal Form (CNF)

Definition

A CFG is in Chomsky Normal Form if all productions are of the form:

Where:


Special Rule

S → ε allowed if ε ∈ L


Steps to Convert CFG to CNF

  1. Remove ε-productions

  2. Remove unit productions

  3. Remove useless symbols

  4. Convert long RHS into binary form

  5. Replace terminals in long productions


Importance

  • Used in parsing algorithms

  • Asked as long conversion question


7️⃣ Greibach Normal Form (GNF)

Definition

A CFG is in Greibach Normal Form if every production is of the form:

A → aα

Where:

  • a ∈ Σ

  • α ∈ V*


Characteristics


Importance

  • Useful in top-down parsing

  • Ensures derivations consume input symbols


8️⃣ Decision Algorithms for CFG

Decidable Problems

  1. Membership problem
    Given string w, determine whether w ∈ L(G)

  2. Emptiness problem
    Determine whether L(G) = ∅

  3. Finiteness problem
    Check if L(G) is finite


Undecidable Problem

  • Equivalence of two CFGs


🔑 CFG vs Regular Grammar

CFGRegular Grammar
More powerfulLess powerful
PDA requiredFA sufficient
Supports recursionNo recursion

🌐 What is a Chrome Extension?

   🌐 What is a Chrome Extension? A  Chrome Extension  is a small software program that adds extra features to your browser. 👉 Think like: ...