×

Problem-solving with algorithm and data structures using Python

What is problem-solving?

There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise problem-solving as a means of reaching one's objectives more quickly and intelligently.

The steps involved in problem-solving are

  1. Identify problem statement
  2. Gather information and design the solution
  3. Set of solutions
  4. Select the optimal solution
  5. Evaluate the solution and the approach

Algorithm

An algorithm is a set of clear instructions for solving a specific problem in computer programming. It generates the required result from a set of inputs.

Data structures

Data structure is a storage that is used to store the data elements. By using these data-structures, we can manipulate the data and access the data as well.

A data structure is used for more than just data organisation. Additionally, it is employed for data processing, retrieval, and archiving. Almost all software systems and applications have been built using many basic and advanced data structures. Therefore, we need to be well-versed in data structures.

Data structures are of two types:

  1. Linear Data structures
    • Array
    • Linked List
    • Stack
    • Queue
    • Matrix
  2. Non – Linear Data structures
    • Trees
    • Heap
    • Hash table
    • Graph

Data structures in Python

Several data structures come built-in with Python, which makes problem-solving using algorithms and data structures easier using Python.

These built-in data structures provide great flex in storing the data and speed up the retrieval and processing of data.

The data structures present in Python are:

  • List
  • Tuple
  • Dictionary
  • Set

These data structures are the key reason for using Python for problem-solving. These data structures have many in-built functions, which make them more efficient.

List: Like arrays in other programming languages, lists in Python are collections of data sorted in some way. It permits many element kinds in the List.

Python's version of lists is comparable to C++'s vectors or Java's array lists. As all the components must be relocated, adding or removing a member from the List's beginning is the most expensive operation. If the pre-allocated memory is all used up, insertion and deletion at the end of the List may also become expensive.

Program for list in python

list1 = []
list1.append(1)
list1.append(2)
list1.append(3)


print(list1)
print(“first element in the list :”, list1[0] )


list1.remove(1)
print(list1)

Output

[1, 2, 3]
The first element in the List: 1
[2, 3]

Tuple: A Python Tuple is a group of items that are separated by commas. The indexing, nested objects, and repetition of a tuple are somewhat similar to those of a list, however, unlike a list, a tuple is immutable.

To create a tuple, all the components (items) must be enclosed in parenthesis (), each separated by a comma. We can even create a tuple without using the parenthesis but using parenthesis is a good practice for writing the code.

Program for tuple in python

tuple1 = (10, 20, 30)


print(“ tuple : “, tuple1)
print(“first element in tuple is : ”, tuple1[0])


tuple2 = (‘a’, ‘b’, ‘c’)
tuple3 = tuple2 + tuple1
print(“combined tuple of 1 and 2 is :”, tuple3)

Output

Tuple : (10, 20, 30)
the first element in the tuple is: 10
combined tuple of 1 and 2 is : (10, 20, 30, ‘a’, ‘b’, ‘c’)

Dictionary: Unlike other data types, which can only retain a single value as an element, a dictionary in Python is a collection of keys and values used to store data like a map.

Python dictionary is used to store key-value Paris.

Dictionary is represented by {} all the key-value pairs are stored inside the {}.

Program for dictionary in python

dict = {0: ‘a’, 1: ‘b’, 2: ‘c’, 3: ‘d’}
print(“dictionary created is :”, dict)


#Acessing elements using key values
print(“value of key 1 is :”, dict[1])


#Acessing elements using get method
print(“value of key 2 is :”, dict.get(2))

Output

dictionary created is : {0: 'a', 1: 'b', 2: 'c', 3: 'd'}
value of key 1 is: b
value of key 2 is: c

Sets: A Set is an iterable, changeable, and duplicate-free unordered collection data type. Python set cannot contain duplicate items. Set is a data structure based on another data structure, a hash Table. Since a set is unordered, it is impossible to access the elements by indexing as we do in List.

Python program for sets

set1 = set([1, 2, 3, 4])
#creating set using set() method
print(“set created :”, set1)


set1.add(5)
#adding elements to the set
print(“after adding an unique element : ”, set1)

Output

set created : {1, 2, 3, 4}
after adding an unique element :  {1, 2, 3, 4, 5}

Related Topics

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Implementing geometric shapes into the game in python

Geometric Drawings We are trying to draw various shapes of geometry by using pygame module to implement Geometric Drawings in game. Let us revise few syntax of basic shapes, to get started...

7 minutes read.

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Gethostbyname() function in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.