INTRODUCTION TO ALGORITHMS AND DATA STRUCTURES
Leah Ndirangu
Posted on February 21, 2022
Algorithms
An algorithm is a set of instructions used to solve mathematical problems or steps used to accomplish a task in a computer program.
When working with algorithms, it is important to identify the resources used, such as the memory, the operations to perform, the amount of data to work on.
Data structures
These are methods of organizing and storing data that perform efficient operations. To measure the efficiency of an algorithm, the Big O-Notation is used.
Big -O-Notation O(n)
This is a mathematical notation that describes the limiting behaviour of a function when the argument tends towards a particular value or infinity. It is about finding the an asymptotic upper bound.
It classifies how a function behaves as input grows or gets larger.
Python has 4 built-in data types used to store collections of data. These data structures are Lists, Tuples Sets, and Dictionaries. These data structures are discussed in this article.
Lists
Lists in Python are used to store multiple elements.
A list is made of comma separated characters or values enclosed in square brackets as shown
letters=['a', 'b','c', 'd']
numbers=[1,2,3,4]
Elements in a list correspond to an index. The first element in a list corresponds to index 0. For example in numbers, 1 corresponds to index 0 and in letters 'a' correspond to index 0.
letters=['a', 'b','c', 'd']
print(letters[0])
numbers=[1,2,3,4]
print(numbers[0])
To determine the length of a list, the len()
function is used.
List Functions
Append
This function adds an item at the end of a list.
append(item)
letters.append('z')
print(letters)
Insert
This function adds an item at a given index in the list.
insert(index, item)
numbers.insert(2, "python")
Count
This function returns a count of how many times an item occurs in the list.
print(numbers.count(3))
Pop
This function removes item at a given index)
numbers=[1,2,3,3,3,4,5,6]
print(numbers.pop(5))
Clear
This function removes all elements in a list.
numbers=[1,2,3,3,3,4,5,6]
print(numbers.clear())
Tuples
Tuples are used to store multiple items in a single variable.
A tuple is a collection of ordered and immutable (unchangeable) elements.
They are written using parentheses.
verterbrates==("mammals", "reptiles", "birds", "fish", "amphibians")
print(verterbrates)
To determine the length of a tuple, len
function is used.
verterbrates==("mammals", "reptiles", "birds", "fish", "amphibians")
print(len(verterbrates))
Dictionaries
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection of ordered, mutable items that does not allow data duplicates.
profile={name:"Jane", age:12, password:1234}
print(profile["Jane"])
Sets
Sets are collection of unordered items that are unique. They are created using curly braces. The in keyword makes iu fast to check whether an item is part of a set rather than in a list.
verterbrates={"mammals", "reptiles", "birds", "fish", "amphibians"}
if "insects" in verterbrates:
print(verterbrates)
else:
print("inverterbrates")
Sets can be combined using mathematical operations such as:
Union: joins elements of two different sets to form one new set containg elements in either.
Insertion: This operator gets elements found in both sets.
Difference: Gets items in the first set but not in the second.
Symmetric difference: gets items in either sets but not both.
Queues
This is a data structure that performs First in First Out linear operations. It is usually open at both ends.
Linked List.
These are containers where nodes of data are linked together into a list. It resembles a chain in that data is transferred from beginning to end without skipping a node.
Connecting nodes into a list can extend indefinitely and limit memory.
It is divided into singly and doubly linked lists.
A singly linked list is unidirectional and is only traversed from head to tail.
A doubly linked list is similar to a singly linked list with reverse iterations.
To find an item in a linked list, find
and contain
functions are used.
Posted on February 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 30, 2024