Python for problem solving and logic building
Unit 1
Fundamentals of Computing:
The fundamentals of computing mean understanding how a computer works. A computer is
made of parts hardware (like the screen, keyboard, and the ‘brain’ called CPU) and software
or programs (like games or apps). The computer does four main jobs:
1.It takes information in (input)
2.Thinks about it or works on it (processing)
3.Shows the answers (output), and
4.Saves the stuff (storage).
Identification of Computational Problems:
Identification of computational problems means figuring out which problems can be solved
with the help of a computer and how to describe them in a way that a computer can
understand. It is the first step in critical thinking, consists of following steps:
• Problem Definition: State clearly what the problem is, what are inputs, what should
be the output, and any condition must be satisfied.
• Decomposition: Break the big problem into small, easier parts.
• Pattern Recognition: Find things in the problem that repeat or look similar.
• Abstraction: Focus only on the important details and ignore the extra stuff.
• Algorithm Design: Write clear steps that tell the computer how to solve the problem.
Algorithm:
an algorithm is a set of detailed, step-by-step instructions or rules for completing a task or
solving a problem, much like a recipe for cooking or the steps for tying your
shoes. Algorithms take some input, process it through a series of operations, and produce a
specific output.
Properties of Algorithm:
It should terminate after a finite time.
It should produce at least one output.
It should take zero or more input.
It should be deterministic means giving the same output for the same input case.
Every step in the algorithm must be effective i.e. every step should do some work.
1
BUILDING BLOCKS OF ALGORITHMS:
(Statements, State, Control flow, Functions)
1.Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
input data-information given to the program
process data-perform operation on a given input
output data-processed result
2.State:
State is defined as the condition of algorithm at a moment in a time. Algorithm can be in any
of the following states.
1. START state
2. INPUT state
3. PROCESS state
4. OUTPUT state
5. STOP state
Algorithm to find the sum of two numbers.
a. Start (START state)
b. Read A, B (INPUT state)
c. C=A+B (PROCESS state)
d. Print C (OUTPUT state)
3.Control flow:
The process of executing the individual statements in a given order is called control flow.
The control can be executed in three ways
1. sequence
2. selection
3. iteration
2
Sequence:
All the instructions are executed one after another is called sequence execution.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop
Selection:
A selection statement causes the program control to be transferred to a specific part of the
program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
3
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
In some programs, certain set of statements are executed again and again based upon
conditional test. i.e. executed more than one time. This type of execution is called looping or
iteration.
Example
Write an algorithm to print all natural numbers up to n
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
4.Functions:
Function is a sub program which consists of block of code (set of instructions) that
performs a particular task.
For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.
Key characteristics of functions:
Name:
Functions are identified by a unique name, which is used to call and execute them.
4
Parameters (Optional):
Functions can accept input values, known as parameters or arguments, which allow them to
perform operations on specific data.
Return Value (Optional):
Functions can optionally return a value as a result of their execution.
Body:
The function body contains the statements and logic that define the actions the function
performs.
Benefits of Using Functions
Reduction in line of code
code reuse
Better readability
Information hiding
Easy to debug and test
Improved maintainability
Example:
Algorithm for addition of two numbers using function
Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop
sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return
5
Notations:
(pseudo code, flow chart, programming language)
Notation is a system of symbols or signs that helps to represent complex concepts, values, or
ideas in a simplified and standardized manner.
1.Pseudocode:
It is a text-based way to describe algorithms using plain English mixed with programming-
like structure.
🔹 Key Notations:
BEGIN / END: Marks the start and end of the algorithm.
SET / ASSIGN: Used to initialize or update variables.
IF / THEN / ELSE: Conditional logic.
FOR / WHILE / DO: Looping constructs.
INPUT / OUTPUT: For user interaction.
CALL: Invokes a function or procedure.
Example: pseudo code to find greater number
6
1.Start
2.Input two numbers a,b.
3.If a is greater than b
4. print a is greater
5.else print b is greater.
6. Stop
2.Flowchart:
A flowchart is a graphical representation of an algorithm, process, or workflow. Flowchart
diagrams consist of shapes connected by lines and represent step-by-step processes to help in
decision-making, reduce ambiguity and enhance workflow.
Flowchart has following main symbols:
7
Example1: flowchart to find greatest of two numbers
Example 2: Flowchart to add two numbers
8
Example 3. flowchart to convert temperature from fahrenheit to celsius
3.Programming Language:
A programming language is a special kind of language that people use to tell computers what
to do. Just like we speak English or Hindi to communicate with each other, programmers use
languages like Python, Java, or C++ to talk to computers.
Key characteristics and concepts associated with programming languages include:
Syntax:
The specific rules and structure for writing code within a particular language.
Semantics:
The meaning and interpretation of the code written in a programming language.
Data Types:
Categories of values that can be stored and manipulated within a program (e.g., numbers,
text, booleans).
o Text (like "Hello")
o True/False values (called booleans)
o Numbers (like 5 or 3.14)
9
Variables:
A variable is a named container that holds values that are used in a program.
example: marks,rollno,x,yz;
Operators:
Symbols or keywords used to perform operations on data (e.g., arithmetic, logical,
comparison).
Control Structures:
Statements that dictate the flow of execution within a program (e.g., conditional statements
like if-else, looping constructs like for and while).
Functions/Procedures:
Reusable blocks of code that perform specific tasks.
Libraries and Frameworks:
Collections of pre-written code and tools that simplify development by providing ready-
made functionalities
Illustrative problems:
1. Finding minimum in a list
Problem description:
The problem is to find the minimum element in the given list of elements. Finding
minimum in a list of elements can be achieved in different ways. One method is to compare
each element with other. As an initial step, first element of the list is considered as minimum
element. And in each iteration, each element in the list is compared with the minimum. If the
element in the list is less than the minimum then swap both elements else compare with the
next element in the list. These steps are continued until the end of the list and finally print the
minimum.
Algorithm:
1. Start.
2. Read the list of elements.
3. Set first element in the list as minimum.
10
4. Move to the next element in the list.
5. If current element<minimum then
Set minimum =current element.
6. Repeat Step 4 and Step 5 until the end of the list is reached.
7. Print the minimum value in the list
8. Stop
Pseudo code:
BEGIN
READ the list of elements.
INITIALIZE min with the first element of the list
FOR each element in the list of elements
IF element is less than min
COPY element to min
ENDIF
ENDFOR
PRINT min as the minimum value
END
Algorithm to Insert a Card into a Sorted List:
1. Start at the beginning of the sorted list.
2. Compare the new card with each card in the list, one by one.
3. Find the first card in the list that is greater than or equal to the new card.
4. Insert the new card before that card.
5. If no such card is found (i.e., the new card is the largest), insert it at the end of the list.
Pseudo code:
INPUT: sorted_list, new_card
SET position = 0
WHILE position < length of sorted_list AND new_card>sorted_list[position]
DO
INCREMENT position by 1
END WHILE
INSERT new_card at position in sorted_list
OUTPUT: sorted_list
Guess an integer number in a range:
The "guess an integer number in a range" problem refers to a common programming exercise
or game where a program generates a random integer within a specified range, and the user
attempts to guess that number. The program then provides feedback (e.g., "too high," "too
low," or "correct") to guide the user's subsequent guesses.
11
Algorithm:
1.Start
2. Set the starting range (for example: 1 to 100)
3. Read the secret number n
4.Repeat:
5. Read a guess number
6. If guess > n then
Print "The guess is too high"
7. Else if guess < n then
Print "The guess is too low"
8. Else if (guess == n) then
Print "Number is found"
Stop
9. Else Go back to step 4 (repeat until number is found)
Tower of Hanoi Problem:
The Tower of Hanoi algorithm is a classic example of a recursive problem-solving approach.
We have three rods (let's call them A, B, and C) and a stack of n disks of different sizes
placed on rod A in decreasing size order (largest at the bottom, smallest at the top). The goal
is to move all disks from rod A to rod C using rod B as an auxiliary rod.
Rules:
1. We can move only one disk at a time.
2. A disk can only be placed on top of a larger disk or on an empty rod.
3. Move all disks from rod A to rod C.
Plain Algorithm (Recursive)
1. To move n disks from rod A to rod C using rod B.
2. Move n-1 disks from rod A to rod B using rod C as auxiliary.
3. Move the largest disk (the nth disk) from rod A to rod C.
4. Move the n-1 disks from rod B to rod C using rod A as auxiliary.
12