You are on page 1of 19

Problem Solving using Python

(ITFC0101)
Introduction, IDE, Program, Problem Solving
Some terms you will frequently encounter

• High-level language
• Low-level language
• Portable
• Python interpreter
• Python prompt
• Script
Python Prompt
Python Script

• Writing a program in a file use the interpreter to execute the contents of the file.
• Such a file is called a script.
• Scripts have the advantage that they can be saved to disk, printed, and so on.
• Using script needs text editor and interpreter.
IDE
Integrated Development environment

• For Python (and many other programming languages) there are programs that include
both a text editor and a way to interact with the interpreter.

• We call these development environments (sometimes Integrated Development


Environment or IDE).
Programming Methodology

• Problem solving
• Problem statement and analysis.
• Develop a high-level algorithm.
• Develop a low-level algorithm.
• Coding
• Choose a programming language.
• Code the program using the selected algorithm.
• Test the program and correct the errors.
Algorithm
Definition— Solution to a programming problem.

Algorithm: A combination of sequence of finite steps to solve a particular problem

• Algorithm can be written in two different ways.


• Pseudo-code — English-like steps that describes the solution. (preferred)
• Flowcharts — Picture with specific blocks detailing out the logical flow of the solution
Properties of Algorithm
Definition— Solution to a programming problem.

• Input: 0 or more
• Output: 1 or more
• Finiteness: Should terminate within finite time, OR after finite iterations
• Deterministic / Definiteness: Non-ambiguous
• Task oriented steps: All steps should have meaningful task e.g. x=x //Useless
• Generic: Steps should be language independent
Flowchart Building Blocks
Example 1
Problem statement: Watch a movie at home
Start

• Algorithm
Switch on the TV
• Switch on the TV.
Change to the movie
channel
• Go to the movie channel.
Sit down watch movie
• Sit down and watch the movie.
End
Example 2
Problem Statement: Withdraw cash from ATM

• Go to the ATM
• Insert your card into the machine.
• Press in your code
• Choose “Withdraw” and enter Amount required
• Take the cash, slip and card.
Example 3
Problem statement: Calculate
the interest of a bank deposit. You are to read the
amount, years and interest rate from the keyboard and print the interest
amount.
• Algorithm
• Read Principal Amount
• Read Years
• Read Rate
• Set Interest as Amount *
Rate*Years/100
• Print Interest
Example 4
Problem Statement: Print what to do when driving to a traffic signal

• Algorithm
• Read traffic signal
• If signal is Green then set Action as GO
• Else
• Set action as STOP
• Print Action
Example 6
Problem Statement: Print Title for a person (Either Mr. or Miss. or Mrs.). You are to read the gender (and status if needed).

• Algorithm
• Read Gender
• If Gender is MALE then
• Title is Mr.
• Else
• Read status
• If status is MARRIED then
• Title is Mrs.
• Else
• Title is Miss.
Example 7
Problem Statement: Find the average of a given list of numbers
• Low level algorithm
1. Initialize SUM as 0 and COUNT as 0.
• High level algorithm 2. If there are no more numbers remaining to be processed, then
go to step 7.
1. Find the SUM of the given 3. Set ITEM as next number in the list.
numbers
4. Add ITEM to SUM
2. Find the COUNT of the given
numbers 5. Increment COUNT by 1
6. Go back to step 2
3. AVERAGE is SUM/COUNT
7. If COUNT in equal to 0, then
1. AVERAGE is “undefined”
8. Else
Strategies of Developing Algorithms
Iteration
Strategies of Developing Algorithms
Recursion

You might also like