Module 1 ATP
Module 1 ATP
Module 1: (7 hrs)
Problem-Solving Strategies
People face problems every day—usually, multiple problems throughout the day.
Eg: Food prepared not enough, Printer not working for an urgent report
Identify the problem and then apply a strategy for solving the problem.
1
Important problem-solving strategies are Trial and Error, Algorithms, Heuristics, Means-Ends
Analysis, and Backtracking (Working backward).
Trial and error is not typically one of the most time-efficient strategies, it is a commonly used
one.
2)Algorithm
An algorithm is a problem-solving formula that provides step-by-step instructions used to
achieve a desired outcome.
Detailed instructions that produce the same result every time they are performed.
Eg: Google: Which link first?
Facebook: which posts to display on the newsfeed
3) Heuristic
A heuristic is a general problem-solving framework
As mental shortcuts that are used to solve problems. A “rule of thumb” is an example of a
heuristic.
Such a rule saves the person’s time and energy when making a decision, but, it is not always the
best method for making a rational decision.
2
Eg: working backwards heuristic: to reach a wedding at 12.pm
Another useful heuristic is the practice of accomplishing a large goal or task by breaking it into a
series of smaller steps like completing a project
4)Means-end analysis
This strategy involves choosing and analysing an action at a series of smaller steps to move
closer to the goal.
Eg: Tower of Hanoi paradigm.
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack or on an empty rod.
3. No larger disc may be placed on top of a smaller disk.
For example, a student might be faced with the problem of how to successfully get through
exam.
Current state: They haven’t started studying,
end goal: to pass the exams.
A solution: putting their phone on “do not disturb” mode while studying
General problem-solving
Heuristic Working backwards; breaking a task into steps
framework
5)Backtracking
Backtracking is a problem-solving algorithmic technique that involves finding a solution
incrementally by trying different options and undoing them if they lead to a dead end.
3
Eg: Searching for a path in a maze or solving puzzles like Sudoku.
4
Definition: Problem Solving is the sequential process of analysing information related to a given
situation and generating appropriate response options.
5
2. Formulating a Model
Understand the processing part of the problem.
Figuring out how to make use of the available data to compute an answer
Broad overview of the sequence of operations that are to be carried out to solve
the given problem
Eg. for a model: 1 = ( 1 + 2 + 3 + ⋯ + )/
3. Developing an Algorithm
Having understood the problem and formulated a model, a precise plan of what
the computer is expected to do have to be developed.
Definition: Algorithm is a precise sequence of instructions for solving a problem.
Two commonly used representations for an algorithm is by using
(1) pseudocode: : pseudocode is a simple and concise sequence of
English-like instructions to solve a problem.
Pseudocode is often used as a way of describing a computer program to
someone who doesn’t understand how to program a computer
(2) flowcharts: A flowchart is a visual representation of an algorithm.
It is a diagram made up of boxes, diamonds and other shapes, connected
by arrows.
6
4. Writing the Program
Transform the algorithm into a set of instructions that can be understood by the
computer.
Writing a program is often called "coding" or “implementing an algorithm”.
So the code (or source code) is actually the program itself. Source code would
vary depending on the programming language.
Definition: Compiling is the process of converting a program into instructions that
can be understood by the computer.
7
ESSENTIALS OF PYTHON PROGRAMMING - Creating and using variables in Python,
Numeric and String data types in Python, Using the math module, Using the Python
Standard Library for handling basic I/O - print, input, Python operators and their
precedence.
Python
– general-purpose and interpreted : processed at runtime by the interpreter.
– Interactive
– object-oriented
– high-level programming language(Python2, Python3)
• It was created by Guido van Rossum during 1985-1990.
Properties
• Easy-to-learn: Python has few keywords, simple structure, and a clearly defined syntax
• Easy-to-read
• Easy-to-maintain
• A broad standard library: Python's library is very portable and cross-platform
compatible
• Interactive
• Portable: Python can run on different hardware platforms
• Automatic garbage collection.
Python files have extension .py.
Eg: Save this file as hello.py: print "Hello, Python!”
To execute: python hello.py
Python Identifiers
• a name used to identify a variable, function, class, module or other object.
• starts with a letter A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores and digits (0 to 9).
• does not allow punctuation characters such as @, $, and % within identifiers.
• Case sensitive programming language.
– sum and Sum are two different identifiers in Python.
Python keywords
8
Indentation
Python provides no braces to indicate blocks of code for class and function definitions or
flow control.
• Blocks of code are denoted by line indentation
• The number of spaces in the indentation is variable, but all statements within the
block must be indented the same amount.
A hash sign (#) that is not inside a string literal begins a comment.
Eg: # This is a program for matrix multiplication
Python interpreter ignores them.
Variable
Reserved memory locations to store values.
Based on the data type of a variable, the interpreter allocates memory and decides what
can be stored in the reserved memory.
The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the
right of the = operator is the value stored in the variable.
counter = 100 # An integer assignment
miles = 1000.0 # A floating point assignment
name = “John” # An string assignment
Python allows to assign a single value to several variables simultaneously.
•a=b=c=1
• a, b, c = 1, 2, "john"
Standard Data Types
9
Numeric data type
• int: Int, or integer, is a whole number, positive or negative, without decimals
x =1
y = 35656222554887711
z = -3255522
• float : "floating point number" is a number, positive or negative, containing one or more
decimals.
x = 1.10
y = 35e3
z = -35.59
• complex : Complex numbers are written with a "j" as the imaginary part:
x = 3+5j
y = 5j
z = -5j
String data type
Strings in Python are identified as a contiguous set of characters represented in the
quotation
Marks(single or double quotes).
• Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting
at 0
in the beginning of the string
• The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition
operator.
Eg: str = 'Hello World!'
1)print str # Prints complete string
2)print str[0] # Prints first character of the string
3)print str[2:5] # Prints characters starting from 3rd to 5th
4)print str[2:] # Prints string starting from 3rd character
5)print str * 2 # Prints string two times
6)print str + "TEST" # Prints concatenated string
result
Hello World!
10
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
A statement is a unit of code that the Python interpreter can execute.
Eg: For example, the script
x=2
print( x)
An expression is a combination of values, variables, and operators.
Eg: x + 17
11
sqrt(x) returns the square root of the number.
import math
a = 2.3
print ("The ceil of 2.3 is : ", end="")
print (math.ceil(a))
print ("The floor of 2.3 is : ", end="")
print (math.floor(a))
Using the Python Standard Library for handling basic I/O - print, input
The print() function prints the specified message to the screen.
The message can be a string, or any other object, the object will be converted into a string
before written to the screen.
print("Hello World")
The input() function allows to read user input.
x = input('Enter your name:')
print('Hello, ' + x)
Python Operator Precedence
An expression may have multiple operators to be evaluated. The operator precedence
defines the order in which operators are evaluated.
BODMAS rule is employed by Python interpreter for arithmetic operators, where
the brackets are evaluated first, the division and multiplication operators next, followed
by addition and subtraction operators.
12
File Handling
Allows users to handle files i.e., to read and write files
In Python, files can broadly be categorized into two types based on their mode of operation:
Text Files: These store data in plain text format. Examples include .txt files.
Binary Files: These store data in binary format, which is not human-readable.
Examples include images, videos, and executable files.
Modes:
1. r: open an existing file for a read operation.
2. w: open an existing file for a write operation. If the file already contains some data,
then it will be overridden but if the file is not present then it creates the file as well.
3. a: open an existing file for append operation. It won’t override existing data.
13
# Python code to illustrate read() mode
file = open("s1cse.txt", "r")
print (file.read())
print (file.read(5))
file.close()
14