COS_102_note[1]-1
COS_102_note[1]-1
Examples of Algorithm
Sorting algorithms (bubble sort, merge sort, quicksort)
Searching algorithms (linear search, binary search)
Graph algorithms (shortest path, minimum spanning tree)
Numerical algorithms (finding roots of equations, numerical integration)
Applications of Algorithm
Algorithms are essential in various fields of computing, including software development, data
processing, artificial intelligence, and scientific computing.
Heuristics: this involves a rule of thumb, shortcut, or educated guess that helps find a solution to a
problem, but does not guarantee an optimal or correct solution.
Properties/Characteristics of Heuristics:
Approximate: Provides a good enough solution in a reasonable amount of time.
Flexible: Can be adapted to different problem variations.
Intuitive: Often based on experience, intuition, or common sense.
Examples of Heuristics
Greedy algorithms: Make locally optimal choices at each step, hoping for a global optimum.
Hill climbing: Iteratively improve a solution by making small changes.
Simulated annealing: Allow occasional "bad" moves to escape local optima.
Genetic algorithms: Mimic natural selection to evolve solutions.
Applications of Heuristics
Heuristics are used when finding an optimal solution is computationally expensive or infeasible, or
when the problem is ill-defined or complex.
Always produce a correct or optimal May or may not produce the best
Guarantee
solution (if one exists) solution
Unsolvable Problems
Unsolvable Problems are problems that have no solution or have solutions that are impossible to
reach due to inherent limitations, contradictions, or lack of resources.
Characteristics Unsolvable Problems
Ill-defined or paradoxical: The problem statement is vague, contradictory, or logically
impossible.
Insufficient or contradictory information: The available information is incomplete,
unreliable, or conflicts with itself.
Lack of resources or expertise: The necessary tools, knowledge, or skills are unavailable or
insufficient.
Inherent constraints: The problem is limited by physical laws, ethical principles, or social
norms that cannot be overcome.
Examples of Unsolvable Problems include:
Mathematical paradoxes: The "halting problem" in computer science (determining whether a
program will halt or run forever).
Philosophical questions: The meaning of life, the a existence of free will.
Social issues: Eliminating poverty, achieving world peace.
Scientific limitations: Traveling faster than the speed of light, reversing the arrow of time.
Strategies for Dealing with Unsolvable Problems
i. Reframe the problem: Try to redefine the problem in a way that makes it solvable or
focus on more manageable aspect of the problem.
ii. Set realistic goals: Instead of seeking a complete solution, aim for partial solutions or
incremental improvements.
iii. Manage the consequences: Develop strategies for coping with the negative effects of the
unsolvable problem and mitigating its impact.
iv. Accept the limitations: Recognize that some problems are beyond our current capabilities
and learn to live with uncertainty or ambiguity.
Problem-Solving Techniques
1. Abstraction: Simplifying a complex problem by focusing on its essential features and ignoring
irrelevant details. Example: Creating a diagram or model to represent a system.
2. Analogy: Finding similarities between a current problem and a previously solved problem, and
applying the same solution strategy. Example: Using the analogy of a virus to explain the spread of
computer malware.
3. Brainstorming: Generating a large number of potential solutions without judgment, then
evaluating and refining them later. Example: Using a mind map to brainstorm ideas for a
marketing campaign.
4. Trial and Error: Experimenting with different solutions until one works. Example: Trying
different combinations of ingredients to create a new recipe.
5. Hypothesis Testing: Formulating a hypothesis about the cause of a problem and then testing it
through experimentation or observation. Example: Conducting a survey to test a hypothesis about
consumer preferences.
6. Reduction: Breaking down a complex problem into smaller, more manageable sub-problems.
Example: Dividing a large research project into smaller tasks.
7. Lateral Thinking: Approaching a problem from a different, unexpected perspective to generate
creative solutions. Example: Using wordplay or riddles to solve a puzzle.
8. Means-End Analysis: Identifying the goal and working backward to determine the steps needed
to achieve it. Example: Planning a road trip by first deciding on the destination and then mapping
out the route.
9. Method of Focal Objects: Combining characteristics of different objects to create a new,
innovative solution. Example: Inventing a new product by combining features of existing products.
10. Morphological Analysis: Systematically exploring all possible combinations of parameters to
identify potential solutions. Example: Designing a new type of vehicle by considering all possible
combinations of engine, body style, and fuel type.
11. Research: Gathering information and knowledge relevant to the problem to inform the solution
process. Example: Conducting a literature review before starting a research project.
12. Root Cause Analysis: Identifying the underlying cause of a problem, rather than just addressing
its symptoms. Example: Investigating why a machine keeps breaking down, rather than just
repairing it each time.
13. Proof: Using logical reasoning and evidence to demonstrate the validity of a solution. Example:
Proving a mathematical theorem using axioms and previously proven theorems.
14. Divide and Conquer: Dividing a problem into smaller sub-problems, solving each sub-problem
independently, and then combining the solutions. Example: Sorting a large list of numbers by first
dividing it into smaller lists and then sorting each list individually.
4. Decision Trees: These are tree-like diagram showing decisions and their possible consequences.
Advantages Decision Trees
Visual representation of hierarchical decision-making
Helpful for visualizing probabilities and outcomes.
Disadvantages Decision Trees
Can become unwieldy for complex problems
May not capture all nuances of a situation.
Implementation, Evaluation, and Refinement, Specifically Focusing on Programming in C
and Python,
Implementation: It is the process of turning a program design into actual code.
Implementation
Translating Algorithms to Code:
Steps for Translating an Algorithm into Program (Code)
Translating algorithm into a program involves five main steps:
understanding the algorithm
choosing a programming language suitable for the task,
coding the algorithm in that language,
testing to make sure it works properly and
debugging to fix any issues. It is about knowing the problem, choosing the right tool and
refining until you get the desired result.
Understanding the Algorithm: The first step in converting an algorithm into a program is to
thoroughly understand the algorithm. This involves analyzing the problem the algorithm is
designed to solve, understanding the logic behind the algorithm, and identifying the inputs and
outputs. It is crucial to have a clear understanding of the algorithm's flow and the steps it takes to
solve the problem. This understanding will guide the programming process and help in choosing
the right programming language.
Translating the algorithm into code: Once the programming language has been chosen, the next
step is to translate the algorithm into code. This involves writing the instructions of the algorithm
in the syntax of the chosen programming language. It is important to follow good coding practices,
such as using meaningful variable names and adding comments to explain the code. This will
make the code easier to understand and maintain.
Testing: After the code has been written, it needs to be tested to ensure it works as expected. This
involves running the program with different inputs and checking the outputs. If the program does
not produce the expected results, the code needs to be debugged.
Debugging: This involves identifying and fixing errors in the code. This can be a time-consuming
process, but it is essential to ensure the program works correctly.
Evaluation: This is the process of testing and debugging code to ensure it works correctly.
Types of Testing:
o Unit Testing: Testing individual components or functions in isolation.
o Integration Testing: Testing how different components work together.
o System Testing: Testing the entire program as a whole.
Debugging Techniques:
o Print Statements/Logging: Inserting print statements (C) or using the logging module (Python) to
track variable values and program flow.
o Debuggers: Introduce integrated development environments (IDEs) with debugging tools (e.g.,
breakpoints, step-through execution, variable inspection).
o Code Review: Having another person review your code to identify errors or potential issues.
Refinement (Optimization and Improvement): This is the process of optimizing and improving
code for better performance(efficiency), readability, and maintainability.
Code Readability:
o C: to ensure readability in C programming, the following measures are taken:
i. Consistent indentation,
ii. meaningful variable names,
iii. Comments to explain complex logic.
o Python: the measures taken to ensure code readability in Python, PEP 8 style guide for clean and
readable code is followed.
Code Maintainability: this is achieved through :the following:
i. Modular design,
ii. clear documentation,
iii. well-structured code to make it easier to modify or fix in the future.
Code Efficiency:
o C: in C programming, the following helps in maintaining code efficiency:
i. Algorithm optimization
ii. efficient data structures,
iii. minimizing unnecessary computations
o Python: to maintain code efficiency in Python, the following measures are taken:
i. Use built-in functions and libraries optimized for performance
ii. Avoid inefficient loops.
Profiling Tools: Introduce profiling tools that help identify performance bottlenecks in the code.
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some
other programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
Versions of Python
The most recent major version of Python is Python 3. However, Python 2, although not
being updated with anything other than security updates, is still quite popular.
Python can be written in a text editor. It is possible to write Python in an Integrated
Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which are
particularly useful when managing larger collections of Python files.
Example
print("Hello, World!")
Python Indentation
Indentation refers to the spaces at the beginning of a code line. In other programming languages,
the indentation in code is for readability only, the indentation in Python is very important. Python
uses indentation to indicate a block of code.
Example
if 5 > 2:
print("Five is greater than two!")
The number of spaces is up to you as a programmer, the most common use is four, but it has to be
at least one.
Example
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
You have to use the same number of spaces in the same block of code, otherwise Python will give
you an error:
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Variables
In Python, variables are created when you assign a value to it:
Example
Variables in Python:
x=5
y = "Hello, World!"
Predefined Modules: These are also referred to as built-in modules. Python has a huge number of
built-in modules. Programmers can utilize these modules in Python applications by invoking their
names along with the keyword 'import'. example Import math. The majority of the built-in Python
modules are written in C and then combined with the Python interpreter to allow the interpreter to
work alongside them. Examples: Math, datetime, statistics, random, os, sys, and other popular
built-in Python modules.
Math: Gives users access to mathematical constants and pi and trigonometric functions.
Datetime: Provides classes for a simpler way of manipulating dates, times, and periods.
OS: Enables interaction with the base operating system, including administration of processes and
file system activities.
Random: The random function offers tools for generating random integers and picking random
items from a list.
JSON: JSON is a data structure that can be encoded and decoded and is frequently used in online
APIs and data exchange. This module allows dealing with JSON.
Re: Supports regular expressions, a potent text-search and text-manipulation tool.
Collections: Provides alternative data structures such as sorted dictionaries, default dictionaries,
and named tuples.
NumPy: NumPy is a core toolkit for scientific computing that supports numerical operations on
arrays and matrices.
Pandas: It provides high-level data structures and operations for dealing with time series and other
structured data types.
Requests: Offers a simple user interface for web APIs and performs HTTP requests.
User-defined Modules: Another advantage that Python gives its programmers is the ability to
create user-defined modules. Python enables programmers to customize their own operations and
gain control over them. Programmers can design their own collection of functions, variables, and
classes within a user-defined module. To create a module just save the code you want in a file with
the file extension .py:
External Modules: External modules must be downloaded from outside. They don't exist yet like
the built-in ones. Installing them is a very simple task and can be done using the "pip install
module_name" command in the compiler terminal. With so many modules available, familiarity
with all seems to go a long way even for the best programmers. So you can search for modules to
find and use them as needed.
Libraries
A library is an umbrella term that comprises a reusable set of Python code/instructions.
A Python library is typically a collection of similar modules grouped together under a single name.
Developers commonly utilize it to share reusable code with the community. This eliminates the
need to write Python code from scratch.
Developers and community researchers can construct their own set of useful functions in the same
domain. When programmers and developers install the Python interpreter on their machines,
standard libraries are included. Python libraries include matplotlib, Pygame, Pytorch, Requests,
Beautifulsoup, and others.
o Web development (Server-side) - Django Flask, Pyramid, CherryPy
o GUIs based applications - Tkinter, PyGTK, PyQt, PyJs, etc.
o Machine Learning - TensorFlow, PyTorch, Scikit-learn, Matplotlib, Scipy, etc.
o Mathematics - NumPy, Pandas, etc.
o BeautifulSoup: a library for web scraping and parsing HTML and XML
o Requests: a library for making HTTP requests
o SQLAlchemy: a library for working with SQL databases
o Kivy: a framework for building multi-touch applications
o Pygame: a library for game development
o Pytest: a testing framework for Python Django
o REST framework: a toolkit for building RESTful APIs
o FastAPI: a modern, fast web framework for building APIs
o Streamlit: a library for building interactive web apps for machine learning and data
science
o NLTK: a library for natural language processing
Modules Libraries
They are used by both programmers and Libraries are used by community members,
developers. developers and researchers.
The use of modules makes it easier to read Libraries provide no contribution for easy
the code. readability.
When a Python programmer imports a Before we can use the libraries' modules or
module, the interpreter searches various packages, we must first install them in our
locations for the module's definition or Python project. We generally use the pip install
body. command.
Modules are generally written in Python Libraries, especially standard libraries, are
with valid statements or codes. usually developed in C or Python.
The basic goal of creating a module is to Libraries do not have such a goal.
prevent DRY i.e, Don't Repeat Yourself.
To return a sorted list of strings containing There is no explicit function that returns the
the function names specified within a number of modules in a library. Even so,
module, we can use Python's built-in dir() programmers can utilize the help() function to
function. get some information.
Popular built-in Python modules include Popular built-in Python libraries include
os, sys, math, random, and so on. Pygame, Pytorch, matplotlib, and more.
Output
Python is powerful
Here, the print() function displays the string enclosed inside the single quotation.
[
Syntax of print()
In the above code, the print() function is taking a single parameter. However, the actual syntax of
the print() function accepts 5 parameters as follows:
Print (object=separator=end=file=flush=)
Here,
object - value(s) to be printed
sep (optional) - allows us to separate multiple objects inside print().
end (optional) - allows us to add add specific values like new line "\n", tab "\t"
file (optional) - where the values are printed. It's default value is sys.stdout (screen)
flush (optional) - boolean specifying if the output is flushed or buffered. Default: False
Example 1: Python Print Statement
print (‘Python Programming’)
print (‘is very interesting’)
Output
Python Programming
is very interesting
In the above example, the print() statement only includes the object to be printed. Here, the value
for end is not used. Hence, it takes the default value '\n'.
So we get the output in two different lines.
Output
Python programming is very interesting
Notice that we have included the end= ' ' after the end of the first print() statement.
Hence, we get the output in a single line separated by space.
In the above example, the print() statement includes multiple items separated by a comma.
Notice that we have used the optional parameter sep= ". " inside the print() statement.
Hence, the output includes items separated by . not comma.
Python Input: this is the process of entering data from the keyboard.
While programming, we might want to take the input from the user. In Python, we can use
the input() function.
Syntax of input()
input(prompt)
Output
Enter a number: 10
You Entred: 10
data type of num:<class ‘str’>
In the above example, we have used the input() function to take input from the user and stored the
user input in the num variable .
It is important to note that the entered value 10 is a string, not a number.
So, type(num) returns <class 'str'>.
To convert user input into a number we can use int() or float() functions as:
Num =int(input(‘Enter a number:’))
Here, the data type of the user input is converted from string to integer .
Solution
Python Variables
Variables are simply containers for storing data values. They are memory locations whose contents
can change during program execution. A variable can be seen as a placeholder for information you
want Python to recall later in the coding process when you need to complete an action. Technically,
a variable acts as an address where data is stored in memory. A python variable may be assigned a
value of one type and then later re-assigned a value of a different type. Exampe: X= “orange” can
later be X= 2.
Creating Variables
Unlike other languages like Java, Python has no command for declaring a variable. A variable is
created the moment you first assign a value to it.
Example
x=5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they
have been set.
Example
x=4 # x is of type int
x = "Sally" # x is now of type str
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Output Variables
The Python print() function is often used to output variables.
Example
x = "Python is awesome"
print(x)
In the print() function, you output multiple variables, separated by a comma:
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Global Variables:Variables that are created outside of a function (as in all of the examples above)
are known as global variables. Global variables can be used by everyone, both inside of functions
and outside.
Example
Create a variable outside of a function, and use it inside the function
x =5
Y=10
def sum():
Sum=x+y
return sum
print(sum())
Output
15
Local variables
Python local variables are defined inside a function. We cannot access the variable outside the
function.
Example
Def sum (x,y):
sum=x+y
return sum
Print(sum(5,10))
Output
15
Python Constants
Python does not have any formally defined constants, however you can indicate a variable to be
treated as constant by using all-caps names with underscores. For example, the name PI_VALUE
indicates that you don’t want the variable redefined or changed in any way. The naming
convention using all-caps is sometimes referred to as screaming snake case- where the all-caps
(screaming) and the underscores (snakes).
Lists: List in Python are like arrays in C, but lists can contain data of different types. The items of
the list are separated with comma (,) and encased inside square bracket []. Example: [2024,
“python”, 3.22, 5+6j]
Tuple: a tuple is like a list. it also contains a collection of items from various data types. Unlike
lists, tuples are enclosed within parenthesis ().
Dictionary: A dictionary is a key-value pair set arranged in any order. It stores a specific value for
each key, like an associative array or a hash table. Value is any Python object, while the key can
hold any primitive data type. The comma (,) and the curly braces are used to separate the items in
the dictionary. Example: {1:’one’, 2: ‘two’ 3: ‘three’}
Boolean: True and False are the two default values for the Boolean type. Python bool() function
allows you to evaluate the value of any expression and returns either True or False based on the
expression. False can be represented by the 0 or the letter "F," while true can be represented by any
value that is not zero.
Set: this is a collection but not an indexed or ordered collection as string, list or tuple. An object
cannot appear more than once in a set unlike list and tuple where an object cannot appear more
than once. The items of the set is enclosed within curly braces and separated by a comma or the
built-in function set() is used to create the set. It can contain different kinds of values.
Example:{2023, 3.11, 5+7j, }
Python Operators
Operators are special symbols that perform operations on variables and values. For example,
Print(6+5)
Here, + is an operator that adds two numbers: 6 and 5.
Output: 11
Types of Python Operators
Different types of operators in Python include:
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators
+ Addition 5+2=7
- Subtraction 4-2=2
* Multiplication 2*3=6
// Floor Division 10 // 3 = 3
% Modulo 5%2=1
** Power 4 ** 2 = 16
2. Python Assignment Operators: Assignment operators are used to assign values to variables.
Example
# assign 5 to x
X=5
Here, = is an assignment operator that assigns 5 to x.
The following are different assignment operators available in Python.
Assignment
= a=7
Operator
+= Addition a += 1 ( a = a + 1)
Assignment
Subtraction
-= a -= 3 ( a = a - 3)
Assignment
Multiplication
*= a *= 4 (a = a * 4)
Assignment
Division
/= a /= 3 (a = a / 3)
Assignment
Remainder
%= a %= 10 ( a = a % 10)
Assignment
Exponent
**= a **= 10 ( a = a ** 10)
Assignment
Greater Than or
>= 3 >= 5 give us False
Equal To
Less Than or
<= 3 <= 5 gives us True
Equal To
4. Python Logical Operators: Logical operators are used to check whether an expression
is True or False. They are used in decision-making.
Example
A=5
b=6
Print((a>2) and (b>=6))
Output: True
Here, and is the logical operator AND. Since both a > 2 and b >= 6 are True, the result is True.
Logical AND:
and a and b
True only if both the operands are True
Logical OR:
or a or b
True if at least one of the operands is True
Logical NOT:
not not a
True if the operand is False and vice-versa.
5. Python Bitwise operators: Bitwise operators act on operands as if they were strings of binary
digits. They operate bit by bit, hence the name.
Example, 2 is 10 in binary, and 7 is 111.
In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Bitwise right
>> x >> 2 = 2 (0000 0010)
shift
Bitwise left
<< x <<2= 40 (0010 1000)
shift
6. Python Special operators: Python language offers some special types of operators like
the identity operator and the membership operator. They are described below with examples.
Identity operators: In Python, is and is not are used to check if two values are located at the
same memory location. It is important to note that having two variables with equal values doesn't
necessarily mean they are identical.
Operator Meaning Example
Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical.
The same is the case with x2 and y2 (strings). But x3 and y3 are lists. They are equal but not
identical. It is because the interpreter locates them separately in memory, although they are equal.
Membership operators: In Python, in and not in are the membership operators. They are used to
test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary, we can only test for the presence of a key, not the value.
Operator Meaning Example
True if value/variable is
in 5 in x
found in the sequence
Output
True
True
True
False
Here, 'H' is in message, but 'hello' is not present in message (remember, Python is case-sensitive).
Similarly, 1 is key, and 'a' is the value in dictionary dict1. Hence, 'a' in y returns False.
Python Control Structures
A program’s control flow is the order in which the program’s code executes. The control flow of a
Python program is regulated by conditional statements, loops and function calls.
Python has three types of control structures:
Sequential - default mode
Selection - used for decisions and branching
Repetition - used for looping, i.e., repeating a piece of code multiple times.
1. Sequential: Sequential statements are a set of statements whose execution process happens in a
sequence. The problem with sequential statements is that if the logic has broken in any one of the
lines, then the complete source code execution will break.
If Statement: this allows a particular code to be executed when a certain condition is true or
satisfied. An if statement tests for only one condition.
Syntax
1. if <conditional expression> :
2. The code block to be executed if the condition is True
Flowchart
conditionn
True
False
Conditional code
Example
Score =75
If score>= 60:
Print (“congratulations! You passed.”)
Here, the code inside the ‘if’ block will be executed only if the ‘score’ is greater than60.
since the ‘score’ is 75, the message “congratulations! You passed will be displayed on the
screen
if-else Statement: The if-else statement evaluates the condition and will execute the body of ’if’
if the test condition is True, but if the condition is False, then the body of ‘else’ is executed.
Flowchart of if-else statement
False
If condition
True
False
If condition 1
else body
True
False
If condition 2
else body
True
if body
Example
Name= “Faith”
Score= 79
“If name== “Faith”:
If score>=80:
Print(“congratulations! You got A”.)
else:
Print(“good effort Faith, keep it up.”)
Else:
Print(“you are doing well but this message is for Faith.”)
if-elif-else Statement: The if-elif-else statement is used when you have multiple conditions to
check. It allows you to test multiple conditions and execute the corresponding block of code for
the first true condition encountered. If the condition in the ‘if’ and all ‘elif’ statements evaluate to
false, the ‘else’ block is executed.
Flowchart of If-elif-else
true
Condition 1 Statement 1
false
true
Condition 2 Statement 2
false
true
Condition 3 Statement 3
false
Statement 4
Example
Score= 84
If score >= 90
Print(“excellent! You got an A.”)
Elif score >= 80:
Print(“nice one! You got a B.”)
Elif score>= 70:
Print(“you got C.”)
Else:
Print(“you need to improve. You got F.”)
In the example above, the prograram checks the ‘score’ against multiple conditions. The code
inside the first true condition’s block will be executed . since the ‘score’ is 84.
Output
nice one!you got a B.
[
3. Repetition
A repetition statement is used to repeat a group(block) of programming instructions.
In Python, we generally have two loops/repetitive statements:
for loop
while loop
for loop: A for loop is used to iterate over a sequence that is either a list, tuple, dictionary, or a set.
We can execute a set of statements once for each item in a list, tuple, or dictionary.
Example