UNIT-1.pptx python for engineering first year students
SabarigiriVason
30 views
83 slides
Feb 26, 2025
Slide 1 of 83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
About This Presentation
thak you
Size: 1.86 MB
Language: en
Added: Feb 26, 2025
Slides: 83 pages
Slide Content
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
A computer is a machine that manipulates data according to a set of instructions. Computer is fast operating electronic device that receives data (input), processes the data, stores data, and produces resultant data (output). INTRODUCTION TO COMPUTER
Computers are the Combination of two things. They are Hardware Software OVERVIEW OF COMPUTER
Computer hardware is a visible components of the Computer that enables the Computer System to function properly . Examples- CPU, Monitor, keyboard, Mouse., etc HARDWARE
COMPONENTS OF A COMPUTER
A set of programs that are executed sequentially is called software. Software is generic term for organized collection of computer data and instruction . SOFTWARE
Software's can be classified into two types: They are ( i ) System Software (ii) Application Software. TYPES OF SOFTWARE
BASIC PROCESS OF COMPUTER
i ) Speed ii) Accuracy iii) Storage iv) Versatility v) Automation vi) Resource sharing vii) High Reliability viii) Reduction in working cost and man power CHARACTERISTICS OF A COMPUTER
Education Online Education Business Marketing Banking Sector Video Games Training Hospitals Service Sector Science APPLICATIONS OF COMPUTERS
UNIT-1 ALGORITHMIC PROBLEM SOLVING
Algorithm is defined as a step by step procedure for solving any problem. An algorithm is a sequence of finite instructions, often used for calculation and data processing. ALGORITHM
Algorithm has a finite number of inputs. Every instruction should be precise and unambiguous. Ensure that the algorithm has proper termination. Effectiveness of each step is very important . Algorithm should be written in sequences Desired output must be obtained only after the algorithm terminates. CHARACTERISTIC OF ALGORITHM
Accuracy Memory Time Sequence QUALITIES OF AN ALGORITHM
Algorithm has a starting point and a final point. Between these two points are the instructions that solve the problem. Algorithms often have steps that repeat or require decisions (such as logic or comparison). REPRESENTATION OF ALGORITHM
Start the algorithm Read the value of radius r Calculate Area= 3.14*r*r Print the area of the circle Stop Example: Area of the circle
Convert the temperature from fahrenheit to celsius formula: (fahrenheit-32)*(5/9) Circumference of circle formula: 2*3.14*r
BUILDING BLOCKS OF ALGORITHM ( STATEMENTS, STATE, CONTROL FLOW, FUNCTIONS )
Statement is an instruction written in high level language that command the computer to performed a specific action. A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components ( e.g., expressions). STATEMENTS
Simple statements Example: assignment: A := A + 5 Compound statements Example: block : begin ------- end do-loop : do -------- while (i < 10 ); if statement: if (condition) statements KINDS OF STATEMENTS
An algorithm is deterministic automaton for accomplishing a goal which, given an initial state, will terminate in a defined end-state. STATE
It defined as the program statements that specifies the order in which statements are executed. Flow of control (or) control flow (when referring to computer programming) is the order function calls, instructions, and statements are executed or evaluated when a program is running. Sequence Control Structure Selection Control Structures IF ..THEN Structures IF ..THEN...ELSE structure Case Structure CONTROL FLOW
Sequence Control Structure
Selection Control Structures IF..THEN Structures
IF..THEN...ELSE structure
Case Structure
Functions are “self contained” modules of code that accomplish a specific task. Functions usually “take in” data, process it, and “return” a result. Functions can be “called” from the inside of other functions. FUNCTIONS
Reduction in code redundancy Code Reuse Better readability Improved maintainability Improved debugging and testing Advantage
Every function has its own Workspace. This means that every variable inside the function is only usable during the execution of the function (and then the variables go away). FUNCTION WORKSPACE
Pseudocode is a kind of structure English for designing algorithm . Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules. PSEUDOCODES
A pseudocode to add two numbers and display the results: READ num1, num2 result = num1 + num2 WRITE result. Example
Write only one Statement per Line Capitalize initial keyword Indent to show hierarchy End multiline structures Keep statements language independent BASIC GUIDELINES FOR WRITING PSEUDOCODE
It can be done easily on a word processor. Easily modified. Implements structured concepts well. It is simple because it uses English-like statements. No special symbols are used. ADVANTAGES OF PSEUDOCODE
It’s not visual. There is no accepted standard, so it varies widely from company to company. Cannot be compiled not executed. DISADVANTAGES OF PSEUDOCODE
A flow chart is a diagrammatic representation, that illustrates the sequence of operations to be performed to arrive at the solution. The flow chart symbols are linked together with arrows showing the flow direction of the process. FLOWCHART
FLOWCHARTS SYMBOLS
Communication Effective analysis Proper documentation Efficient Coding Proper Testing & Debugging Efficient Program Maintenan ce ADVANTAGES OF FLOWCHARTS
Complex logic Alterations and Modifications No Update DISADVANTAGES OF FLOWCHARTS
A computer is the ideal machine to execute computational algorithms . The computer can perform arithmetic operations It can also perform an operation only when some condition is satisfied (using the conditional branch instruction) PROGRAMMING LANGUAGES
Machine language Assembly language or low level programming language High level programming language TYPES OF LANGUAGES USED IN COMPUTER PROGRAMMING
SOME WELL KNOWN PROGRAMMING LANGUAGES
ALGORITHMIC PROBLEM SOLVING
Algorithms are procedural solutions to problems. These solutions are not answers but specific instructions for getting answers. ALGORITHMIC PROBLEM SOLVING
ALGORITHMIC PROBLEM SOLVING
Understanding the Problem Ascertaining the Capabilities of the Computational Device Choosing between Exact and Approximate Problem Solving Deciding on appropriate Data Structures Algorithm Design Techniques Methods of Specifying an Algorithm Proving an Algorithm’s Correctness Analyzing an Algorithm STEPS FOR DESIGNING AND ANALYZING AN ALGORITHM
Sum of two numbers Algorithm: Step 1 : Start Step 2 : Input the value of A and B. Step 3 : Find the sum of A and B. sum=A+B Step 4 : Print the value of sum Step 5 : Stop. EXAMPLES
SUM OF TWO NUMBERS
Find the area and circumference of circle . Algorithm Step 1 : Start Step 2 : Input the radius of the circle Step 3 : Find the area and circumference using the formula Area = 3.14 * r *r Circumference = 2*3.14*r Step 4 : Print the area and circumference of the circle. Step 5 : Stop
FIND THE AREA AND CIRCUMFERENCE OF CIRCLE .
SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS (ITERATION, RECURSION)
Algorithms are used to manipulate the data for a given problem. For complex problem its algorithm is often divided into smaller units called modules. Two approaches to design an algorithm Top down approach Bottom up approach SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS
A top down approach starts by dividing the complex algorithm into one or more modules. Top down design begins with top most module and incrementally add modules that it calls. Top down approach
In Bottom up design, start designing the most basic bottom modules and then proceed towards designing higher level modules. Bottom up approach
Iteration that involves executing one or more steps for a number of times until some condition is true. It can be implemented using constructs such as while, do while, and for loop. Iteration
Step 1: [ initialise ] set I=1,N=10 Step 2: Repeat step 3&4 while I<=N Step 3: Print I Step 4: set I=I+1 Step 5: end Example
Recursion is a technique of solving a problem by breaking it down into smaller and smaller sub problems. This breaking process will continue until you get a small enough problem that can be easily solved. Recursion
Step 1: start Step 2: input number as n Step 3:call factorial(n) Step 4: stop User defined function Step 1: set f=1 Step 2 :if n==1 then return 1 else set f=n*factorial(n-1) Step 3: Print f Example
Illustrative problems
To find a minimum value into an array of items, take the first element and compare its value against value of other elements. Finally we find the minimum Find minimum in a list
Step 1: start Step 2: declare and read elements of a list. Step 3: declare and set a variable min as first element of the list. Step 4: traverse the list index( i ) from 1 st position to n-1 st position. Step 5: if list[ i ]<min then min = list[ i ] Step 6: Repeat the step until I becomes n. Step 7: display min is the smaller number, Step 8: stop Algorithm
To insert a card in the sorted card, we must increase the list size with 1. we can insert a new card in the appropriate position by comparing each element value with the new card. When the position is found we have to move the remaining elements by one position up and the card can be inserted Insert a card in a list of sorted cards
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection. Linear search
Linear S earch Example
Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[ i ] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit Algorithm
procedure linear_search ( list , value ) for each item in the list if match item == value return the item 's location end if end for end procedure Pseudocode
Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer . For this algorithm to work properly, the data collection should be in the sorted form. Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise , the item is searched for in the sub-array to the right of the middle item. This process continues on the sub-array as well until the size of the subarray reduces to zero. Binary search
The following is our sorted array and let us assume that we need to search the location of value 31 using binary search . Binary Search Example
First, we shall determine half of the array by using this formula − mid = low + (high - low) / 2 Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So , 4 is the mid of the array. Step-1
Now we compare the value stored at location 4, with the value being searched, i.e. 31 . We find that the value at location 4 is 27 , which is not a match. As the value is greater than 27 and we have a sorted array, so we also know that the target value must be in the upper portion of the array. Step-2
We change our low to mid + 1 and find the new mid value again . low = mid + 1 mid = low + (high - low) / 2 Our new mid is 7 now . We compare the value stored at location 7 with our target value 31. Step-3
The value stored at location 7 is not a match , rather it is more than what we are looking for. So, the value must be in the lower part from this location. Step-4
Hence, we calculate the mid again. This time it is 5 . Step-5
We compare the value stored at location 5 with our target value. We find that it is a match. Step-6
We conclude that the target value 31 is stored at location 5 . Binary search halves the searchable items and thus reduces the count of comparisons to be made to very less numbers. Step-7
Procedure binary_search A ← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = lowerBound + ( upperBound - lowerBound ) / 2 if A[ midPoint ] < x set lowerBound = midPoint + 1 if A[ midPoint ] > x set upperBound = midPoint - 1 if A[ midPoint ] = x EXIT: x found at location midPoint end while end procedure Pseudo Code (Binary Search)
To guess an integer number in a range Towers of hanoi Assignment
TOWERS OF HANOI
The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk. RULES
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two disks. We divide the stack of disks in two parts. The largest disk (n th disk) is in one part and all other (n-1) disks are in the second part . Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks onto it. We can imagine to apply the same in a recursive way for all given set of disks.
The steps to follow are − Step 1 − Move n-1 disks from source to aux Step 2 − Move n th disk from source to dest Step 3 − Move n-1 disks from aux to dest
A recursive algorithm for Tower of Hanoi can be driven as follows − START Procedure Hanoi ( disk , source , dest , aux ) IF disk == 1 , THEN move disk from source to dest ELSE Hanoi ( disk - 1 , source , aux , dest ) // Step 1 move disk from source to dest // Step 2 Hanoi ( disk - 1 , aux , dest , source ) // Step 3 END IF END Procedure STOP