UNIT-1.pptx python for engineering first year students

SabarigiriVason 30 views 83 slides Feb 26, 2025
Slide 1
Slide 1 of 83
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83

About This Presentation

thak you


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

NOTATION (PSEUDO CODE, FLOW CHART, PROGRAMMING LANGUAGE)

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
Tags