SlideShare a Scribd company logo
Knowledge Institute of Technology, Salem-637504
(Affiliated to Anna University, Chennai)
(Accredited by NAAC)
Department of Computer Science and Engineering
(Accredited by NBA)
Course
Code
GE8151 Course Name
PROBLEM SOLVING AND
PYTHON PROGRAMMING
Year I SEM I CLASS CSE
Name of
the Faculty
Mrs.R.Sathya Priya, Assistant Professor Dept. CSE
PSPP U-I
UNIT-I ALGORITHMIC PROBLEM
SOLVING 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
SYLLABUS
2
3
 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.
Find minimum in a list
4
List
5
 List is a collection of elements stored under a
common name.
 List is commonly represented by using [ ].
 Example:
 b[ ] = {10, 20, 30, 40, 50}
b [0] b [1] b [2] b[3] b[4]
10 20 30 40 50
Finding Minimum in a list
6
 Given a list of numbers from which you are
supposed to find the minimum value.
 An algorithm is required for entering the numbers
in the list and then calculate the minimum value.
 How it works?
 Take the first number in the list and call it
minimum.
 Compare the minimum’s value with all other
values in the list one by one.
 The moment you find a smaller element than the
7
Example
Initial List 8 6 9 10 5 7 3 21
Let Min = 8, Compare MIN with every element in the list one by one.
8 6 9 10 5 7 3 21
Step 1: 6<Min, So set Min=6
8 6 9 10 5 7 3 21
Step 2: 9 > Min , So no change
8 6 9 10 5 7 3 21
Step 3: 10 > Min , So no change
8 6 9 10 5 7 3 21
Step 4: 5 < Min, So Set Min =5
8 6 9 10 5 7 3 21
Step 5: 7 > Min, So No Change.
8 6 9 10 5 7 3 21
Step 6: 3 < Min, So Set Min=3
8 6 9 10 5 7 3 21
Step 7: 21 > Min , So No change
So the minimum value in the given list is 3.
8
Algorithm:
Step 1: Start the program.
Step 2: Read the number of elements in list n.
Step 3: Initialize i=0 as the starting index
Step Repeat steps 4.1 and 4.2 until the while
condition is true (i<n ) , otherwise continue with
step5 .
Step 4.1: Read the element a[i]
Step 4.2: i=i+1
Step 5: initialize k=1, Min=a[0]
Step 6: Repeat step 6.1 and 6.2 until the while
condition is true (k<n), otherwise continue with
step7.
Step 6.1: If a[k]<Min then set Min=a[k]
Step 6.2: k=k+1
9
Pseudo code:
READ N
INITIALIZE i=0,
WHILE i<n
READ a[i]
SET i=i+1
ENDWHILE
INITIALIZE Min=a[0], k=1
WHILE k<n
IF a[k] < Min THEN
Min=a[k]
k=k+1
ENDWHILE
PRINT Min
10
Flow chart
Inserting a Card into a list of sorted
Cards
11
Inserting a Card into a list of sorted
Cards
12
 Inserting a card in a list of sorted cards is same as
inserting an element into a sorted list of numbers.
 For this, start from the end of the list and compare
the new element with the elements of the list to
find a suitable position at which the new element
has to be inserted.
 While comparing, also shift the elements one step
ahead to create a vacancy for the new element.
13
Example : Insert the element 23 into the given sorted list
Position 0 1 2 3 4 5 6
Original list 10 15 20 25 30 35
Step 1: 23>35 ? No so move 35 to position 6
10 15 20 25 30 35
Step 2: 23>30 ? No so move 30 to position 5
10 15 20 25 30 35
Step 3: 23>25 ? No so move 25 to position 4
10 15 20 25 30 35
Step 4: 23>20 ? Yes insert 23 at position 3
10 15 20 23 25 30 35
14
Algorithm:
Step 1: Start the program.
Step 2: Read the number of elements in list n.
Step 3: Initialize i=0
Step 4: Repeat steps 4.1 and 4.2 until the while condition is true
(i<n ) , otherwise continue with step5 .
Step 4.1: Read the element a[i] –in sorted order
Step 4.2: i=i+1
Step 5: Read the element to be inserted X
Step 6: Set j=n-1
Step 7: Repeat steps 7.1 and 7.2 until the while condition is true
( (j>=0) && (X<a[j])) otherwise continue with step8 .
Step 7.1: a[j+1]=a[j]
Step 7.2: Set j=j-1
Step 8: a[j+1] = X
Step 9: Stop the Program.
15
Pseudo code:
READ n
SET i=0
WHILE i<n
READ a[i]
i=i+1
ENDWHILE
READ X
SET j=n-1
WHILE j>=0 && a[j]>X
a[j+1]=a[j]
j=j-1
ENDWHILE
a[j+1]=X
16
Flow chart
Guess an integer number in a range.
17
Guess an integer number in a
range
18
 It is a game.
 The user selects a range.
 Let’s say User selected a range, i.e., from A to B,
where A and B belong to Integer.
 Some random integer will be selected by the system and
the user has to guess that integer in the minimum number
of guesses.
 There is no limit on number of guesses.
Example:
 If the User inputs range, let’s say from 1 to 100. And System
randomly selected 42 as the integer.
 Now the guessing game started, if the user entered 50 as
his/her first guess. The compiler shows “Try Again! You
guessed too high”.
 Now the second guess started.
19
Algorithm:
Step 1: Start the program.
Step 2: Read the range of integers Start, End.
Step 3: Set num = random number from Start to End.
Step 4: Set =1
Step 5: Read the Guess number as G
Step 6: If G=num then print ‘you win in the ith guess’
Step 7: Else If G>num the print ‘your guess is too high’ go to step
5.
Step 8: Else print ‘your guess is too low’ go to step 5.
Step 9: Stop the program.
20
Pseudo code:
READ Start, End
SET num=Random(Start, End)
SET i =1
LOOP:
READ G
IF G=num THEN
PRINT ‘You win in ith turn’
EXIT
ELSEIF G>num THEN
PRINT ‘Too High’
ELSE
PRINT ‘Too Low’
i=i+1
ENDLOOP
21
Flowchar
t
Tower of Hanoi
22
Tower of Hanoi
23
 Tower of Hanoi is a mathematical puzzle where we
have three poles (namely A, B, C) and n disks.
 The objective of the puzzle is to move the disks from
pole A to Pole C using the Pole B.
 simple rules:
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from
one of the poles and placing it on top of another pole
i.e. a disk can only be moved if it is the uppermost
disk on a pole.
3) No disk may be placed on top of a smaller disk.
Ex: with two disks
24
Ex: with Three disks
25
26
Algorithm:
Step 1: Start
Step 2: Read the number of disc n
Step 3: Call the function TOH(n, A, B, C)
Step 4: Stop
 Algorithm for Function Definition TOH
Step 1: if n==1 then Move disc from A to C and
go to step 5 else continue with step 2
Step 2: Call the function TOH(n-1,A,C,B)
Step 3: Move disc n from A to C
Step 4: Call the function TOH(n-1,B,A,C)
Step 5: Return
27
Pseudo code:
READ n
CALL TOH(n,A,B,C)
Function Definition TOH(n, A, B, C)
IF n==1
Move disc from A to C
return
ELSE
CALL TOH(n-1,A,C,B)
Move disc n from A to C
CALL TOH(n-1,B,A,C)
Return
28
Flowcha
rt

More Related Content

Similar to UNIT I_PSPP - Illustrative Problems (1).pptx

25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual
kamesh dagia
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
yazad dumasia
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
SushantRaj25
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
KamalAlbashiri
 
Decision Science.pdf
Decision Science.pdfDecision Science.pdf
Decision Science.pdf
pandeyaman577
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Rakotoarison Louis Frederick
 
Sorting2
Sorting2Sorting2
Sorting2
Saurabh Mishra
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
Ehsan Ehrari
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docx
AleezaAnjum
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
DHANIK VIKRANT
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Array data structure
Array data structureArray data structure
Array data structure
harsh112327
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 

Similar to UNIT I_PSPP - Illustrative Problems (1).pptx (20)

25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual25422733 c-programming-and-data-structures-lab-manual
25422733 c-programming-and-data-structures-lab-manual
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Decision Science.pdf
Decision Science.pdfDecision Science.pdf
Decision Science.pdf
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Sorting2
Sorting2Sorting2
Sorting2
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docx
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Array data structure
Array data structureArray data structure
Array data structure
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Ada notes
Ada notesAda notes
Ada notes
 

More from RSathyaPriyaCSEKIOT

c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
RSathyaPriyaCSEKIOT
 
unit-2 mc.pdf
unit-2 mc.pdfunit-2 mc.pdf
unit-2 mc.pdf
RSathyaPriyaCSEKIOT
 
unit-5
unit-5unit-5
unit-1 notes
unit-1 notesunit-1 notes
unit-1 notes
RSathyaPriyaCSEKIOT
 
unit-4
unit-4 unit-4
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
RSathyaPriyaCSEKIOT
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
RSathyaPriyaCSEKIOT
 
C program full materials.pdf
C program  full materials.pdfC program  full materials.pdf
C program full materials.pdf
RSathyaPriyaCSEKIOT
 

More from RSathyaPriyaCSEKIOT (9)

c programming session 1.pptx
c programming session 1.pptxc programming session 1.pptx
c programming session 1.pptx
 
UNIT-4
UNIT-4UNIT-4
UNIT-4
 
unit-2 mc.pdf
unit-2 mc.pdfunit-2 mc.pdf
unit-2 mc.pdf
 
unit-5
unit-5unit-5
unit-5
 
unit-1 notes
unit-1 notesunit-1 notes
unit-1 notes
 
unit-4
unit-4 unit-4
unit-4
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
pattern-printing-in-c.pdf
pattern-printing-in-c.pdfpattern-printing-in-c.pdf
pattern-printing-in-c.pdf
 
C program full materials.pdf
C program  full materials.pdfC program  full materials.pdf
C program full materials.pdf
 

Recently uploaded

Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 

Recently uploaded (20)

Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 

UNIT I_PSPP - Illustrative Problems (1).pptx

  • 1. Knowledge Institute of Technology, Salem-637504 (Affiliated to Anna University, Chennai) (Accredited by NAAC) Department of Computer Science and Engineering (Accredited by NBA) Course Code GE8151 Course Name PROBLEM SOLVING AND PYTHON PROGRAMMING Year I SEM I CLASS CSE Name of the Faculty Mrs.R.Sathya Priya, Assistant Professor Dept. CSE PSPP U-I
  • 2. UNIT-I ALGORITHMIC PROBLEM SOLVING 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 SYLLABUS 2
  • 3. 3  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.
  • 4. Find minimum in a list 4
  • 5. List 5  List is a collection of elements stored under a common name.  List is commonly represented by using [ ].  Example:  b[ ] = {10, 20, 30, 40, 50} b [0] b [1] b [2] b[3] b[4] 10 20 30 40 50
  • 6. Finding Minimum in a list 6  Given a list of numbers from which you are supposed to find the minimum value.  An algorithm is required for entering the numbers in the list and then calculate the minimum value.  How it works?  Take the first number in the list and call it minimum.  Compare the minimum’s value with all other values in the list one by one.  The moment you find a smaller element than the
  • 7. 7 Example Initial List 8 6 9 10 5 7 3 21 Let Min = 8, Compare MIN with every element in the list one by one. 8 6 9 10 5 7 3 21 Step 1: 6<Min, So set Min=6 8 6 9 10 5 7 3 21 Step 2: 9 > Min , So no change 8 6 9 10 5 7 3 21 Step 3: 10 > Min , So no change 8 6 9 10 5 7 3 21 Step 4: 5 < Min, So Set Min =5 8 6 9 10 5 7 3 21 Step 5: 7 > Min, So No Change. 8 6 9 10 5 7 3 21 Step 6: 3 < Min, So Set Min=3 8 6 9 10 5 7 3 21 Step 7: 21 > Min , So No change So the minimum value in the given list is 3.
  • 8. 8 Algorithm: Step 1: Start the program. Step 2: Read the number of elements in list n. Step 3: Initialize i=0 as the starting index Step Repeat steps 4.1 and 4.2 until the while condition is true (i<n ) , otherwise continue with step5 . Step 4.1: Read the element a[i] Step 4.2: i=i+1 Step 5: initialize k=1, Min=a[0] Step 6: Repeat step 6.1 and 6.2 until the while condition is true (k<n), otherwise continue with step7. Step 6.1: If a[k]<Min then set Min=a[k] Step 6.2: k=k+1
  • 9. 9 Pseudo code: READ N INITIALIZE i=0, WHILE i<n READ a[i] SET i=i+1 ENDWHILE INITIALIZE Min=a[0], k=1 WHILE k<n IF a[k] < Min THEN Min=a[k] k=k+1 ENDWHILE PRINT Min
  • 11. Inserting a Card into a list of sorted Cards 11
  • 12. Inserting a Card into a list of sorted Cards 12  Inserting a card in a list of sorted cards is same as inserting an element into a sorted list of numbers.  For this, start from the end of the list and compare the new element with the elements of the list to find a suitable position at which the new element has to be inserted.  While comparing, also shift the elements one step ahead to create a vacancy for the new element.
  • 13. 13 Example : Insert the element 23 into the given sorted list Position 0 1 2 3 4 5 6 Original list 10 15 20 25 30 35 Step 1: 23>35 ? No so move 35 to position 6 10 15 20 25 30 35 Step 2: 23>30 ? No so move 30 to position 5 10 15 20 25 30 35 Step 3: 23>25 ? No so move 25 to position 4 10 15 20 25 30 35 Step 4: 23>20 ? Yes insert 23 at position 3 10 15 20 23 25 30 35
  • 14. 14 Algorithm: Step 1: Start the program. Step 2: Read the number of elements in list n. Step 3: Initialize i=0 Step 4: Repeat steps 4.1 and 4.2 until the while condition is true (i<n ) , otherwise continue with step5 . Step 4.1: Read the element a[i] –in sorted order Step 4.2: i=i+1 Step 5: Read the element to be inserted X Step 6: Set j=n-1 Step 7: Repeat steps 7.1 and 7.2 until the while condition is true ( (j>=0) && (X<a[j])) otherwise continue with step8 . Step 7.1: a[j+1]=a[j] Step 7.2: Set j=j-1 Step 8: a[j+1] = X Step 9: Stop the Program.
  • 15. 15 Pseudo code: READ n SET i=0 WHILE i<n READ a[i] i=i+1 ENDWHILE READ X SET j=n-1 WHILE j>=0 && a[j]>X a[j+1]=a[j] j=j-1 ENDWHILE a[j+1]=X
  • 17. Guess an integer number in a range. 17
  • 18. Guess an integer number in a range 18  It is a game.  The user selects a range.  Let’s say User selected a range, i.e., from A to B, where A and B belong to Integer.  Some random integer will be selected by the system and the user has to guess that integer in the minimum number of guesses.  There is no limit on number of guesses. Example:  If the User inputs range, let’s say from 1 to 100. And System randomly selected 42 as the integer.  Now the guessing game started, if the user entered 50 as his/her first guess. The compiler shows “Try Again! You guessed too high”.  Now the second guess started.
  • 19. 19 Algorithm: Step 1: Start the program. Step 2: Read the range of integers Start, End. Step 3: Set num = random number from Start to End. Step 4: Set =1 Step 5: Read the Guess number as G Step 6: If G=num then print ‘you win in the ith guess’ Step 7: Else If G>num the print ‘your guess is too high’ go to step 5. Step 8: Else print ‘your guess is too low’ go to step 5. Step 9: Stop the program.
  • 20. 20 Pseudo code: READ Start, End SET num=Random(Start, End) SET i =1 LOOP: READ G IF G=num THEN PRINT ‘You win in ith turn’ EXIT ELSEIF G>num THEN PRINT ‘Too High’ ELSE PRINT ‘Too Low’ i=i+1 ENDLOOP
  • 23. Tower of Hanoi 23  Tower of Hanoi is a mathematical puzzle where we have three poles (namely A, B, C) and n disks.  The objective of the puzzle is to move the disks from pole A to Pole C using the Pole B.  simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the poles and placing it on top of another pole i.e. a disk can only be moved if it is the uppermost disk on a pole. 3) No disk may be placed on top of a smaller disk.
  • 24. Ex: with two disks 24
  • 25. Ex: with Three disks 25
  • 26. 26 Algorithm: Step 1: Start Step 2: Read the number of disc n Step 3: Call the function TOH(n, A, B, C) Step 4: Stop  Algorithm for Function Definition TOH Step 1: if n==1 then Move disc from A to C and go to step 5 else continue with step 2 Step 2: Call the function TOH(n-1,A,C,B) Step 3: Move disc n from A to C Step 4: Call the function TOH(n-1,B,A,C) Step 5: Return
  • 27. 27 Pseudo code: READ n CALL TOH(n,A,B,C) Function Definition TOH(n, A, B, C) IF n==1 Move disc from A to C return ELSE CALL TOH(n-1,A,C,B) Move disc n from A to C CALL TOH(n-1,B,A,C) Return