0% found this document useful (0 votes)
401 views

Unit-1 Python Notes PDF

The document is a syllabus for a course on problem solving and Python programming. It outlines 5 units that will be covered: 1) computational thinking and problem solving, 2) data types, expressions, and statements, 3) control flow, functions, and strings, 4) lists, tuples, and dictionaries, and 5) files, modules, and packages. Each unit lists topics that will be taught and example problems to illustrate the concepts. The syllabus provides an overview of the structure and content of the course.

Uploaded by

michaelmathu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
401 views

Unit-1 Python Notes PDF

The document is a syllabus for a course on problem solving and Python programming. It outlines 5 units that will be covered: 1) computational thinking and problem solving, 2) data types, expressions, and statements, 3) control flow, functions, and strings, 4) lists, tuples, and dictionaries, and 5) files, modules, and packages. Each unit lists topics that will be taught and example problems to illustrate the concepts. The syllabus provides an overview of the structure and content of the course.

Uploaded by

michaelmathu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

1

GE3151 PROBLEM SOLVING AND PYTHON


PROGRAMMING

SYLLABUS

UNIT I COMPUTATIONAL THINKING AND PROBLEM SOLVING 9

Fundamentals of Computing – Identification of Computational Problems -Algorithms, building blocks of


algorithms (statements, state, control flow, functions), notation (pseudo code, flow chart, programming
language), algorithmic problem solving, simple strategies for developing algorithms (iteration, recursion).
Illustrative problems: find minimum in a list, insert a card in a list of sorted cards, guess an integer number
in a range, Towers of Hanoi.

UNIT II DATA TYPES, EXPRESSIONS, STATEMENTS 9

Python interpreter and interactive mode, debugging; values and types: int, float, boolean, string , and list;
variables, expressions, statements, tuple assignment, precedence of operators, comments; Illustrative
programs: exchange the values of two variables, circulate the values of n variables, distance between two
points.

UNIT III CONTROL FLOW, FUNCTIONS, STRINGS 9

Conditionals:Boolean values and operators, conditional (if), alternative (if-else),chained conditional (if-
elif-else);Iteration: state, while, for, break, continue, pass; Fruitful functions: return values,parameters,
local and global scope, function composition, recursion; Strings: string slices,immutability, string
functions and methods, string module; Lists as arrays. Illustrative programs: square root, gcd,
exponentiation, sum an array of numbers, linear search, binary search.

UNIT IV LISTS, TUPLES, DICTIONARIES 9

Lists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list parameters;
Tuples: tuple assignment, tuple as return value; Dictionaries: operations and methods; advanced list
processing - list comprehension; Illustrative programs: simple sorting, histogram, Students marks
statement, Retail bill preparation.

UNIT V FILES, MODULES, PACKAGES 9

Files and exceptions: text files, reading and writing files, format operator; command line arguments, errors
and exceptions, handling exceptions, modules, packages; Illustrative programs: word count, copy file,
Voter’s age validation, Marks range validation (0-100). TOTAL : 45 PERIODS

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
2

UNIT I COMPUTATIONAL THINKING AND PROBLEM SOLVING 9

Fundamentals of Computing – Identification of Computational Problems -Algorithms, building


blocks of algorithms (statements, state, control flow, functions), notation (pseudo code, flow chart,
programming language), algorithmic problem solving, simple strategies for developing
algorithms (iteration, recursion). Illustrative problems: find minimum in a list, insert a card in a
list of sorted cards, guess an integer number in a range, Towers of Hanoi.

I. FUNDAMENTALS OF COMPUTING

COMPUTER:

Computer is an electronic device that accepts data as input, processes the input data by performing
mathematical and logical operations on it, and gives the desired output.

Basic functions or operations of Computer:


• Input
• Processing
• Output
• Storing
• Controlling
Input:
It is the process of getting the data from the user or from somewhere else into the computer to
process.
Processing:
It is the process of converting the input to output.
Output:
It is the outcome or result of the process.
Storing:
It is the process of saving the data. So that it can be retrieved whenever needed.
Controlling:
It is the process of directing the sequence in which the operations to be performed.

Characteristics Of Computers
• Speed
• Accuracy
• Diligence
• Storage capability
• Versatility

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
3
Speed:
• Speed is measured as number of instructions executed per second.
• The computer can process data very fast, at the rate of millions of instructions per second.
• Speed can be measured by milli seconds/ micro seconds/ nano seconds/ pico seconds.
Accuracy:
• Computer provides a high degree of accuracy.
• For example, the computer can accurately give the result of a division of any two numbers up to 10
decimal places.
Deligence:
• When used for a longer period of time, the computer does not get tired or lack of concentration.
• It can perform long and complex calculations with the same speed and accuracy from the start till
the end.
Storage capability:
• Large volumes of data and information can be stored in the computer and also retrieved whenever
required.
• A limited amount of data can be stored, temporarily, in the primary (main) memory.
• Secondary storage devices like floppy disk and compact disk can store a large amount of data
permanently.
Versatility:
• Computer is versatile in nature.
• It can perform different types of tasks with the same ease.

GENERATIONS OF COMPUTERS

• The computer has evolved from a large-sized simple calculating machine to a simple but much more
powerful machine. The evolution of computer to the current state is defined in terms of the generations
of the computers.
• Currently there are five generations of the computers.
• Generations are categorized on the basis of
1. Technology used (hardware and software).
2. Computing characteristics (speed).
3. Physical appearance (size).
4. Applications (Uses).

First Generation (1940 to 1956) –using vacuum tubes


1. Hardware technology:
➢ Used vacuum tubes for circuitry and magnetic drums for memory.
➢ Input was given through punched cards, paper tapes.
➢ Output was displayed as printouts.
2. Software technology:
➢ Instructions were written in machine language.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
4
➢ Machine language uses 0s and 1s for coding of the instructions.
3. Computing characteristics:
➢ Computation time was in milliseconds(10-3Sec).
4. Physical appearance:
➢ Computers were enormous in size and required a large room for installation.
5. Applications:
➢ For scientific applications
6. Examples:
➢ Universal automatic computer(UNIVAC)
➢ Electronic numeric integrator and computer (ENIAC)
➢ Electronic discrete variable automatic computer(EDVAC)
Limitations:
➢ Used a large number of vacuum tubes and thus generated a lot of heat.
➢ They consumed a great deal of electricity.
➢ Expensive to operate.
➢ Required constant maintenance.
➢ Since it used machine language, they were difficult to program.
➢ One program at a time.

Second generation(1956 to 1963) –using transistors


1. Hardware technology:
➢ Used transistors which allowed computers to become smaller, faster, cheaper, energy
efficient and reliable.
➢ Used magnetic core technology for primary memory.
➢ Used Magnetic tapes and magnetic disks for Secondary memory.
➢ Input was given through punched cards.
➢ Output was using Printouts.
➢ Used the concept of a stored program, where instructions were stored in the memory.
2. Software Technology:
➢ Instructions were written using Assembly language.
➢ Assembly language uses mnemonics like ADD for addition, SUB for subtraction etc..
➢ Example programming languages are COBOL, FORTRAN.
3. Computing Characteristics:
➢ Computation time was in microseconds (10-6 Sec).
4. Physical appearance:
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
5
➢ Reduced size compared to first generation computers.
5. Applications:
➢ Cost of commercial production was very high.
6. Examples:
➢ PDP-8 –Programmed Data Process.
➢ IBM 1401 – International Business Machine
➢ CDC 1604 – Control Data Corporation.
Advantages
➢ Generated lot of heat but much less than the first generation computers.
➢ Required less maintenance than first generation computers

Third Generation (1964-1971) – using Integrated Circuits
1. Hardware Technology:
➢ Uses Integrated Circuit (IC) chips.
➢ In an IC chip, multiple transistors are placed on a silicon chip.
➢ Input was given through Keyboard.
➢ Output was displayed through Monitor.
2. Software Technology:
➢ Uses Operating System which allowed different applications to run at the same time.
➢ High Level Languages used for programming like C,C++ etc.
3. Computing Characteristics:
➢ Computation time was in nanoseconds(10-9Sec)
4. Physical appearance:
➢ Quite small compared to the second generation computers.
5. Applications:
➢ Computers became accessible to mass audience.
➢ Computers were produced commercially.
6. Examples:
➢ IBM 370 , PDP 11

Advantages:
➢ Use less power and heat than second generation.
➢ Cost of the computer is reduced.
➢ Less maintenance cost.

Fourth Generation (1971 – Present) – using microprocessors


1. Hardware Technology:
➢ Use LSI(Large Scale Integration) and VLSI(Very Large Scale Integration) technology.
➢ VLSI allows hundreds and thousands of components to be integrated in a small chip.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
6
➢ Used Microprocessor- a chip containing millions of transistors and components and designed
using LSI and VLSI technology.
➢ This generation gave rise to Personal Computers (PC).
➢ Semiconductor for primary memory.
➢ Magnetic disks for secondary memory.
➢ Development of pointing devices like mouse and handheld devices.
➢ The Linking of computers is another key development.
2. Software Technology:
➢ Supported GUI(Graphical User Interface) , OS like MS-DOS and MS-windows.
➢ High-Level Programming Language used for writing programs.
3. Computing characteristics:
➢ Computation time was in picoseconds (10-12Sec).

4. Physical appearance:
➢ Smaller than computers in previous generation.
5. Applications:
➢ Widely available for commercial purpose.
➢ PC became available for home user.
Advantages:
➢ Computers are smaller and cheaper.
➢ Portable and reliable.
➢ Generates much lesser heat and less maintenance.
➢ Easy to use and learn.
➢ Networking has resulted in resource sharing and easy communication.
Fifth Generation (Present and next) – using Artificial Intelligence.
1. Hardware Technology:
➢ To develop computers those are capable of learning and self-organization.
➢ They use Super Large Scale Integrated Chips (SLSI) to store millions of components in a
single chip.
➢ It also uses parallel processing that allows several instructions to be executed in parallel.
➢ It consists of large memory.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
7
2. Software Technology:
➢ Fifth generation computers based on Artificial Intelligence (AI)
➢ They try to simulate the human way of thinking and reasoning.
➢ It includes areas likeExpert system (ES), Natural Language Processing (NLP), Speech
recognition, voice recognition, robotics etc.

Comparison between Different generations


Characteristics First Second Third Fourth Fifth
generation generation generation generation generation
Hardware Vacuum Transistors, Integrated LSI,VLSI,
Technology tubes, magnetic Circuits(IC) Microprocessor Uses
magnetic core chips, ,PC, Artificial
drums, technology, keyboard, Semiconductor Intelligence
punched magnetic Monitor Memory, (AI),
cards, paper tapes and linking of Parallel
tapes and magnetic computers, Processing,
printouts disks. Mouse and Applications
Handheld include,
Devices Expert
Software Machine Assembly Operating GUI, OS like system(ES),
Technology Language Language system MS-DOS and Natural
High-level MS-windows Language
languages High-level Processing
Programming (NLP),
languages speech
Computing Computation Computation Computation Computation recognition,
Characteristics time in time in time in time in voice
milliseconds microseconds nanoseconds picoseconds recognition,
Physical Enormous in Reduced size Quite small Smaller than robotics
appearance size compared to compared to computers of
first second previous
generation generation generation
computers computers.
Applications For Cost of Commercial Widely
scientific commercial purpose available for
applications production commercial
was very high purposes, PC
for home users.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
8
Examples UNIVAC, PDP-8, IBM 370, Intel 4004,
ENIAC, IBM 1401, PDP 11 IBM, Apple’s
EDVAC CDC 1604 Macintosh

BASIC ORGANIZATION OF A COMPUTER

Computer is an electronic device that accepts data as input, processes the input data by performing
mathematical and logical operations on it, and gives the desired output.

Four parts of computer system:


1. Hardware - Mechanical part of computer, e.g., keyboard, monitor, hard disk drive etc.
2. Software – Set of instructions (Program) and documentation. Instructs the computer about the task
to be performed.
3. Data – Isolated values or raw facts. Provided as input to computer for processing.
4. Users – People who write computer programs or interact with computer.

Input-Process-Output Concept:
A computer is an electronic device that (1) accepts data (2) processes data (3) generates output and
(4) stores data. The concept of generating output information from the input is also referred to as input-
process-output concept.
Components of Computer Hardware:
The computer system hardware comprises of three main components,
1. Input/output unit(I/O Unit)
2. Central Processing unit (CPU Unit)
3. Memory unit

Input/output Unit:
• Users interact with the computer via I/O unit.
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
9
• Input unit accepts data from user.
• Some of the input devices are keyboard, trackball, mouse.
• Output unit provides processed data to user.
• Some of the output devices are monitor and printer.

CPU- Central Processing Unit:


• CPU Controls, coordinates, supervises operations of computer.
• It is responsible for processing of the input data.
• CPU is also called the brain of the computer.
• CPU executes stored program instructions, i.e. instructions and data stored in memory before
execution.

Parts of CPU are,


1. ALU (Arithmetic and Logic Unit)
2. CU (Control Unit)
3. Registers
➢ Processing of CPU includes the following,
• CPU gets data and instructions from memory.
• It interprets source program to Object program.
• Then it sends processed data or result to memory.

Arithmetic Logic Unit (ALU):


• ALU performs all the arithmetic and logic operations on the input data.
• ALU uses registers to hold data that is being processed.
• It consists of two units
• Arithmetic unit
• Logic unit

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
10
1. Arithmetic Unit
- Performs arithmetic operations on the data
- E.g.Addition, subtraction, multiplication, division.
2. Logic Unit
- Performs logic operations.
- E.g. Greater than, less than or equal to condition.

Control Unit (CU)


• Control unit does not do actual processing of data; it organizes the processing of data and
instructions.
• It acts as a supervisor and controls and coordinates the activity of the other units of computer.
• It coordinates the input and output devices of a computer.
• CU tells when to fetch data and instructions, what to do, where to store the results, sequencing of
events during processing etc,.
• CU also holds CPU’s Instruction Set (list of all operations that CPU can perform)
Registers:
• Registers are high speed storage areas within the CPU, with least storage capacity.
• Used for the temporary storage of data, instructions, addresses, intermediate results.
• They are directly accessed and manipulated by the CPU during instruction execution.
• Registers are also called as CPU’s working memory.
• Some of the registers are,
➢ PC (Program Counter)
➢ IR (Instruction Register)
PC IR
➢ MAR (Memory Address Register)
MAR ACC
➢ ACC (Accumulator)
➢ MBR (Memory Buffer Register)
MBR DR
➢ DR (Data Register)
• The number of registers can vary from ten to many hundreds depends on type and complexity of
processor.
• Size of register (word size) may be 8, 16, 32 or 64-bit.
• Nowadays, PCs have 64-bit registers.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
11
Memory Unit
• Memory unit stores the data, instructions, intermediate results and output.
• Memory unit consists of three types
1. Cache Memory
2. Primary or Main memory
3. Secondary memory
1. Cache Memory
• Cache memory is a very high speed memory placed in between RAM and CPU.
• Cache memory stores the frequently accessed data; it increases the speed of processing.
• CPU first checks cache for data in the cache, if data is not found in cache, then looks for it in
RAM.

CPU Cache Memory RAM

2.Primary Memory:
• Primary memory is the main memory of computer. It is used to store data and instructions
temporarily during the processing of data.
• Also called as Main memory / System memory / Temporary memory / Volatile memory.
• It is a Semiconductor memory.
• Two types:
1. RAM (Random Access Memory)
2. ROM (Read Only Memory)

RAM( Random Access Memory)


• RAM is Volatile –i.e. temporary storage (it stores data whn the computer is on).
• The information stored in RAM gets erased when the computer is turned off.
• RAM provides temporary storage of data and instructions.
• Limited storage capacity, due to its high cost.
• Types:
1. Static RAM
2. Dynamic RAM

ROM (Read Only Memory)


• ROM is Non-volatile, i.e. Permanent storage
• But it is a read only memory.
• Stores standard processing programs that permanently reside in computer.
• ROMcomes programmed by manufacturer
• Types are
1. PROM (Programmable Read Only Memory)
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
12
2. EPROM (Erasable Programmable Read Only Memory)
3. EEPROM (Electrically Erasable Programmable Read Only Memory)

3.Secondary Memory:
• Secondary memory stores data and instructions permanently. The information can be stored in
secondary memory for a long time and it is permanent in nature.
• Also called as Permanent memory / Non-volatile memory / External memory.
• It provides back-up storage for data and instructions.
• It has high storage capacity than primary memory.
• It is also cheaper than primary memory.
• Examples: Hard disk, Magnetic tapes, Magnetic disks, Floppy disks, Optical disk

II. IDENTIFICATION OF COMPUTER PROBLEM


Problem Solving:
➢ Problem solving is the systematic approach to define the problem and creating number of
solutions.
➢ The problem solving process starts with the problem specifications and ends with a correct
program.
PROBLEM SOLVING TECHNIQUES
➢ Problem solving technique is a set of techniques that helps in providing logic for solving a
problem.
Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. Programs

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
13
ALGORITHM

• It is defined as a sequence of instructions that describe a method for solving a problem. In


other words it is a step by step procedure for solving a problem.
INPUT ALGORITHM OUTPUT

Rules
• 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.

BUILDING BLOCKS OF ALGORITHM (statements, state, control flow, functions)

• As algorithm is a part of the blue-print or plan for the computer program.


• An algorithm is constructed using following blocks.
➢ Statements
➢ States
➢ Control flow
➢ Function

Statements

• Statements are simple sentences written in algorithm for specific purpose.


• Statements may consist of assignment statements, input/output statements, comment statements

Example:

• Read the value of ‘a’ and ‘b’ //This is input statement

• Calculate c=a+b //This is assignment statement

• Print the value of c // This is output statement

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
14
• Comment statements are given after // symbol, which is used to tell the purpose of the line.

States

• An algorithm is deterministic automation for accomplishing a goal which, given an initial


state, willterminate in a defined end-state.
• An algorithm will definitely have start state and end state.

Control Flow

• Control flow which is also stated as flow of control, determines what section of

code is to run in program at a given time.

There are three types of flows, they are

1. Sequential control flow

2. Selection or Conditional control flow

3. Looping or repetition control flow

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
15

1. Sequential control flow:

• The name suggests the sequential control structure is used to perform the action one after another.
• Only one step is executed once.
• The logic is top to bottom approach.

Example

Description: To find the sum of two numbers.

1. Start

2. Read the value of ‘a’

3. Read the value of ‘b’

4. Calculate sum=a+b

5. Print the sum of two number

6. Stop

2. Selection or Conditional control flow

• Selection flow allows the program to make choice between two alternate paths

based oncondition. It is also called as decision structure.

Basic structure:

IF CONDITION is TRUE then

perform some action

ELSE IF CONDITION is FALSE then

perform some action

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
16

Example:

1.finding greatest of two numbers.

1. Start

2. Read a

3. Read b

4. If a>b then

4.1. Print a is

greater else

4.2. Print b is greater

5. Stop

2. Write an algorithm to find the largest among three different numbers entered by user.
Step 1: Start
Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c.
Step 4: if a>b and if a>c
Display a is the largest number.
Step 5: Else If b>c
Display b is the largest number.
Step 6: Else

Display c is the greatest number.

Step 7: Stop

3. Write an algorithm to check whether a number entered by user is prime or not.


Step 1: Start
Step 2: Declare variables n,i,flag.

Step 3: Initialize variables flag=1, i=2

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
17
Step 4: Read n from user.
Step 5: Repeat the steps till i<(n/2)
5.1 If n%i==0 equals then flag=0
Go to step 6
5.2 i=i+1
Step 6: If flag=0 Display n is not prime else
Display n is prime
Step 7: Stop

3. Repetition control flow(iteration)

• Repetition control flow means that one or more steps are performed repeatedly
until some condition is reached.
• This logic is used for producing loops in program logic when one more instructions may need
to beexecuted several times or depending on condition.
Basic Structure:

Repeat until CONDITION is true

Statements

Iteration

Example 1

Write an algorithm to find the factorial of a number entered by user.


Step 1: Start
Step 2: Initialize variables factorial=1,i=1
Step 4: Read value of n
Step 5: Repeat the steps till i=n
5.1: factorial=factorial*i
5.2: i=i+1
Step 6: Display factorial
Step 7: Stop

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
18
Example 2

Description: to print the values from 1 to n

1. Start

2. Read the value of ‘n’

3. Initialize i =1

4. Repeat step 4.1 until i< n

4.1. Print i

5. Stop

Function

• A function is a block of organized, reusable code that is used to perform a single, related action.
• Function is also named as methods, sub-routines.

Elements of functions:

1. Name for declaration of function

2. Body consisting local declaration and statements

3. Formal parameter

4. Optional result type.

Basic Syntax
function_name(parameters)

function statements

end function

Algorithmfor addition of two numbers using


function Main function()
Step 1: Start
Step 2:Call the function add()
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
19
Step 3: Stop
sub function add()
Step1:Functionstart
Step2:Get a,bValues
Step 3: add c=a+b
Step 4: Print c
Step 5: Return

II. NOTATIONS OF AN ALGORITHM

• Algorithm can be expressed in many different notations, including Natural Language,


Pseudo code, flowcharts and programming languages.
• Natural language tends to be verbose and ambiguous.
• Pseudocode and flowcharts are represented through structured human language.
• A notation is a system of characters, expressions, graphics or symbols designs used among
each other’s in problem solving to represent technical facts, created to facilitate the best result
for a program.

1. PSEUDOCODE

• The pseudocode is written in normal English language which cannot be understood by the
computer.
• It uses the basic structure of a normal programming language, but is intended for human
reading ratherthan machine reading.
• It is text-based detail design tool. Pseudo means false and code refers to instructions written
in programming language.
• Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules.

Basic rules to write pseudocode:


1. Only one statement per line. Statements represents single action is written on same line. For
example to read the input, all the inputs must be read using single statement.
2. Capitalized initial keywords The keywords should be written in capital
letters. Eg: READ, WRITE, IF, ELSE,
ENDIF, WHILE, REPEAT, UNTIL

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
20
Example:
Find the total and average of three subjects

RAED name, department, mark1, mark2, mark3

Total=mark1+mark2+mark3
Average=Total/3
WRITE name, department,mark1, mark2, mark3
3. Indent to show hierarchy
Indentation is a process of showing the boundaries of the structure.
4. End multi-line structures
Each structure must be ended properly, which provides more clarity.

To find sum of two numbers


READ
num1,num2
sum=num1+num
2 PRINT sum

Find greatest of two numbers


READ a, b
IF a>b then PRINT a is greater

ELSE
PRINT b is greater
ENDIF
5. Keep statements language independent.

Advantages of Pseudocode
• Can be done easily on a word processor
• Easily modified
• Implements structured concepts well
• It can be written easily
• It can be read and understood easily
• Converting pseudocode to programming language is easy as compared with flowchart
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
21
Disadvantages of Pseudocode
• It is not visual
• There is no standardized style or format
2. FLOWCHART
• A graphical representation of an algorithm. Flowcharts is a diagram made up of
boxes,diamonds, and other shapes, connected by arrows.

• Each shape represents a step in process and arrows show the order in which they occur.

Rules for drawing flowchart


1. In drawing a proper flowchart, all necessary requirements should be listed out in logical order.
2. The flow chart should be clear, neat and easy to follow. There should not be any room for
ambiguity inunderstanding the flowchart.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
22
3. The usual directions of the flow of a procedure or system is from left to right or top top to bottom.
Only oneflow line should come out from a process symbol.

4. Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible
answer,cap leave the decision symbol.

5. Only one flow line is used in conjunction with terminal symbol.

6. If flowchart becomes complex, it is better to use connector symbols to reduce the number of flow
lines.
7. Ensure that flowchart has logical start and stop

Advantages of
Flowchart
Communication:
• Flowcharts are better way of communicating the logic of the system.
Effective Analysis
• With the help of flowchart, a problem can be analyzed in more effective way.
Proper Documentation
• Flowcharts are used for good program documentation, which is needed for various purposes.
Efficient Coding
• The flowcharts act as a guide or blue print during the system analysis and program development
phase.
Systematic Testing and Debugging
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
23
• The flowchart helps in testing and debugging the program
Efficient Program Maintenance
• The maintenance of operating program becomes easy with the help of flowchart. It helps the
programmer to put efforts more efficiently on that part.

Disadvantages of Flowchart

Complex Logic:
• Sometimes, the program logic is quite complicated. In that case flowchart becomes complex
anddifficult to use.
Alteration and Modification:
• If alterations are required the flowchart may require redrawing completely.
Reproduction:
• As the flowchart symbols cannot be typed, reproduction becomes problematic.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
24
Conditional Structure
• Conditional structure is used to check the condition. It will be having two outputs only (True or
False)
➢ IF and IF…ELSE are the conditional structures used in Python language.
➢ CASE is the structure used to select multi way selection control. It is not
supported inPython.

IF… ELSE
IF…THEN…ELSE is the structure used to specify, if the condition is true, then execute
Process1, else, that is condition is false then execute Process2

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
25

Example:

Iteration or Looping Structure


• Looping is generally used with WHILE or DO...WHILE or FOR loop.
• WHILE and FOR is entry checked loop

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
26

In python DO…WHILE is not supported.


• If the loop condition is true then the loop gets into infinite loop, which may lead to system crash

III. PROGRAMMING LANGUAGE


• A programming language is a vocabulary and set of grammatical rules for instructing a computer
or computing device to perform specific tasks. In other word it is set of instructions for the
computer to solve the problem.
• Programming Language is a formal language with set of instruction, to the computer to solve a
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
27
problem. The program will accept the data to perform computation.
Program= Algorithm +DataNeed for Programming Languages

Types of Programming Language


In general Programming languages are classified into three types. They are
· Low – level or Machine Language
· Intermediate or Assembly Language
· High – level Programming language

Machine Language:
• Machine language is the lowest-level programming language (except for computers that utilize
programmable microcode). Machine languages are the only languages understood by computers.
It is also called as low level language.
Example code:100110011 111001100
Assembly Language:
• An assembly language contains the same instructions as a machine language, but the instructions
and variables have names instead of being just numbers. An assembler language consists of
mnemonics, mnemonics that corresponds unique machine instruction.
Example code:
startAdd x,y
Sub x,y
High – level Language:
• A high-level language (HLL) is a programming language such as C, FORTRAN, or Pascal that
enables a programmer to write programs that are more or less independent of a particular type of
computer.
• programs written in a high-level language must be translated into machine language by a
compiler or interpreter.
Interpreted Programming Language:
• Interpreter is a program that executes instructions written in a high-level language.
• An interpreter reads the source code one instruction or one line at a time, converts this line into
machinecode and executes it.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
28

Compiled Programming Languages


• Compile is to transform a program written in a high-level programming language from source
code intoobject code. This can be done by using a tool called compiler.
• A compiler reads the whole source code and translates it into a complete machine code
program toperform the required tasks which is output as a new file.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
29

IV.ALGORITHMIC PROBLEM SOLVING:


➢ Algorithmic problem solving is solving problem that require the formulation of an algorithm
for thesolution.

Understanding the Problem


▪ It is the process of finding the input of the problem that the algorithm solves.
▪ It is very important to specify the set of inputs ,the algorithm needs to handle
▪ A correct algorithm is not one that works most of the time, but one that works
Correctly for all inputs.
Ascertaining the Capabilities of the Computational Device
• If the instructions are executed one after another, it is called sequential algorithm
Choosing between Exact and Approximate Problem Solving
• The next principal decision is to choose between solving the problem exactly or
solving it approximately.
• Based on this, the algorithms are classified as exact algorithm and approximation algorithm.
• Data structure plays a vital role in designing and analysis the algorithms.
• Some of the algorithm design techniques also depend on the structuring data specifying a
problem’s instance.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
30
Algorithm Design Techniques
• An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving
problems algorithmically that is applicable to a variety of problems from different areas of computing.
Learningthese techniques is of utmost importance for the following reasons.
➢ First, they provide guidance for designing algorithms for new problems,
➢ Second, algorithms are the corner stone of computer science.
Methods of Specifying an Algorithm:
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. Programs
➢ Once an algorithm has been specified, you have to prove its correctness.
➢ A common technique for proving correctness is to use mathematical induction because an
algorithm’s iterations provide a natural sequence of steps needed for such proofs.
Analyzing an Algorithm
1. Efficiency.
Time efficiency: indicating how fast the algorithm runs,
Space efficiency: indicating how much extra memory it uses

2. simplicity.
▪ An algorithm should be precisely defined and investigated with mathematical expressions.
▪ Simpler algorithms are easier to understand and easier to program.
▪ Simple algorithms usually contain fewer bugs.
Coding an Algorithm
▪ Most algorithms are destined to be ultimately implemented as computer
programs. Programming an algorithm presents both a peril and an opportunity.
▪ A working program provides an additional opportunity in allowing an empirical analysis
of the underlying algorithm. Such an analysis is based on timing the program on several inputs
and then analyzing the results obtained.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
31
IV. SIMPLE STRATEGIES FOR DEVELOPING ALGORITHM
They are two commonly used strategies used in developing algorithm
1. Iteration
2. Recursion
Iteration
• The iteration is when the set of statement repeatedly executes till the controlling condition becomes
false
• The iteration is applied to the set of instructions which we want to get repeatedly executed.
• Iteration includes initialization, condition, and execution of statement within loop and
update (increments and decrements) the control variable.
➢ A sequence of statements is executed until a specified condition is true is called iterations.
1. for loop
2. While loop

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
32

Flowchart:

Recursions:
❖ A function that calls itself is known as recursion.
❖ Recursion is a process by which a function calls itself repeatedly until some specified condition
has been satisfied.
Algorithmfor factorial of n numbers using recursion:
Main function:
Step1: Start
Step2: Get n
Step3: call factorial(n)
Step4: print fact
Step5: Stop

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
33
Sub function factorial(n):
Step1: if(n==1) then fact=1 return fact
Step2: else fact=n*factorial(n-1) and return fact
Flowchart

Pseudo code for factorial using


recursion: Main function:
BEGIN
GET n
CALL
factorial(n)
PRINT factBIN

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
34

Sub function factorial(n):


IF(n==1) THEN fact=1
RETURN fact
ELSE
RETURN fact=n*factorial(n-1)

V. ILLUSTRATIVE PROBLEMS
1. Find minimum in a list
➢ A user has a list of numbers and wishes to find the minimum value in the list.
➢ Take the first element and compare its value against the values of all other elements.
Once wefind a smaller element we continue the comparisons with its value. Finally we
find the minimum.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
35

Algorithm:
Step 1: Start

Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5

Step4.1: Read a[i]


Step 4.2: i=i+1 goto step 4
Step 5: Compute min=a[0]
Step 6: Initialize i=1
Step 7: If i<n, then go to step 8 else goto step 10
Step 8: If a[i]<min, then goto step 8.1,8.2 else goto 8.2

Step 8.1: min=a[i]


Step 8.2: i=i+1 goto7

Step 10: Stop

Pseudocode:

BEGIN READ n
FOR i=0 to n, then READ a[i]
INCREMENT i
END FOR
COMPUTE min=a[0]
FOR i=1 to n, then

IF a[i]<min, then
CALCULATE min=a[i]
INCREMENT i
ELSE INCREMENT i
END IF-ELSE
END FOR
PRINT min
END
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
36

Flowchart:

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
37

2. Insert a card in a list of sorted cards


➢ Insert a Card in a List of Sorted Cards – is same as inserting an element into a sorted array.
➢ We start from the high end of the array and check to see if that's where we want to insert the
data. If so, fine. If not, we move the preceding element up one and then check to see if we
want to insert x in the “hole” left behind. We repeat this step as necessary.
➢ Thus the search for the place to insert x and the shifting of the higher elements of the array are
accomplished together.

Algorithm:
Step 1: Start

Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then goto step 4.1, 4.2 else goto step 5

Step4.1: Read a[i]


Step 4.2: i=i+1 goto step 4
Step 5: Read item
Step 6: Calculate i=n-1
Step 7: If i>=0 and item<a[i], then go to step 7.1, 7.2 else goto step 8

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
38
Step 7.1: a[i+1]=a[i]
Step 7.2: i=i-1 goto step 7
Step 8: Compute a[i+1]=item
Step 9: Compute n=n+1
Step 10: If i<n, then goto step 10.1, 10.2 else goto step 11
Step10.1: Print a[i] Step10.2: i=i+1 goto step 10
Step 11: Stop

Pseudocode:
BEGIN READ n
FOR i=0 to n,
thenREAD a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], then

CALCULATE a[i+1]=a[i]

DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]

COMPUTE n=n+1
FOR i=0 to n, then
PRINT a[i]
INCREMENT i
END FOR
END

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
39

Flowchart:

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
40

3. Guess an integer in a range


The computer is going to randomly select an integer from 1 to N and ask you to guess it. The
computer will tell you if each guess is too high or too low. We have to guess the number by making
guesses until you find the number that the computer chose. Once found the number, think about what
technique used, when deciding what number to guess next?
Algorithm:
Step1: Start
Step 2: Compute guess=input

Step 3: Read guess


Step 4: If guess>n, then Print your guess is too high Else
Step6:If guess<n, then Print your guess is too low Else
Step 7:If guess==n,then Print Good job Else nope
Step 6: Stop
Pseudocod
BEGIN
COMPUTE guess=input
READ guess,
IF guess>n
PRINT Guess is high

ELSE IF guess<n

PRINT Guess is low

ELSE IF guess=n

PRINT Good job

ELSE
PRINT nope
END

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
41

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
42

4. TOWER OF HANOI
➢ Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one
rings.
➢ Tower of Hanoi is one of the best example for recursive problem solving.
Pre-condition:
➢ These rings are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one.
➢ There are other variations of the puzzle where the number of disks increase, but the tower count
remainsthe same.

Post-condition:
➢ All the disk should be moved to the last pole and placed only in ascending order as shown below.

Rules
➢ The mission is to move all the disks to some another tower without violating the sequence of
arrangement.
A few rules to be followed for Tower of Hanoi are
• Only one disk can be moved among the towers at any given time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
➢ Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation
shows thata puzzle with 3 disks has taken 23 - 1 = 7 steps.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
43
Algorithm
➢ To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem
with lesser amount of disks, say → 1 or 2.

➢ We mark three towers with name, source, aux (only to help moving the disks) and
destination.Input: one disk
➢ If we have only one disk, then it can easily be moved from source to destination peg.
Input: two disks
If we have 2 disks −
• First, we move the smaller (top) disk to aux peg.
• Then, we move the larger (bottom) disk to destination peg.
• And finally, we move the smaller disk from aux to destination peg.
Input: more than two disks
➢ divide the stack of disks in two parts.
➢ The largest disk (nth disk) is in one part and all other (n-1) disks are in the second part.

Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks onto it.
We can imagine to apply the same in a recursive way for all given set of disks.
· The steps to follow are –
Step 1 − Move n-1 disks from source to
auxStep 2 − Move nth disk from source
to dest Step 3 − Move n-1 disks from
aux to dest

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
44
A recursive algorithm for Tower of Hanoi can be driven as follows –
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN move disk from source to dest ELSE
Hanoi(disk - 1, source, aux, dest)

Hanoi(disk - 1, aux, dest, source)

END IF
END
STOP Procedure

******************************************************************************

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
45

Draw a flow chart to find greatest among three numbers.(AU 2018)

Draw a flow chart to find sum of n numbers(AU 2018)

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
46

Draw a flow chart to find whether the given number is leap year or not.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
47

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
48

2 MARKS
1. What is an algorithm?
An algorithm is a finite number of clearly described, unambiguous do able steps that can be
systematically followed to produce a desired results for given input in the given amount of time. In other
word, an algorithm is a step by step procedure to solve a problem with finite number of steps.
2. What is Pseudo code?
Pseudocode is an informal high-level description of the operating principle of a computer
program or
algorithm. Pseudo means false and code refers to instructions written in programming language.
3. What is Problem Solving?
Problem solving is the systematic approach to define the problem and creating number of
solutions. Theproblem solving process starts with the problem specifications and ends with a correct
program.
4. Distinguish between algorithm and program.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
49

5. Define Flow chart.


A graphical representation of an algorithm. Flow charts is a diagram made up
ofboxes, diamonds, and other shapes, connected by arrows.
6. Write an algorithm to accept two numbers, compute the sum and print the
result.Step 1: Start
Step 2: Declare variables num1,num2 and
sum,Step 3: Read values num 1 and num2.
Step 4: Add and assign the result to
sum.Sum←num1+num2
Step 5: Display sum
7. Differentiate between iteration and recursion.

8. What is Programming language? With example.


Programming Language is a formal language with set of instruction, to the computer to solve a
problem.
Java, C, C++, Python, PHP.
9. What are the steps for developing algorithms.
• Problem definition
• Development of a model
• Specification of Algorithm
• Designing an Algorithm
• Checking the correctness of Algorithm
GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC
50
• Analysis of Algorithm
• Implementation of Algorithm
• Program testing
• Documentation Preparation
10. What are the Guidelines for writing pseudo code?
• Write one statement per line
• Capitalize initial keyword
• Indent to hierarchy
• End multiline structure
• Keep statements language independent.

GE3151 PROBLEM SOLVING AND PYTHON PROGRAMMING I YEAR/I SEM MRS.A.THAVA VINU/AP,AI &DS ,DMIEC

You might also like