What Is an Algorithm? #
An algorithm is a step-by-step set of instructions designed to perform a specific task or solve a particular problem. It is the foundation of all computer programs and is used to process data, make decisions, or automate repetitive tasks.
Daily Life Examples of Algorithm #
Making a Cup of Tea:
Step 1: Fill the kettle with water.
Step 2: Boil the water.
Step 3: Place a tea bag in a cup.
Step 4: Pour boiling water into the cup.
Step 5: Add sugar/milk if desired.
Step 6: Stir and serve.
Finding a Word in a Dictionary:
Step 1: Open the dictionary.
Step 2: Locate the section based on the first letter of the word.
Step 3: Narrow down based on the second and subsequent letters.
Step 4: Find the word and read its definition.
Crossing the Street:
Step 1: Look left.
Step 2: Look right.
Step 3: Look left again.
Step 4: If the road is clear, cross; otherwise, wait.
Key Features of an Algorithm: #
- Input: The algorithm accepts zero or more inputs to work with.
- Output: The algorithm produces one (or multiple) result or outcome (procedures produce no result).
- Definiteness: Each step must be clearly defined without ambiguity.
- Finiteness: The algorithm must terminate after a finite number of steps.
- Effectiveness: Each step of the algorithm can be performed in a finite amount of time using available resources.
KISS (Keep It Simple Stupid)
What Is Pseudocode? #
Pseudocode is a simplified, informal way of writing algorithms that uses plain language and basic programming constructs without worrying about syntax. It bridges the gap between human thinking and actual code. Example of Pseudocode:
Task: Find the largest number in a list.
Input: list of numbers
1. Initialize a variable Max to the first element of the list.
2. For each element in the list:
a. If the element is greater than Max:
i. Set Max to this element.
3. Output: Max.
Advantages of Using Pseudocode #
- Language-Independent: It’s not tied to a specific programming language.
- Focus on Logic: Allows the designer to focus on the algorithm’s structure without worrying about syntax.
- Easy to Understand: Non-programmers can follow and provide feedback.
MSI #
- Manually. Always solve the problem manually first. Ideally on paper, with concrete examples.
- Step-by-Step. Then design the algorithm. What are the steps that need to be taken? Generic (abstract) step-by-step solution.
- Impelement. Proceed to implement the algorithm in a programming language.
Example #
Codewars: Count of positives, sum of negatives:
Given an array of integers.
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative.
If the input is an empty array or is null, return an empty array.
Example: For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].
Homework (graded) #
Given a string. Return two numbers (tuple), the first will represent the number of spaces in the string, the other of non-spaces. Your task is pass the M. and S. steps on paper and only then to implement the algorithm in Python. Both the paper and the Python script are required.
Use for
loop to iterate over the string. You’re not allowed to use len()
function nor string.count()
method.