Introduction (30.07.2024)
Introduction (30.07.2024)
31-07-2024 1
Introduction
• Purpose
Every engineering student should learn how to program for problem solving in the
domain and can optimize the tech-task easily and efficiently with computer
programming knowledge
• Objectives
1) To provide exposure to basic problem-solving techniques using computers.
2) To inculcate the art of logical thinking abilities and propose novel solutions for real
world problems through programming language constructs.
• Outcomes
1) Classify various algorithmic approaches, categorize the appropriate data
representation, and demonstrate various control constructs.
2) Choose appropriate programming paradigms.
3) Interpret and handle data using files to propose solution through reusable modules.
4) Idealize the importance of modules and packages.
31-07-2024 2
31-07-2024 3
Programming paradigm (Contd…)
Examples:
Programming paradigm (Contd…)
Object oriented programming
The program is written as a collection of classes and object which are
meant for communication.
The smallest and basic entity is object and all kind of computation is
performed on the objects only.
More emphasis is on data rather procedure.
It can handle almost all kind of real life problems which are today in
scenario.
Advantages:
Data security
Inheritance
Code reusability
Flexible and abstraction is also present
Programming paradigm (Contd…)
Examples:
NESL (National E-Governance Services Limited) - one of the oldest one and
C/C++ also supports because of some library function.
Programming paradigm (Contd…)
Python Programming
Control Structures
Collections
Computer
Programming:
Strings and Regular
Python
Functions and Files Expressions
References:
1. Eric Matthes, Python Crash Course: A Hands-On, Programming, 2nd Edition, No starch Press, 2019.
2. Martic C Brown, Python: The Complete Reference, 4th Edition, McGraw Hill Publishers, 2018.
3. John V. Guttag, Introduction to computation and programming using python: with applications to understanding data, 2nd
Edition, MIT Press, 2016.
31-07-2024 16
Course Details
• Embedded Theory
Theory component and Laboratory component
• Course Credits
31-07-2024 17
Rubrics Assessment (Evaluation)
Component Max. Marks Weightage Total
Marks (%) Marks
Periodic Assessment Test (PAT) - 3 10 * 3 10 30
Midterm Test 50 20 20
FAT* 50 40 40
Total 100
31-07-2024 18
What is Python?
31-07-2024 19
Characteristics of Python:
Python is Interpreted − Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is similar
to PERL and PHP.
Python is Interactive − You can actually sit at a Python prompt and interact
with the interpreter directly to write your programs.
31-07-2024 21
Characteristics of Python:
Portable − Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
Extendable − You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
Databases − Python provides interfaces to all major commercial databases.
GUI Programming − Python supports GUI applications that can be created
and ported to many system calls, libraries andwindows systems, such as
Windows MFC, Macintosh, and the XWindow system of Unix.
Scalable − Python provides a better structure and support for large programs
than shell scripting.
31-07-2024 22
Way to Run Python:
31-07-2024 23
Introduction to Problem Solving
• Solutions that cannot be reached through a direct set of steps are called
heuristic solution
31-07-2024 26
Introduction to Problem Solving
• Computers are built to solve problems with algorithmic solutions, which are
often difficult or very time consuming when input is large
31-07-2024 27
Introduction to Problem Solving
• Field of computers that deals with heuristic types of problems is called
Artificial Intelligence (AI)
• Artificial intelligence enables a computer to do things like human by building
its own knowledge bank
• As a result, the computer’s problem-solving abilities are similar to those of a
human being.
• Artificial intelligence is an expanding computer field, especially with the
increased use of Robotics.
Computational Problems:
• Computation is the process of evolution from one state to another in
accordance with some rules.
31-07-2024 28
Introduction to Problem Solving
• Where the answer for every instance is either yes or No
Decision • Deciding whether the given number is prime
Problems
31-07-2024 29
Introduction to Problem Solving - steps
Defining / Understanding a Problem
31-07-2024 Testing 30
Introduction to Problem Solving- Logic (Basis for solving
any problem)
• Definition : A method of human thought that involves thinking in a linear,
step by step manner about how a problem can be solved
• Logic is a language for reasoning.
It is a collection of rules we use when doing reasoning.
• Example: John's mum has four children.
• The first child is called April.
• The second May.
• The third June.
• What is the name of the fourth child?
31-07-2024 31
Problem Solving by Computer
• Phase 1:
• Organizing the problem or pre-programming phase
(Problem analysis diagram, flow-chart, algorithm)
• Phase 2:
• Programming phase (coding the program)
31-07-2024 32
Problem Solving by Computer
Organizing the problem or pre-programming phase – tools
31-07-2024 33
Problem Solving by Computer- PAC
Analyzing the problem – Problem Analysis Chart/Diagram
PAC is used as basis in establishing method of coding, testing, and data type description
Understand and analyze the problem to determine whether it can be solved by a computer.
31-07-2024 34
Problem Solving -PAC
Output Solution
Data Processing Alternatives
given in List of
the List of ideas for
problem processing Output the
required requirement solution of
or
or the
provided problem
by the user procedures
31-07-2024 35
PAC – example1
• Payroll Problem
Calculate the salary of an employee who works by hourly basis. The formula to be used is
Salary = Hour works * Pay rate
Solution
Data Processing Output
Alternatives
1. Define the hours
worked
Hours Salary = and pay rate as
work, constants.
Hours work * Salary 2. Define the hours
Pay rate pay rate worked
and pay rate as input
values.
31-07-2024 36
PAC – Example2
• Write a Problem Analysis Chart (PAC) to convert the distance in
miles to kilometers where 1.609 kilometers per mile.
Alternate
Alternate Alternate
Alternate
Data Processing Output Solutions Solutions
Solutions Solutions
1. Define the the
1. Define 11.. Define
Define the
the
miles as as
miles miles
miles as
as
Distance in Kilometers = Distance in constants.
constants. constants.
constants.
miles 1.609 x kilometers ∗2.
∗2. ∗2.
Define the the
Define ∗2. Define
Define the
the
miles miles as input
miles as input miles
miles as
as input
input
values.
values. values.
values.
31-07-2024 37
PAC – example3
• Write a Problem Analysis Chart (PAC) to find an area of a circle
where area = pi * radius * radius
Data Processing
Processing Output
Output
area = 3.14 x
radius radius x area
radius
31-07-2024 38
Problem Solving by Computer- HIPO
31-07-2024 39
Problem Solving by Computer- HIPO
• Programming which use this approach (problem is divided into
subtasks) is called Structured Programming
Main
Module
Module-1 Module-2
31-07-2024 40
Problem Solving by Computer- HIPO
• HIPO chart for Payroll problem
PAYROLL
31-07-2024 41
Problem Solving by Computer- HIPO
Extended Payroll problem
• Write a program to calculate both the gross pay and the net pay of
every employee of your company. Use the following formulae for
calculation:
Gross pay = number of hours worked * pay rate
Net pay = gross pay – deductions
• The program should also print the cheque that tells the total net pay.
31-07-2024 42
Problem Solving by Computer- HIPO
Extended Payroll problem -PAC
31-07-2024 43
Problem Solving by Computer- HIPO
Extended Payroll problem -HIPO
Payroll
Get
Get Calculate
Hourly
Pay rate Deductions
Worked
31-07-2024 44
Developing an Algorithm
• Algorithm is a step-by-step procedure, which defines a set of instructions to
be executed in a certain order to get the desired output.
31-07-2024 45
Developing an Algorithm cont…
• An algorithm should have the following characteristics −
1. Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
2. Input − An algorithm should have 0 or more well-defined inputs.
3. Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
4. Finiteness − Algorithms must terminate after a finite number of steps.
5. Feasibility − Should be feasible with the available resources.
6. Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.
31-07-2024 46
Developing an Algorithm cont…
How to Write an Algorithm?
• There are no well-defined standards for writing algorithms.
• Rather, it is problem and resource dependent.
• Algorithms are never written to support a particular programming code.
• As we know that all programming languages share basic code constructs like
loops (do, for, while), flow-control (if-else), etc.
• These common constructs can be used to write an algorithm.
• Sometimes algorithm writing is a process and is executed after the problem
domain is well-defined.
• That is, we should know the problem domain, for which we are designing a
solution.
31-07-2024 47
Developing an Algorithm cont…
• Example
• Problem − Design an algorithm to add two numbers and display the result.
• step 1 − START
• step 2 − declare three integers a, b & c
• step 3 − define values of a & b
• step 4 − add values of a & b
• step 5 − store output of step 4 to c
• step 6 − print c
• step 7 − STOP
31-07-2024 48
Developing an Algorithm cont…
• Algorithms tell the programmers how to code the program.
• Alternatively, the algorithm can be written as −
• step 1 − START ADD
• step 2 − get values of a & b
• step 3 − c ← a + b
• step 4 − display c
• step 5 − STOP
• In design and analysis of algorithms, usually the second method is used to
describe an algorithm.
• It makes it easy for the analyst to analyze the algorithm ignoring all unwanted
definitions.
31-07-2024 49
Developing an Algorithm cont…
• He can observe what operations are being used and how the process is
flowing.
• Writing step numbers, is optional.
• We design an algorithm to get a solution of a given problem.
• A problem can be solved in more than one ways.
31-07-2024 50
Flowcharts
• Flowcharts graphically represent the flow of a program.
• There are four basic shapes used in a flow chart.
31-07-2024 51
Flowcharts cont…
Symbol Symbol Name Purpose
Used at the beginning and end of the
Start/Stop algorithm to show start and end of the
program.
Indicates processes like mathematical
Process
operations.
Used for denoting program inputs and
Input/Output
outputs.
Stands for decision statements in a
Decision program, where answer is usually Yes or
No.
31-07-2024 52
Flowcharts cont…
Symbol Symbol Name Purpose
Shows relationships between different
Arrow
shapes.
Connects two or more parts of a
On-page
flowchart, which are on the same
Connector
page.
Off-page Connects two parts of a flowchart
Connector which are spread over different pages.
31-07-2024 53
Flowcharts cont…
• Guidelines for Developing Flowcharts
• Flowchart can have only one start and one stop symbol
• On-page connectors are referenced using numbers
• Off-page connectors are referenced using alphabets
• General flow of processes is top to bottom or left to right
• Arrows should not cross each other.
31-07-2024 54
Flowcharts cont…
• Advantages of Flowchart:
• Flowcharts are a better way of communicating the logic of the system.
• Flowcharts act as a guide for blueprint during program designed.
• Flowcharts help in debugging process.
• With the help of flowcharts, programs can be easily analyzed.
• It provides better documentation.
• Easy to trace errors in the software.
• Easy to understand.
• The flowchart can be reused for inconvenience in the future.
• It helps to provide correct logic.
31-07-2024 55
Flowcharts cont…
• Disadvantages of Flowchart:
• It is difficult to draw flowcharts for large and complex programs.
• There is no standard to determine the amount of detail.
• Difficult to reproduce the flowcharts.
• It is very difficult to modify the Flowchart.
• Making a flowchart is costly.
• If changes are done in software, then the flowchart must be redrawn.
31-07-2024 56
Flowcharts cont…
• Arrows connect the basic shapes in a flowchart.
• The shapes and arrows of a flowchart describe the flow of a program from
start to end.
• Flowcharts typically flow from the top to the bottom or flow from the left to
the right.
31-07-2024 57
Flowcharts cont…
• A flowchart that describes this simple program is shown.
• The Python code that corresponds to this flowchart is:
# start
print("Output!")
# end
31-07-2024 58
Flowcharts cont…
• A description of a program that includes a calculation is below:
31-07-2024 59
Flowcharts cont…
• The Python code that corresponds to this flow chart is:
# start
num = input("Enter a number: ")
num = float(num)
num_plus_2 = num + 2
print(num_plus_2)
# end
31-07-2024 60
Flowcharts cont…
31-07-2024 61
Flowcharts cont…
31-07-2024 62
Flowcharts cont…
31-07-2024 63
Flowcharts cont…
31-07-2024 64
Flowcharts cont…
31-07-2024 65
Flowcharts cont…
31-07-2024 66
Pseudocode
• Pseudocode is a method of planning which enables the programmer to plan
without worrying about syntax.
• It’s simply an implementation of an algorithm in the form of annotations and
informative text written in plain English.
• It has no syntax like any of the programming language and thus can’t be compiled
or interpreted by the computer.
• Pseudocode, as the name suggests, is a false code or a representation of code which
can be understood by even a layman with some school level programming
knowledge.
• Pseudocode is a term which is often used in programming and algorithm based
fields.
• It is a methodology that allows the programmer to represent the implementation of
an algorithm.
31-07-2024 67
Pseudocode cont…
• Advantages of Pseudocode
• Improves the readability of any approach.
• It’s one of the best approaches to start implementation of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart.
• Also works as a rough documentation, so the program of one developer can
be understood easily when a pseudo code is written out.
• In industries, the approach of documentation is essential. And that’s where a
pseudo-code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a
program should do, hence making the code construction phase easier for the
programmer.
31-07-2024 68
Pseudocode cont…
• Five major steps in Python pseudocode
• The pseudocode in python must be line by line so every statement involved
must be represented as just one single line in pseudocode.
• Just as in python code how indentations are used, these indentations must be
preferred in python pseudocode too.
• Ensure that each statement of the pseudocode is simple and easy to
understand.
• Ensure the first word of the pseudocode is always in Upper case letters.
31-07-2024 69
Pseudocode cont…
• Special Keyword:
• START: To begin the pseudocode.
• INPUT: Take input from the user.
• PRINT: To print the output on the screen.
• READ/GET: Input format while reading data from the file.
• SET, INIT: Initialize a value.
• INCREMENT, BUMP: Increase the value of the variable, equivalent to a++.
• DECREMENT: Decrease the value of the variable, equivalent to a--.
• COMPUTE, CALCULATE, DETERMINE: To calculate the expression result.
31-07-2024 70
Pseudocode cont…
• Write the FizzBuzz Algorithm Using pseudocode
• FizzBuzz is a standard coding, in which we write a code to print Fizz, Buzz, and
FizzBuzz when the multiple of 3 and 5 occurs.
• The problem states:
• Write a code that prints each number from 1 to 30 in a new line.
• Print "Fizz" if the number is the multiple of 3.
• Print "Buzz" if the number is a multiple of 5.
• For a number that is multiple of both 3 and 5 print “FizzBuzz.”
31-07-2024 71
Pseudocode cont…
• FizzBuzz Pseudocode
num : 1
FOR num -> 1 to 20
IF num MOD 15 ==0
PRINT “FizzBuzz”
ELSE IF num MOD 3 ==0
PRINT “Fizz”
ELSE IF num MOD 5==0
PRINT “Buzz”
ELSE PRINT num
ENDIF
ENDFOR
31-07-2024 72
Pseudocode cont…
• Equivalent Python Code:
for num in range(1, 21):
if num % 15 ==0:
print("FizzBuzz")
elif num %3 ==0:
print("Fizz")
elif num %5==0:
print("Buzz")
else:
print(num)
31-07-2024 73
Pseudocode cont…
• Python pseudocode Examples
• Retrieve the number to be reversed from the user into variable
sample_number.
• Initialize the temporary variable test_number as zero.
• Perform a while loop until the sample_number variable is greater than zero.
• Modulus the sample_number variable by 10 and store the remainder.
• Multiply the temporary number of test_number by 10 and add the returned
value to the remainder.
• Print the generated test_number onto the console.
31-07-2024 74
Pseudocode cont…
• Pseudocode:
sample_number = int(input("Number to be reversed: "))
test_number = 0
while(sample_number>0):
remainder_number = sample_number % 10
test_number = (test_number * 10) + remainder_number
sample_number = sample_number//10
print("Value after reverse : {}".format(test_number))
31-07-2024 75
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE LARGEST OF TWO NUMBERS.
BEGIN
NUMERIC nNum1,nNum2
DISPLAY "ENTER THE FIRST NUMBER : "
INPUT nNum1
DISPLAY "ENTER THE SECOND NUMBER : "
INPUT nNum2
IF nNum1 > nNum2
DISPLAY nNum1 + " is larger than "+ nNum2
ELSE
DISPLAY nNum2 + " is larger than " + nNum1
END
31-07-2024 76
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE SUM OF TWO NUMBERS.
begin
numeric nNum1,nNum2,nSum
display "ENTER THE FIRST NUMBER : "
accept nNum1
display "ENTER THE SECOND NUMBER : "
accept nNum2
compute nSum=nNum1+nNum2
display "SUM OF THESE NUMBER : " nSum
end
31-07-2024 77
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE AREA OF RECTANGLE.
begin
numeric nLen,nBrd,nAre
display "ENTER THE LENGTH OF RECTANGLE : "
accept nLen
display "ENTER THE BREADTH OF RECTANGLE : "
accept nBrd
nAre=nLen*nBrd
display "AREA OF RECTANGLE : " nAre
end
31-07-2024 78
Pseudocode cont…
• WRITE A PSEUDOCODE TO FIND THE GREATEST OF TWO NUMBERS.
begin
numeric nNum1, nNum2
display "ENTER THE FIRST NUMBER : "
accept nNum1
display "ENTER THE SECOND NUMBER : "
accept nNum2
if(nNum1>nNum2)
begin
display "GREATEST ONE : " nNum1
end
else
begin
display "GREATEST ONE : " nNum2
end
end
31-07-2024 79
Conclusion
31-07-2024 80