Problem Solving:
Problem solving techniques:
Algorithm:
Properties of Algorithms;
- Should be written in simple English
- Each and every instruction should be precise and unambiguous.
- Instructions in an algorithm should not be repeated infinitely.
- Algorithm should conclude after a finite number of steps.
- Should have an end point.
- Derived results should be obtained only after the algorithm terminates.
Qualities of a good algorithm
The following
are the primary factors that are often used to judge the quality of the
algorithms.
Time – To execute a program, the computer
system takes some amount of time. The lesser
is the time required, the better is the algorithm.
Memory – To execute a program, computer system
takes some amount of memory space.
The lesser is the memory required, the better is the algorithm.
Accuracy – Multiple algorithms may
provide suitable or correct solutions to a given problem, some of these may provide more accurate results than
others, and such algorithms may be suitable.
Example:
Write an
algorithm to print „Good Morning”
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
BUILDING BLOCKS OF ALGORITHMS (statements, state, control flow, functions)
Algorithms can be constructed from basic building blocks namely, sequence, selection and iteration.
Statements:
- input data-information given to the program
- process data-perform operation on a given input
- output data-processed result
Transition from
one process to another process under specified condition with in a time is
called state.
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
All the
instructions are executed one after another is called sequence execution.
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.
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.
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
Functions:
- For complex problems, the problem is been divided into smaller and simpler tasks during algorithm design.
Benefits of Using Functions
- code reuse
- Better readability
- Information hiding
- Easy to debug and test
- Improved maintainability
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
Psuedo code
Pseudocode is a detailed yet informal high-level description of a computer program or algorithm. It's designed to represent the program's logic in a language-agnostic manner, making it easier to understand for humans before actual coding in a specific programming language.
While Python itself is a high-level programming language, when people talk about pseudocode in Python, they often refer to a style of writing code that is more human-readable and resembles Python syntax without strictly adhering to the language's rules.
# Example of Pseudocode in Python-style
# Calculate the sum of two numbers
function sum_of_numbers(num1, num2):
sum = num1 + num2
return sum
# Ask for user input
input_num1 = get_user_input("Enter first number: ")
input_num2 = get_user_input("Enter second number: ")
# Convert input to numbers
number1 = convert_to_number(input_num1)
number2 = convert_to_number(input_num2)
# Calculate the sum
result = sum_of_numbers(number1, number2)
# Display the result
display_result(result)
Flowchart
Flowcharts are graphical representations of a process or algorithm, displaying the steps in a sequence. Although Python itself does not have built-in tools to create flowcharts, you can design them to plan out Python programs or algorithms using various software or even by hand. The rules for constructing flowcharts are relatively universal, regardless of the programming language being used.
Here are some basic rules for constructing flowcharts:
Start/End Symbol: Each flowchart must have a start and an end point. Typically, these are represented by an oval shape.
Input/Output Symbol: For getting input from users or displaying output, rectangles are often used in a flowchart.
Processing Steps: Use rectangles or other shapes to represent processing steps. These might involve calculations, comparisons, or any operation.
Decision Making: Use a diamond-shaped symbol to represent a decision point, typically involving a yes/no or true/false question.
Connectors/Arrows: Arrows show the flow of the process, connecting symbols to indicate the sequence of steps.
To create a flowchart for a Python program:
- Plan the Logic: Before you start coding, plan the logic and steps your program needs to take.
- Identify Symbols: Determine the symbols you'll use for start, end, input, output, processing, and decision-making steps.
- Draw the Flowchart: Use software tools like Lucidchart, Microsoft Visio, or even pen and paper to create the flowchart following the identified symbols and rules.
- Label the Symbols: Clearly label each symbol with the corresponding action or step.
This visual representation helps in understanding and organizing the logical flow of the program before implementing it in Python. It's an effective way to plan and communicate complex algorithms or processes.