Skip to document
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

GE3151 - Problem Solving AND Python Programming Notes 1

python programming notes useful for all students
Course

python programming (cs8151)

86 Documents
Students shared 86 documents in this course
University

Anna University

Academic year: 2020/2021
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Anna University

Comments

Please sign in or register to post comments.
  • Student
    Thank You for the document
  • Student
    Thank You for the document
  • Student
    very useful one
  • DD
    Good to read

Preview text

1

GE8151 - PROBLEM SOLVING AND PYTHON

PROGRAMMING

REGULATIONS – 2017

UNIT – I

Prepared By :
Mr. Vinu S, ME,
Assistant Professor,
St’s College of Engineering,

Chennai -600119.

2

UNIT I

ALGORITHMIC PROBLEM SOLVING

1 ALGORITHMS

In computing, we focus on the type of problems categorically known as algorithmic problems, where their solutions are expressible in the form of algorithms.

The term „algorithm‟ was derived from the name of Mohammed al-Khowarizmi, a Persian mathematician in the ninth century. Al-Khowarizmi → Algorismus (in Latin) → Algorithm.

An algorithm is a well-defined computational procedure consisting of a set of instructions that takes some value or set of values, as input, and produces some value or set of values, as output. In other word, an algorithm is a procedure that accepts data; manipulate them following the prescribed steps, so as to eventually fill the required unknown with the desired value(s).

People of different professions have their own form of procedure in their line of work, and they call it different names. For instance, a cook follows a procedure commonly known as a recipe that converts the ingredients (input) into some culinary dish (output), after a certain number of steps.

1.1 The Characteristics of a Good Algorithm

Precision – the steps are precisely stated (defined).  Uniqueness – results of each step are uniquely defined and only depend on the input and the result of the preceding steps.  Finiteness – the algorithm stops after a finite number of instructions are executed.  Effectiveness – algorithm should be most effective among many different ways to solve a problem.  Input – the algorithm receives input.  Output – the algorithm produces output.  Generality – the algorithm applies to a set of inputs.

1 BUILDING BLOCKS OF ALGORITHM (INSTRUCTIONS, STATE,
CONTROL FLOW, FUNCTIONS)

An algorithm is an effective method that can be expressed within a finite amount of space and time and in a well-defined formal language for calculating a function. Starting from an initial state and initial input, the instructions describe a computation that, when executed,

Input Algorithm Output

For More Visit : Learnengineering
For More Visit : Learnengineering
3

proceeds through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; that algorithms, known as randomized algorithms.

An instruction is a single operation, that describes a computation. When it executed

it convert one state to other.

Typically, when an algorithm is associated with processing information, data can be read from an input source, written to an output device and stored for further processing. Stored data are considered as part of the internal state of the entity performing the algorithm.

In practice, the state is stored in one or more data structures.

The state of an algorithm is defined as its condition regarding stored data. The stored data in an algorithm are stored as variables or constants. The state shows its current values or contents.

Because an algorithm is a precise list of precise steps, the order of computation is

always crucial to the functioning of the algorithm. Instructions are usually assumed to be listed explicitly, and are described as starting "from the top" and going "down to the bottom", an idea that is described more formally by flow of control.

In computer science, control flow (or flow of control ) is the order in which

individual statements, instructions or function calls of an algorithm are executed or evaluated.

For complex problems our goal is to divide the task into smaller and simpler functions during algorithm design. So a set of related sequence of steps, part of larger algorithm is known as functions.

1.2 Control Flow

Normally Algorithm has three control flows they are:  Sequence  Selection  Iteration

Sequence: A sequence is a series of steps that occur one after the other, in the same order every time. Consider a car starting off from a set of lights. Imagine the road in front of the car is clear for many miles and the driver has no need to slow down or stop. The car will start in first gear, then move to second, third, fourth and finally fifth. A good driver (who doesn‟t have to slow down or stop) will move through the gears in this sequence, without skipping gears.

Selection: A selection is a decision that has to be made. Consider a car approaching a set of lights. If the lights turn from green to yellow, the driver will have to decide whether to stop or continue through the intersection. If the driver stops, she will have to wait until the lights are green again.

Iteration: Iteration is sometimes called repetition, which means a set of steps which are repeated over and over until some event occurs. Consider the driver at the red light, waiting

4

for it to turn green. She will check the lights and wait, check and wait. This sequence will be repeated until the lights turn green.

1 NOTATION OF ALGORITHM

There are four different Notation of representing algorithms:  Step-Form  Pseudocode  Flowchart  Programming Language

1.3 Algorithm as Step-Form

It is a step – by – step procedure for solving a task or problem. The steps must be ordered, unambiguous and finite in number. It is English like representation of logic which is used to solve the problem.

Some guidelines for writing Step-Form algorithm are as follows:

 Keep in mind that algorithm is a step-by-step process.  Use begin or start to start the process.  Include variables and their usage.  If there are any loops, try to give sub number lists.  Try to give go back to step number if loop or condition fails.  Use jump statement to jump from one statement to another.  Try to avoid unwanted raw data in algorithm.  Define expressions.  Use break and stop to terminate the process.

For accomplishing a particular task, different algorithms can be written. The different algorithms differ in their requirements of time and space. The programmer selects the best suited algorithm for the given task to be solved.

Let as look at two simple algorithms to find the greatest among three numbers, as follows:

Algorithm 1:

Step 1: Start. Step 2: Read the three numbers A,B,C. Step 3: Compare A and B. If A is greater perform step 4 else perform step 5. Step 4: Compare A and C. If A is greater, output “A is greater” else output “C is greater”. Then go to step 6 Step 5: Compare B and C. If B is greater, output “B is greatest” else output “C is greatest”. Step 6: Stop.

For More Visit : Learnengineering
For More Visit : Learnengineering
7

Connector (Inspection)

T his symbol is typically small and is used as a Connector to show a jump from one point in the process flow to another. Connectors are usually labeled with capital letters (A, B, AA) to show matching jump points. They are handy for avoiding flow lines that cross other shapes and flow lines. They are also handy for jumping to and from a sub- processes defined in a separate area than the main flowchart. Predefined Process (Subroutine)

A Predefined Process symbol is a marker for another process step or series of process flow steps that are formally defined elsewhere. This shape commonly depicts sub-processes (or subroutines in programming flowcharts).

Preparation

As the names states, any process step that is a Preparation process flow step, such as a set-up operation. Eg, Used in For Loop. Magnetic Disk (Database)

The most universally recognizable symbol for a data storage location, this flowchart shape depicts a database.

1.3 Control Structures of Pseudocode and Flowcharts

Each control structures can be built from the basic elements as shown below.

Sequence: A sequence is a series of steps that take place one after another. Each step is represented here by a new line.

Our sequence example of changing gears could be described as follows :

Pseudocode Flowchart

BEGIN

Statement Statement END

8

Pseudocode Flowchart BEGIN

1st Gear

2nd Gear

3rd Gear

4th Gear

5th Gear

END

Selection : A Selection is a decision. Some decisions may be answered as Yes or No. These are called binary selections. Other decisions have more than two answers. These are called multiway selections.

A binary selection may be described in pseudocode and flow chart as follows :

Pseudocode Flowchart

IF (question) THEN

statement

ELSE

statement

ENDIF

An example of someone at a set of traffic lights follows :

Pseudocode Flowchart IF (lights are green) THEN

Go

ELSE

Stop

ENDIF

A Multiway Selection may be described as follows :

For More Visit : Learnengineering
For More Visit : Learnengineering
9

Pseudocode Flowchart

CASEWHERE (question)

Alternative 1: Statement

Alternative 2 : Statement

OTHERWISE : Statement

ENDCASE

An example of someone at a set of traffic lights follows :

Pseudocode Flowchart

CASEWHERE Lights are :

Green : Go

Amber : Slowdown

Red : Stop

ENDCASE

Repetition: A sequence of steps which are repeated a number of times, is called repetition. For a repeating process to end, a decision must be made. The decision is usually called a test. The position of this test divides repetition structures into two types : Pre-test and Post-test repetitions.

Pre-test repetitions (sometimes called guarded loops) will perform the test before any part of the loop occurs.

Post-test repetitions (sometimes called un-guarded loops) will perform the test after the main part of the loop (the body) has been performed at least once.

Pre-test repetitions may be described in as follows :

10

Pseudocode Flowchart

WHILE (question)

Statement

ENDWHILE

A traffic lights example follows :

Pseudocode Flowchart

WHILE (lights are not green)

wait

ENDWHILE

Post-test repetitions may be described as follows:

A traffic lights example follows :

Pseudocode Flowchart

REPEAT

Statement

UNTIL (Question)

For More Visit : Learnengineering
For More Visit : Learnengineering
13
1.3 Programming Language

Computers won‟t understand your algorithm as they use a different language. It will need to be translated into code, which the computer will then follow to complete a task. This code is written in a programming language. There are many programming languages available. Some of you may come across are C, C++, Java, Python, etc... Each of this language is suited to different things. In this book we will learn about Python programming language.

1 RECURSION

A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input. More generally if a problem can be solved utilizing solutions to smaller versions of the same problem, and the smaller versions reduce to easily solvable cases, then one can use a recursive algorithm to solve that problem.

1.4 Example

For positive values of n, let's write n!, as we known n! is a product of numbers starting from n and going down to 1. n! = n. (n-1)...... 2 .1. But notice that (n-1) ... 2 is another way of writing (n-1)!, and so we can say that n!=n.(n−1)!. So we wrote n! as a product in which one of the factors is (n−1)!. You can compute n! by computing (n−1)! and then multiplying the result of computing (n−1)! by n. You can compute the factorial function on n by first computing the factorial function on n−1. So computing (n−1)! is a subproblem that we solve to compute n!.

Let's look at an example: computing 5!.

 You can compute 5! as 5⋅4!.  Now you need to solve the subproblem of computing 4!, which you can compute as 4⋅ 3!.  Now you need to solve the subproblem of computing 3!, which is 3⋅2!.  Now 2!, which is 2⋅1!.  Now you need to compute 1!. You could say that 1! =1, because it's the product of all the integers from 1 through 1. Or you can apply the formula that 1! = 1⋅0!. Let's do it by applying the formula.  We defined 0! to equal 1.  Now you can compute1!=1⋅0!=1.  Having computed 1!=1, you can compute 2!=2⋅1!=2.  Having computed 2!=2, you can compute 3!=3⋅2!=6.  Having computed 3!=6, you can compute 4!=4⋅3!=24.  Finally, having computed 4!=24, you can finish up by computing 5!=5⋅4!=120.

So now we have another way of thinking about how to compute the value of n!, for all nonnegative integers n:

14

 If n=0, then declare that n!=1.  Otherwise, n must be positive. Solve the subproblem of computing (n−1)!, multiply this result by n, and declare n! equal to the result of this product.

When we're computing n! in this way, we call the first case, where we immediately know the answer, the base case, and we call the second case, where we have to compute the same function but on a different value, the recursive case.

Base case is the case for which the solution can be stated non‐recursively (ie. the answer is known). Recursive case is the case for which the solution is expressed in terms of a smaller version of itself.

1.4 Recursion in Step Form

Recursion may be described as follows:

Syntax for recursive algorithm

Step 1: Start Step 2: Statement(s) Step 3: Call Subprogram(argument) Step 4: Statement(s) Step 5: Stop

Step 1: BEGIN Subprogram(argument) Step 2: If base case then return base solution Step 3: Else return recursive solution, include call subprogram(smaller version of argument) Recursive algorithm solution for factorial problem

Step 1: Start Step 2: Read number n Step 3: Call factorial(n) and store the result in f Step 4: Print factorial f Step 5: Stop

Step 1: BEGIN factorial(n) Step 2: If n==0 then return 1 Step 3 : Else Return n*factorial(n-1) Step 4:END factorial

For More Visit : Learnengineering
For More Visit : Learnengineering
15
1.4 Recursion in Flow Chart
Syntax for recursive flow chart

Figure 1 Recursive Flow chart Syntax

Figure 1 Recursive flow chart solution for factorial problem

Start

Stop

Read N

Fact=Factorial(N)

Print Fact

Begin Factorial(N)

Is N==

Return 1 Return N*Factorial(N-1)

End Factorial

No

Yes

Subprogram call(argument)

Is base Case

Begin subprogram(argument)

End Subprogram

Return recursive solution with Subprogram call(smaller version of argument)

No

Yes

Return base solution

16
1.4 Recursion in Pseudocode
Syntax for recursive Pseudocode

SubprogramName(argument) BEGIN SUBPROGRAM SubprogramName(argument) Is base Case Return base case solution Else Return recursive solution with SubprogramName (smaller version of argument) ENDSUBPROGRAM Recursive Pseudocode solution for factorial problem

BEGIN Factorial BEGIN SUBPROGRAM Fact(n)

READ N IF n==0 THEN RETURN 1

f= Fact(N) ELSE RETURN n*Fact(n-1)

PRINT f ENDSUBPROGRAM

END

1 ALGORITHMIC PROBLEM SOLVING

We can consider algorithms to be procedural solutions to problems. These solutions are not answers but specific instructions for getting answers. It is this emphasis on precisely defined constructive procedures that makes computer science distinct from other disciplines. In particular, this distinguishes it from theoretical mathematics, whose practitioners are typically satisfied with just proving the existence of a solution to a problem and, possibly, investigating the solution‟s properties. Let us discuss a sequence of steps one typically goes through in designing and analysing an algorithm

  1. Understanding the problem
  2. Ascertain the capabilities of the computational device
  3. Exact /approximate solution
  4. Decide on the appropriate data structure
  5. Algorithm design techniques
  6. Methods of specifying an algorithm
  7. Proving an algorithms correctness
  8. Analysing an algorithm

Understanding the problem:

The problem given should be understood completely. Check if it is similar to some standard problems & if a Known algorithm exists. Otherwise a new algorithm has to be developed.

For More Visit : Learnengineering
For More Visit : Learnengineering
19

Step base algorithm to find minimum element in a list

Step 1: Start Step 2: READ total number of element in the List as N Step 3: READ first element as E Step 4: MIN =E Step 5: SET i= Step 6: IF i>n go to Step 11 ELSE go to step 7 Step 7: READ ith element as E Step 8: IF E < MIN THEN SET MIN = E Step 9: i=i+ Step 10: go to step 6 Step 11: Print MIN Step 12: Stop

Figure 1 Flow Chart to Find Minimum Element in a List

20

Pseudocode to find minimum element in a list

READ total number of element in the List as N READ first element as E SET MIN =E SET i= WHILE i<=n READ ith element as E IF E < MIN THEN SET MIN = E ENDIF INCREMENT i by ONE ENDWHILE PRINT MIN

1.6 Insert a Card in a List of Sorted Cards

Insert a Card in a List of Sorted Cards – is same as inserting an element into a sorted array.

We start from the high end of the array and check to see if that's where we want to insert the data. If so, fine. If not, we move the preceding element up one and then check to see if we want to insert x in the “hole” left behind. We repeat this step as necessary.

Thus the search for the place to insert x and the shifting of the higher elements of the array

are accomplished together.

Figure 1 Steps to Insert a Card in a List of Sorted Cards

Step base algorithm to Insert a Card in a List of Sorted Cards

Step 1: Start Step 2: Declare variables N, List[], i, and X. Step 3: READ Number of element in sorted list as N Step 4: SET i= Step 5: IF i<N THEN go to step 6 ELSE go to step 9 Step 6: READ Sorted list element as List[i] Step 7: i=i+ Step 8: go to step 5 Step 9: READ Element to be insert as X Step 10: SET i = N- Step 11: IF i>=0 AND X<List[i] THEN go to step 12 ELSE go to step Step 12: List[i+1]=List[i] Step 13: i=i-

Position 0 1 2 3 4 5 Element To Be Insert
Original List 4 6 9 10 11 7
7>11 × 4 6 9 10 11
7>10 × 4 6 9 10 11
7>9 × 4 6 9 10 11
7>6 √ 4 6 7 9 10 11
For More Visit : Learnengineering
For More Visit : Learnengineering
21

Step 14: go to step 11 Step 15: List[i+1]=X

Figure 1 Flow Chart to Insert a Card in a List of Sorted Cards

Pseudocode to Insert a Card in a List of Sorted Cards

READ Number of element in sorted list as N SET i= WHILE i<N READ Sorted list element as List[i]

22

i=i+ ENDWHILE READ Element to be insert as X SET i = N- WHILE i >=0 and X < List[i] List[i+1] =List[i] i = i – 1 END WHILE List[i+1] = X

1.6 Guess an Integer Number in a Range

Let's play a little game to give an idea of how different algorithms for the same problem can have wildly different efficiencies. The computer is going to randomly select an integer from 1 to N and ask you to guess it. The computer will tell you if each guess is too high or too low. We have to guess the number by making guesses until you find the number that the computer chose. Once found the number, think about what technique used, when deciding what number to guess next?

Maybe you guessed 1, then 2, then 3, then 4, and so on, until you guessed the right number. We call this approach linear search , because you guess all the numbers as if they were lined up in a row. It would work. But what is the highest number of guesses you could need? If the computer selects N, you would need N guesses. Then again, you could be really lucky, which would be when the computer selects 1 and you get the number on your first guess. How about on average? If the computer is equally likely to select any number from 1 to N, then on average you'll need N/2 guesses.

But you could do something more efficient than just guessing 1, 2, 3, 4, ..., right? Since the computer tells you whether a guess is too low, too high, or correct, you can start off by guessing N/2. If the number that the computer selected is less than N/2, then because you know that N/2 is too high, you can eliminate all the numbers from N/2 to N from further consideration. If the number selected by the computer is greater than N/2, then you can eliminate 1 through N/2. Either way, you can eliminate about half the numbers. On your next guess, eliminate half of the remaining numbers. Keep going, always eliminating half of the remaining numbers. We call this halving approach binary search , and no matter which number from 1 to N the computer has selected, you should be able to find the number in at most log 2 N+1 guesses with this technique. The following table shows the maximum number of guesses for linear search and binary search for a few number sizes:

Highest Number Max Linear Search Guesses Max Binary Search Guesses 10 10 4

100 100 7

1,000 1,000 10

For More Visit : Learnengineering
For More Visit : Learnengineering
25

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:

Rules

 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.

Figure 1 Step by Step Moves in Solving Three Disk Tower of Hanoi Problem

Algorithm:

To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with lesser amount of disks, say → 1 or 2. We mark three towers with name, source, destination and aux (only to help moving the disks). If we have only one disk, then it can easily be moved from source to destination tower.

If we have 2 disks −

 First, we move the smaller (top) disk to aux tower.  Then, we move the larger (bottom) disk to destination tower.  And finally, we move the smaller disk from aux to destination tower.

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 (nth 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 (n-1)

disks onto it. We can imagine to apply the same in a recursive way for all given set of disks.

26

The steps to follow are − Step 1 − Move n-1 disks from source to aux Step 2 − Move nth disk from source to dest Step 3 − Move n-1 disks from aux to dest A recursive Step based algorithm for Tower of Hanoi can be driven as follows −

Step 1: BEGIN Hanoi(disk, source, dest, aux) Step 2: IF disk == 1 THEN go to step 3 ELSE go to step 4 Step 3: move disk from source to dest AND go to step 8 Step 4: CALL Hanoi(disk - 1, source, aux, dest) Step 5: move disk from source to dest Step 6: CALL Hanoi(disk - 1, aux, dest, source) Step 7: END Hanoi A recursive Pseudocode for Tower of Hanoi can be driven as follows –

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

Figure 1 Flow Chart for Tower of Hanoi Algorithm

Tower of Hanoi puzzle with n disks can be solved in minimum 2n− 1 steps.

For More Visit : Learnengineering
For More Visit : Learnengineering
27
1 MORE ILLUSTRATIVE PROBLEMS
1.7 Temperature Conversion

The commonly used scales for representing temperature are the Celsius scale, denoted in °C and the Fahrenheit scale (°F). Conversion from one scale to another can be done using the following equation. F= (C*1)+ C=(F-32)*1/1. Let us look at the algorithm, Pseudocode, and flow chart for the Temperature conversion problem.

Algorithm in step form

Step 1: Start Step 2: READ the value in Celsius scale as C Step 3: Use the formula F= (C*1)+32 to convert Celsius scale to Fahrenheit scale Step 4: PRINT F Step 5: READ the value in Fahrenheit scale as F Step 6: Use the formula C=(F-32)*1/1 to convert Fahrenheit scale to Celsius scale Step 7: PRINT C Step 8: Stop

Pseudocode

READ Celsius value as C COMPUTE F= (C*1)+ PRINT F as Fahrenheit READ Fahrenheit value as F COMPUTE C=(F-32)*1/1. PRINT C as Celsius

Flow Chart

28

Figure 1 Flow Chart for Temperature Conversion

1.7 Swap Two Values without Temporary Variable
Swapping is the process of exchanging values of two variables. It can be

performed in with temporary variable and without temporary variable method. Here we are going to discuss about the algorithm, Pseudocode, and flow chart for swapping without using temporary variable.

Algorithm in step form

Step 1: Start Step 2: READ the values of A, B Step 3: A=A+B Step 4: B= A-B Step 5: A= A-B Step 6: PRINT A, B Step 8: Stop Pseudocode

READ two values say A, B COMPUTE A=A+B COMPUTE B= A-B COMPUTE A= A-B PRINT Swapped values of A, B

For More Visit : Learnengineering
For More Visit : Learnengineering
31

Figure 1 Flow Chart to Check Whether a Person is Eligible to Vote or Not.

1.7 Biggest Among Three Numbers
Following algorithm, Pseudocode, and flow chart are designed to print the

biggest number among the given 3 numbers.

Algorithm in step form

Step 1: Start. Step 2: Read the three numbers A, B, C.

Step 3: Compare A and B. If A is greater perform step 4 else perform step 5. Step 4: Compare A and C. If A is greater, output “A is greater” else output “C is greater”. Then go to step 6 Step 5: Compare B and C. If B is greater, output “B is greatest” else output “C is greatest”.

Then go to step 6 Step 6: Stop. Pseudocode

READ three numbers as A, B, C IF A>B THEN IF A>C THEN PRINT “A is Big” ELSE PRINT “C is Big” ELSE IF B>C THEN PRINT “B is Big”

32
ELSE

PRINT “C is Big” END IF

Figure 1 Flow Chart to Print Biggest Among Three Numbers
1.7 Armstrong Number

Armstrong numbers are numbers, if the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number since 1^3+5^3+3^3=

Following algorithm, Pseudocode, and flow chart are used to check whether the given number is Armstrong number or not.

Algorithm in step form

Step 1: Start. Step 2: READ a number as N Step 3: sum= Step 4: temp=N Step 5: IF temp> 0 go to step 5 ELSE go to step 6 Step 5: digit=temp% Step 5: sum =sum+digitdigitdigit Step 5: temp=temp/ Step 5: go to step 5 Step 6: IF N==sum THEN PRINT “Given Number is Armstrong Number” ELSE PRINT “Given Number is Not Armstrong Number”

For More Visit : Learnengineering
For More Visit : Learnengineering
33

Step 7: Stop Pseudocode

READ a number as N SET sum= ASSIGN temp=N

WHILE temp>0 THEN digit=temp% sum =sum+digitdigitdigit temp=temp/ END WHILE

IF N==sum PRINT “Given Number is Armstrong Number” ELSE “Given Number is Not Armstrong Number”

34

Figure 1 Flow Chart to Check Whether the Given Number is Armstrong Number or Not

1.7 Number Palindrome

Palindrome Number is a number that remains the same when its digits are reversed. For example 12521 is a Palindrome Number.

Following algorithm, Pseudocode, and flow chart are used to check whether the given number is Palindrome Number or not.

Algorithm in step form

Step 1: Start. Step 2: READ a number as N

For More Visit : Learnengineering
For More Visit : Learnengineering
37

Step 7: go to step 4 Step 8: SET i=0, total= Step 9: IF i<N THEN go to step 10 ELSE go to step 12 Step 10: total=total+List[i] Step 11: i=i+1, go to 9 Step 12: average=total/N Step 13: PRINT total as Total Marks Step 14: PRINT average as Average Marks

Pseudocode

READ Number of subjects as N

SET i= WHILE i<N READ single subject mark as List[i] i=i+

ENDWHILE SET i=0, total= WHILE i<N total=total+List[i] i=i+

ENDWHILE COMPUTE average=total/N PRINT total as Total Marks PRINT average as Average Marks

38

Figure 1 Flow Chart to Calculate the Total and Average of Marks

1.7 Check Whether a Number is Prime or Not
For More Visit : Learnengineering
For More Visit : Learnengineering
39

If a number is divisible only by itself and 1 then it is called prime Number. Following are the Algorithm, Pseudocode, and flow chart to Check Whether a Number is Prime or not.

Algorithm in step form

Step 1: Start Step 2: READ a Number to check as N Step 3: SET i=2, flag= Step 4: IF i<=N/2 THEN go to step 4 ELSE go to step 5 Step 4: IF N%i==0 THEN SET flag=1 and go to step 5 ELSE go to step 4. Step 4: i=i+1, go to step 4 Step 5: IF flag==1 THEN PRINT “Given Number is Not Prime” ELSE PRINT “Given Number is Prime” Step 6: Stop

Pseudocode

READ a Number to check as N SET i=2, flag= WHILE i<=N/ IF N%i==0 THEN flag= break; ELSE i=i+ END WHILE

IF flag==1 THEN PRINT “Given Number is Not Prime” ELSE

PRINT “Given Number is Prime”
40

Figure 1 Flow Chart to Check Whether a Number is Prime or Not

For More Visit : Learnengineering
For More Visit : Learnengineering
Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

GE3151 - Problem Solving AND Python Programming Notes 1

Course: python programming (cs8151)

86 Documents
Students shared 86 documents in this course

University: Anna University

Was this document helpful?

This is a preview

Do you want full access? Go Premium and unlock all 89 pages
  • Access to all documents

  • Get Unlimited Downloads

  • Improve your grades

Upload

Share your documents to unlock

Already Premium?
1
GE8151 - PROBLEM SOLVING AND PYTHON
PROGRAMMING
REGULATIONS 2017
UNIT I
Prepared By :
Mr. Vinu S, ME,
Assistant Professor,
St.Joseph’s College of Engineering,
Chennai -600119.
2
UNIT I
ALGORITHMIC PROBLEM SOLVING
1.1 ALGORITHMS
In computing, we focus on the type of problems categorically known as algorithmic
problems, where their solutions are expressible in the form of algorithms.
The term „algorithm‟ was derived from the name of Mohammed al-Khowarizmi, a
Persian mathematician in the ninth century. Al-Khowarizmi Algorismus (in Latin)
Algorithm.
An algorithm is a well-defined computational procedure consisting of a set of
instructions that takes some value or set of values, as input, and produces some value or
set of values, as output. In other word, an algorithm is a procedure that accepts data;
manipulate them following the prescribed steps, so as to eventually fill the required unknown
with the desired value(s).
People of different professions have their own form of procedure in their line of work,
and they call it different names. For instance, a cook follows a procedure commonly known
as a recipe that converts the ingredients (input) into some culinary dish (output), after a
certain number of steps.
1.1.1 The Characteristics of a Good Algorithm
Precision the steps are precisely stated (defined).
Uniqueness results of each step are uniquely defined and only depend on the input
and the result of the preceding steps.
Finiteness the algorithm stops after a finite number of instructions are executed.
Effectiveness algorithm should be most effective among many different ways to
solve a problem.
Input the algorithm receives input.
Output the algorithm produces output.
Generality the algorithm applies to a set of inputs.
1.2 BUILDING BLOCKS OF ALGORITHM (INSTRUCTIONS, STATE,
CONTROL FLOW, FUNCTIONS)
An algorithm is an effective method that can be expressed within a finite amount of
space and time and in a well-defined formal language for calculating a function. Starting from
an initial state and initial input, the instructions describe a computation that, when executed,
Algorithm
Input
Output
For More Visit : www.Learnengineering.in
For More Visit : www.Learnengineering.in
www.edubuzz360.com
https://play.google.com/store/apps/details?id=com.sss.edubuzz360

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.