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

ITS D002 - Problem Solving Lesson 01 (Ver 3.0)

This document provides an introduction to problem-solving within the context of computer science and programming, outlining key concepts such as problem representation, classification, and strategies. It emphasizes the importance of understanding problems, planning solutions, and utilizing algorithms and heuristics. Additionally, it discusses the programming process, including analysis, design, coding, testing, and documentation, highlighting the cyclical nature of program development.

Uploaded by

gary.ngpl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ITS D002 - Problem Solving Lesson 01 (Ver 3.0)

This document provides an introduction to problem-solving within the context of computer science and programming, outlining key concepts such as problem representation, classification, and strategies. It emphasizes the importance of understanding problems, planning solutions, and utilizing algorithms and heuristics. Additionally, it discusses the programming process, including analysis, design, coding, testing, and documentation, highlighting the cyclical nature of program development.

Uploaded by

gary.ngpl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

PREPARED by KOH Chung Haur @ 2022

ITSD002
Problem Solving
Topic 1
Introduction to Problem Solving

DIPLOMA IN INFORMATION TECHNOLOGY


Version 2022.1

© 2022 Singapore Institute of Management Group Limited


Learning Outcomes
After studying this chapter and the recommended reading, you
should be able to:
• Relate the ideas of computer science, programming and problem-
solving
• Understand abstraction and the role it plays in the problem-solving
process
• Understand and implement the notion of abstract data type
Lesson Outline
• Problem Representation
• Problem Classification
• Problem Solving Strategies
• Problem Solving Steps
• Problem Solving Principles
• Program Planning
• Programming Languages
What is a problem?
• Are these problems?
• You are at the main entrance of SIM and you need to be at room 03-21
by 8.30am
• You are at SIM. You need to reach Orchard ION within 45 minutes
• You have $3 left and you are wondering what you can have for lunch

• A problem exists when an ‘obstacle’ separates the present state from


the goal state
What is a problem?
What is the obstacle?
Problem Representation
• All problems have
• States
• Start/current state
• Goal state
• A set of operators to ‘move’ from one state to another state
• One or more constraints on the types of ‘moves’ allowed
Problem Representation
Problem Representation
• Start state
• Entrance A
• Goal state
• A location in the maze where the cheese is
• Operators
• Move left, right, up or down
• Constraints
• Can only move if the path is clear
What is Problem Solving?
“...... the art of finding ways to get from where you are now to
where you want to be (assuming you do not already know
how).”
[Nolan 1989]

• It involves pursuing a goal


• It is a process which involves a sequence of activities or operations
• It requires mental activity
How to Acquire Problem-Solving Skills?
• Practice makes perfect!
• Practice will enable you to:
• identify similarities between problems
• master a variety of problem-solving techniques
• build up your confidence in solving problems

• Being aware of the way you think when solving a problem is a


necessary step to allow you to regulate your thinking process and
improve your problem-solving skills
How can Problems be Classified?
• Example: Two Rats in a Maze
• Two rats are let loose in a maze. Somewhere inside the maze is a large
piece of cheese
How can Problems be Classified?
• Example: Two Rats in a Maze
• Which rat will reach the cheese FIRST?
How can Problems be Classified?
• Example: Two Rats in a Maze
• Rat B uses heuristics to get quicker to the cheese
• The heuristic (or “rule of thumb”) this mouse used consisted in
assuming that the path with the stronger smell of cheese should be the
shorter path to get to the cheese
• [It is possible to build a maze that will fool this rat]
How are Problems Solved?
• We use algorithms and heuristics to solve problems
• An algorithm consists of a sequence of steps which if performed
correctly will result in the task, or process, to be carried out
successfully
• Heuristics are short-cuts or educated guesses used to reduce the
amount of time or effort necessary to execute a process or an
algorithm
How are Problems Solved?
• Problems with algorithmic solutions
• Solution consists of a series of actions
• Example
• Baking a cake; balancing a checkbook
• Problems with heuristic solutions
• Solution requires reasoning built on knowledge and experience, and a
process of trial and error
• Example
• What is the fastest way to Orchard; what stock to buy; what business to
invest
• Solution cannot be reached through a direct set of steps
How are Problems Solved?
• Algorithms (that do not use heuristics) consists of rules which
always lead to a correct solution, if one exists
• Heuristics are fundamental to solve problems for which an
algorithmic solution either has not been found, or has a prohibitive
execution time
• Most heuristics, however, do not guarantee a correct or the best
solution possible
Types of Problems
• Problems can be classified as
• Well-defined vs. Ill-defined
• Knowledge-rich vs. Knowledge-lean

• Well-defined problem
• All information necessary to solve the problem is either explicitly
given or can be inferred
• Example:
• What is the result of the following multiplication?
234 x 124
Types of Problems
• Ill-defined problem
• There is uncertainty in either the given information, the permissible
operations/actions, or the final state
• Example:
• Given 16 matches arranged in 5 squares as shown, move 3 matches to
form 4 squares
Types of Problems
• Knowledge-rich problem
• It requires prior specialized or domain-specific knowledge to solve the
problem
• Example:
• What is the best way to stack the containers in a container ship?
• Knowledge-lean problem
• It can be solved by using domain-general knowledge
• Strategies and methods that apply to many types of problems
Types of Problems
• Knowledge-lean problem
• Example
• Starting with the arrangement of dots given below:

• Rearrange them so that they end up in the following arrangement:

• A dot can move to an empty space adjacent to it


• A dot can jump over only ONE dot of either color
• White dots can only move to the left and colored dots to the right
Problem Solving Strategies
• Some basic strategies which are widely employed
• Brute Force
• Trial & Error
• Analogy
• Divide & Conquer
• Heuristics
Problem Solving Strategies
• Brute Force
• The strategy used by Rat A (in previous example)
• Simply work through every possible combination of actions until it
works
• If a solution exists, this method will find it, but it may take a long time
• Example
• Place 1 to 9 number into the boxes, such that the sum of the entries in any
row, or any column, or any main diagonal, is the same
• Maximum number of trials
= 9! = 3,628,800
Problem Solving Strategies
• Trial & Error
• A variation of Brute Force (where you make guesses)
• Trying one candidate solution after another until you are successful (if
ever!)
• When this strategy is used in logical reasoning, it eliminates
impossible solutions
• You may have though of using trial and error to solve the matches
problem
Problem Solving Strategies
• Analogy
• Problem 1: A tourist wants to convert $100 American dollars to
Australian dollars. If the rate is $1.3 AUD to the USD, how much
Australian dollar will he get?
• Problem 2: A car travelling at the average speed of 80 km/h reaches its
destination after 5 hours. How are has it travelled?
• If you know how to solve the first problem, you can solve the second
problem by analogy
Problem Solving Strategies
• Divide & Conquer
• Step 1: Divide the original problem into several smaller ones
• Step 2: Solve (conquer) – find the solutions for each sub-problem
• Step 3: Combine the solutions of the sub-problems into a solution for
the original solution
• Example
• Suppose that you are trying to find what is the thickest book that I have
on my four bookcases in my office
• You may simplify the problem by finding the thickest book each of the
bookcases and then compare these four books to find the thickest
Polya’s Problem Solving Method
1. Understand the problem, make sure you understand what the
problem is asking
2. Plan a strategy for solving the problem
3. Execute your strategy, and revise it if necessary
4. Check and interpret your result
Step 1: Understand the Problem
• Take note of any data, or information, that is given (e.g. numbers,
quantities, names, etc)
• From the data given
• Identify the start state
• Identify any constraints (conditions)
• Identify the goal state (the unknowns)
• Draw a picture if it helps you to organize the information (data,
constrains and unknowns)
• You must know exactly what problem you are trying to solve
Step 2: Plan A Strategy
• This step is the most difficult, you will need to be creative and
organized.
• Use your experience in solving similar problems
• Identify general strategies and techniques that can be used to help
solving the problem
• Use flowcharts, pseudocode, diagrams to map your strategy
Step 3: Execute Your Strategy and Revise
• Keep an organized written record of your work
• This will help you if you need to make revisions
• Double-check each step to make sure you don’t propagate errors to
the end of the solution
• Assess your strategy as by working through your solution
• If you find a flaw return to step 2 and revise your strategy
• Revision of strategy is quite unavoidable
Step 4: Check and Interpret Result
• Make sure the result makes sense
• For example, are the values or units sensible?
• Check the consistency of the result by considering special or
limiting cases
• For example, does it work for 0 or 1? Of for the last day of the month
or year?
• This final step may be the most important from the point of view of
“outsiders”
Fun Riddle
Three prisoners know that the jailer has 3 white hats and 2 red hats

The jailer puts a hat on the head of each prisoner and says,
“if you can deduce the color of your own hat, you will be freed”.
Each prisoner can see the hats of the other two prisoners but not his
own.
The first prisoner says, “I cannot tell the color of my hat”.
The second prisoner says, “I cannot tell the color of my hat”.
The third prisoner, who is blind, is able to determine the color of his hat
and freed.
Problem Solving Principles
• Identify the problem
• Completely understand the problem
• Identify alternative ways to solve the problem; select the best
alternative
• Devise a plan to solve the problem; list solution steps
• Carry out the plan
• Evaluate solution
• Review the results
Problem Solving Process
• Combination of algorithmic and/or heuristic solutions

• Step-by-step procedure devised to process given data and produce


requested output

• Common characteristics that all programs have


Difficulties with Problem Solving
• Lack of problem-solving experience
• Inadequate solution steps
• Incorrect problem definition
• Alternatives chosen incorrectly
• Invalid logic
• Incorrect solution evaluation
What is Programming?
• A program is a list of instructions that is executed by a computer to
accomplish a task
• Tasks are broken down into a sequence of instructions
• Creating those instructions is programming
• The way we communicate with the computer
• Programmer
• A person who solves problems by writing programs on a computer
• User
• Any person who runs a program
Program Planning / Development Cycle
1. Analyze the problem
2. Design a program to solve the problem
3. Code the program
4. Test the program
5. Complete the documentation
6. Revise as necessary
Analyse the Problem
• Define the problem
• Identify desired results (output)
• Determine input needed to produce those results
• Example: Create a program to generate 6 numbers to play the lottery
• Is 7, 7, 7, 7, 7, 7 ok?
• Is -3, 0, 8, 9, 689, 689 ok?
• Is 1, 2, 6, 47.98765, 88, 93.45 ok?
• These are all 6 numbers, but we see we must be more specific
• Desired results: 6 different positive integers within the range of 1 to
49
Design the Program
• Plan the solution to the problem
• Create a detailed description of program
• Use charts (flowcharts), models, or ordinary language (pseudocode)
• Identify algorithms needed
• Algorithm: a step-by-step method to solve a problem or complete a
task
• Algorithms must be:
• Well defined
• Must produce some result
• Well ordered
• Must terminate in a finite time
Code the Program
• Translate algorithms, charts, models, pseudocode, or ordinary
language into program code
• Coding
• Done in a specific programming language
• E.g. Python statements
• This phase should only begin after a solid design exists
• Documenting
• Add statements to document which describes to the reader what the
code does
• Internal documentation: for the programmers to read
• External documentation: for the user
Syntax
• Each programming language uses its specific syntax
• Correct syntax for telling your friend where you put a cheese
sandwich is:
“I have put it on the table.”
• Incorrect use of English syntax to say:
“I have it on the table put.”
• All the right words are there, but without proper syntax, the sentence
is gibberish in English
• But translated word for word, the second sentence is correct syntax
in German
Test the Program
• Locate and remove any errors in the program
• Create test data that will be used to check the program’s correctness
• In analysis phase:
• Continually ask questions
• Did I interpret data correctly?
• Does program fulfill requirements?
• Are my formulas or procedures correct? Etc...
• In design phase:
• Use desk-checking to walk through the program by hand with a set of
data that you know the answer to
Test the Program
• In coding phase:
• Check that the program will catch errors by using test data designed to
create errors
• Software will alert you to errors in syntax but not in the logic of the
program
• Finally, test your program with as many sets of test data as
possible
• Use good data, bad data, data you know the answers for, etc
• The more testing of various types of data you can use, the more likely
you are to have a program that is free of errors
Types of Errors
• Syntax Errors
• A violation of the programming language’s rules for creating valid
statements
• May be caused by incorrect grammar or punctuation, or misspelling a
keyword
• The program will not run at all with syntax errors
Types of Errors
• Logic Errors / Runtime Errors
• The program runs, but does not produce the expected results
• Maybe caused by using an incorrect formula, or incorrect sequence of
statements, etc
• Can be detected during the desk checking phase of the programming
cycle
• If logic errors exist, the program must be debugged
Complete the Documentation
• Organize all the material that describes the program
• Create an outline of the program so that it is apparent what major
tasks and subtasks must be accomplished and the relationships
among these tasks
• Describe in detail how each of these tasks is to be carried out
• Create a user’s guide
• To help users can understand the intricacies of the program
Program Development is a Process
• Program development is a cyclical process that often requires
returning to earlier steps and, with complex programs, may take
many months
• The design process may uncover flaws in the analysis
• Coding may find problems leading to modifications or additions to
the design
• Testing inevitably uncovers problems that require returning to
previous phases
Programming and Scripting Languages
• Programs are written using programming languages

• Machine Languages
• A set of primitive instructions built into every computer
• The instructions consist only of sequences of 0s and 1s
• Program with native machine language is a tedious process, moreover,
the programs are highly difficult to read and modify
• Examples:
• 0110110111110111 0000000100000000 0000000100000000
Programming and Scripting Languages
• Assembly Languages
• Developed to make programming easy
• Symbolic representation of machine language
• Uses short words that are known as mnemonic
• Since the computer cannot understand assembly language, however, a
program called assembler is used to convert assembly language
programs into machine code
• Example: add two numbers
• ADD R1, R2, R3
Assembly Source File
Machine Code File


ADDF3 R1, R2, R3
Assembler …
1101101010011010
… …
Programming and Scripting Languages
• High-Level Languages
• Contains English words and phrases and algebraic expressions
• Easy to learn and program, without knowing how the CPU works
• Example: computes the area of a circle with radius 5
• area = 5 * 5 * 3.1415;
• Popular high-level languages:
• C; C++; C#; COBOL; FORTRAN; BASIC; Pascal; Ada; Objective C;
Java; Visual Basic; Delphi
• Python
Programming and Scripting Languages
• Compiling source code
• A program written in a high-level language is called a source
program
• Since a computer cannot understand a source program. Program called
a compiler is used to translate the source program into a machine
language program called an object program
• The object program is often then linked with other supporting library
code before the object can be executed on the machine

Source File Compiler Machine-language


Linker Executable File
File

Library Code
Compiling a High-level Program and Executing
It
Executing a High-level Program with an
Interpreter
• An interpreter is a program that both translates and executes the
instructions in a high-level language program
Programming and Scripting Languages
• Scripting Languages
• Interpreted, not compiles
• Client-side (such as JavaScript)
• Server-side (such as PHP or ASP)
Writing Programs
• To write a program in a high-level language, you need:
• Appropriate software
• A text editor to type and edit program statements
• A debugger to help find errors in program code
• A compiler or interpreter to translate the program into machine
language
Programming Logic and Design
• All programming languages use basic programming logic
• If you understand this logic, it will be much easier to learn any
specific language
• The purpose of programming logic and design is to focus on
flowcharts and pseudocode
• The design is the foundation of a good program
Useful Resources

Python:
• https://www.python.org/
• https://www.w3schools.com/python/
• https://wingware.com
References
Textbook:
• Necaise, R. D. (2011). Data Structures and Algorithms using Python. John
Wiley & Sons. [Chapter 1]

Supplementary Reading:
• Miller, B., and Ranum, D. (2011). Problem Solving with Algorithms and
Data Structures using Python (2nd Edition). John Wiley & Sons. [Chapter 1]
• Venit, S., and Drake, E. (2015). Prelude to Programming (6th Edition).
Pearson. [Chapter 0, 1, 3]
Thank you

You might also like