# 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
• 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 )

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)

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)

#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)

``````set created : {1, 2, 3, 4}