1
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMINGSYLLABUSOBJECTIVES:
•
To know the basics of algorithmic problem solving
•
To read and write simple Python programs.
•
To develop Python programs with conditionals and loops.
•
To define Python functions and call them.
•
To use Python data structures
–
-lists, tuples, dictionaries.
•
To do input/output with files in Python.
UNITI ALGORITHMICPROBLEM SOLVING9
Algorithms, building blocks of algorithms (statements, state, control flow, functions),notation (pseudo code, flow chart, programming language), algorithmic problem solving, simplestrategies for developing algorithms (iteration, recursion). Illustrative problems: find minimum in alist, insert a card in a list of sorted cards, guess an integer number in a range, Towers of Hanoi.
UNIT IIDATA, EXPRESSIONS, STATEMENTS9
Python interpreter and interactive mode; values and types: int, float, boolean, string, andlist; variables, expressions, statements, tuple assignment, precedence of operators, comments;modules and functions, function definition and use, flow of execution, parameters and arguments;Illustrative programs: exchange the values of two variables, circulate the values of n variables,distance between two points.
UNIT IIICONTROLFLOW, FUNCTIONS9
Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chainedconditional (if-elif-else); Iteration: state, while, for, break, continue, pass; Fruitful functions: returnvalues, 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.
UNITIVLISTS, TUPLES, DICTIONARIES9
Lists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, listparameters; Tuples: tuple assignment, tuple as return value; Dictionaries: operations and methods;advanced list processing-list comprehension; Illustrative programs: selection sort, insertion sort,mergesort, histogram.
UNITVFILES, MODULES, PACKAGES9
Files and exception: text files, reading and writing files, format operator; command linearguments, errors and exceptions, handling exceptions, modules, packages; Illustrative programs:word count, copy file.
TOTAL : 45 PERIODSTEXT BOOKS
Allen B. Downey, ‘‘Think Python: How to Think Like a Computer Scientist
, 2nd edition,Updatedfor Python 3,
Shroff/OReilly Publishers, 2016
(http://greenteapress.com/wp/thinkpython/)Guido van Rossum and F
red L. Drake Jr, ―An Introduction to Python–
Revised and updated forPython 3.2, Network Theory Ltd., 2011.
REFERENCES
:
John V Guttag, ―Introduction to Computation and Programming Using Python, Revised and
expanded Edition, MIT Press , 2013Robert Se
dgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An
Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
2
UNIT I ALGORITHMIC PROBLEM SOLVINGINTRODUCTIONPROBLEM SOLVING
Problemsolving 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 acorrect program.
PROBLEM SOLVING TECHNIQUES
Problem solving techniqueis a set of techniques that helps in providing logic for solving aproblem.Problem solving can be expressed in the form of 1.Algorithms.2.Flowcharts.3.Pseudo codes.4.Programs
1
.ALGORITHM
It is defined as a sequence of instructions that describe amethod for solving a problem.In other words it is a step by step procedure for solving a problem
•
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.
Qualities of a good algorithm
The following are the primary factors that are oftenused to judge the quality of thealgorithms.
Time
–
To execute a program, the computer system takes some amount of time. Thelesseris the time required, the better is the algorithm.
Memory
–
To execute a program, computer system takes some amount of memoryspace.The lesser is the memory required, the better is the algorithm.
Accuracy
–
Multiple algorithms may provide suitable or correct solutions to a givenproblem, some of these may provide more accurate results than others, and such algorithms may besuitable
Building Blocks of Algorithm
As algorithm is a part of the blue-print or plan for the computer program. An algorithm isconstructed using following blocks.
•
Statements
•
States
•
Controlflow
•
Function
3
Statements
Statements are simple sentences written in algorithm for specific purpose. Statements mayconsists of
assignment statements, input/output statements, comment statementsExample:
•
Read the value of a’ //This is input statement
•
Calculate c=a+b//This is assignment statement
•
Print the value of c // This is output statementComment 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 initialstate, will terminate 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 torun in program at a giventime. There are three types of flows, they are1.Sequential control flow2.Selection or Conditional control flow3.Looping or repetition control flow
Sequential control flow:
The name suggests the sequential control structure is used to perform the action oneafteranother. Only one step is executed once. The logic is top to bottom approach.ExampleDescription: To find the sum of two numbers.1.Start2.
Read the value of a’
3.
Read the value of b’
4.Calculate sum=a+b5.Print the sum of two number6.Stop
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
thenperform some action
ELSE IF
CONDITION
is
FALSE
thenperform some actionThe conditional control flow is explained with the example of finding greatest of twonumbers.ExampleDescription: finding the greater number1.Start2.Read a
43.Read b4.If a>b then4.1.Print a is greaterelse4.2.Print b is greater5.Stop
Repetition control flow
Repetition control flow means that one or more steps are performed repeatedly until somecondition is reached. This logic is used for producing loops in program logic when one one moreinstructions may need to be executed several times or depending on condition.Basic Structure:Repeat until
CONDITION
is trueStatementsExampleDescription: to print the values from 1 to n1.Start2.
Read the value of n’
3.Initialize i as 14.Repeat step 4.1 until i< n4.1.Print i5.Stop
Function
A function is a block of organized, reusable code that is used to perform a single, relatedaction. Function is also named as
methods, sub-routines
.Elements of functions:1.Name for declaration offunction2.Body consisting local declarationand statements3.Formal parameter4.Optional result type.Basic Syntaxfunction_name(parameters)function statementsend function
Algorithm for addition of two numbers using function
Mainfunction()Step 1:
Start
Step 2:
Call the function add()
Step 3:
Stop
sub function add()
Step1:
Functionstart