Python_Unit 1 || Computational Thinking and Problem Solving

Admin
By -
6 minute read
0

Problem Solving:



     Problem solving 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 a Correct program.



Problem solving techniques:

     Problem solving technique is a set of techniques that helps in providing logic for solving a problem.

Problem Solving Techniques:

     Problem solving can be expressed in the form of,

1. Algorithms.

2. Flowcharts.

3. Pseudo codes.

4. programs


Algorithm:

        It is defined as a sequence of instructions that describe a method for solving a problem. In other words it is a step by step procedure for solving a problem.

Properties of Algorithms;

  • 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 often used to judge the quality of the algorithms.

    Time To execute a program, the computer system takes some amount of time. The lesser is the time required, the better is the algorithm.

    Memory To execute a program, computer system takes some amount of memory space. The lesser is the memory required, the better is the algorithm.

    Accuracy Multiple algorithms may provide suitable or correct solutions to a given problem, some of these may provide more accurate results than others, and such algorithms may be suitable.


Example:

    Write an algorithm to print „Good Morning”

Step 1: Start

Step 2: Print “Good Morning”

Step 3: Stop



BUILDING BLOCKS OF ALGORITHMS (statements, state, control flow, functions)

        Algorithms can be constructed from basic building blocks namely, sequence, selection and iteration.

Statements:

 Statement is a single action in a computer.

 In a computer statements might include some of the following actions

  •  input data-information given to the program
  • process data-perform operation on a given input
  • output data-processed result

 State:

Transition from one process to another process under specified condition with in a time is called state.

 Control flow:

The process of executing the individual statements in a given order is called control flow.

The control can be executed in three ways

    1. sequence

    2. selection

    3. iteration

 Sequence:

All the instructions are executed one after another is called sequence execution.

 Example:

Add two numbers:

Step 1: Start

Step 2: get a,b

Step 3: calculate c=a+b

Step 4: Display c

Step 5: Stop

 

Selection:

A selection statement causes the program control to be transferred to a specific part of the program based upon the condition.

If the conditional test is true, one part of the program will be executed, otherwise it will execute the other part of the program.

Example

Write an algorithm to check whether he is eligible to vote?

Step 1: Start

Step 2: Get age

Step 3: if age >= 18 print “Eligible to vote”

Step 4: else print “Not eligible to vote”

Step 6: Stop

 

Iteration:

In some programs, certain set of statements are executed again and again based upon conditional test. i.e. executed more than one time. This type of execution is called looping or iteration.

 Example

 Write an algorithm to print all natural numbers up to n

Step 1: Start

Step 2: get n value.

Step 3: initialize i=1

Step 4: if (i<=n) go to step 5 else go to step 7

Step 5: Print i value and increment i value by 1

Step 6: go to step 4

Step 7: Stop

 

Functions:

  1.     Function is a sub program which consists of block of code(set of instructions) that performs a particular task.
  2.      For complex problems, the problem is been divided into smaller and simpler tasks during algorithm design.

 

Benefits of Using Functions

  •     Reduction in line of code
  •    code reuse
  •    Better readability
  •    Information hiding
  •    Easy to debug and test
  •    Improved maintainability

 Example:

     Algorithm for addition of two numbers using function

Main function()

Step 1: Start

Step 2: Call the function add()

Step 3: Stop

 

sub function add()

Step 1: Function start

Step 2: Get a, b Values

Step 3: add c=a+b

Step 4: Print c

Step 5: Return





Psuedo code

    Pseudocode is a detailed yet informal high-level description of a computer program or algorithm. It's designed to represent the program's logic in a language-agnostic manner, making it easier to understand for humans before actual coding in a specific programming language.

    While Python itself is a high-level programming language, when people talk about pseudocode in Python, they often refer to a style of writing code that is more human-readable and resembles Python syntax without strictly adhering to the language's rules.


# Example of Pseudocode in Python-style

# Calculate the sum of two numbers
function sum_of_numbers(num1, num2):
    sum = num1 + num2
    return sum

# Ask for user input
input_num1 = get_user_input("Enter first number: ")
input_num2 = get_user_input("Enter second number: ")

# Convert input to numbers
number1 = convert_to_number(input_num1)
number2 = convert_to_number(input_num2)

# Calculate the sum
result = sum_of_numbers(number1, number2)

# Display the result
display_result(result)
        This example demonstrates a Python-like syntax, using function definitions, comments, and variable assignments to convey the logic of a program without adhering strictly to Python's syntax rules. The purpose of pseudocode is to outline the structure and flow of the program in a more understandable format before implementing it in a specific programming language. 



Flowchart

    Flowcharts are graphical representations of a process or algorithm, displaying the steps in a sequence. Although Python itself does not have built-in tools to create flowcharts, you can design them to plan out Python programs or algorithms using various software or even by hand. The rules for constructing flowcharts are relatively universal, regardless of the programming language being used.

Here are some basic rules for constructing flowcharts:

  1. Start/End Symbol: Each flowchart must have a start and an end point. Typically, these are represented by an oval shape.

  2. Input/Output Symbol: For getting input from users or displaying output, rectangles are often used in a flowchart.

  3. Processing Steps: Use rectangles or other shapes to represent processing steps. These might involve calculations, comparisons, or any operation.

  4. Decision Making: Use a diamond-shaped symbol to represent a decision point, typically involving a yes/no or true/false question.

  5. Connectors/Arrows: Arrows show the flow of the process, connecting symbols to indicate the sequence of steps.


To create a flowchart for a Python program:

  1. Plan the Logic: Before you start coding, plan the logic and steps your program needs to take.
  2. Identify Symbols: Determine the symbols you'll use for start, end, input, output, processing, and decision-making steps.
  3. Draw the Flowchart: Use software tools like Lucidchart, Microsoft Visio, or even pen and paper to create the flowchart following the identified symbols and rules.
  4. Label the Symbols: Clearly label each symbol with the corresponding action or step.

This visual representation helps in understanding and organizing the logical flow of the program before implementing it in Python. It's an effective way to plan and communicate complex algorithms or processes.



Program to find minimum number in a List





Program to add card in a list and Sort it..,








Python program to "Guess an integer number in range"










Program for Tower of Hanoi








Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!