×

Problem Solving in Python

Problem-solving is the process of identifying a problem, creating an algorithm to solve the given problem, and finally implementing the algorithm to develop a computer program.

An algorithm is a process or set of rules to be followed while performing calculations or other problem-solving operations. It is simply a set of steps to accomplish a certain task.

In this article, we will discuss 5 major steps for efficient problem-solving. These steps are:

  • Understanding the Problem
  • Exploring Examples
  • Breaking the Problem Down
  • Solving or Simplification
  • Looking back and Refactoring

Step 1 – Understanding the Problem

While understanding the problem, we first need to closely examine the language of the question and then proceed further. The following questions can be helpful while understanding the given problem at hand.

  1. Can the problem be restated in our own words?
  2. What are the inputs that are needed for the problem?
  3. What are the outputs that come from the problem?
  4. Can the outputs be determined from the inputs? In other words, do we have enough information to solve the given problem?
  5. What should the important pieces of data be labeled?

Example : Write a function that takes two numbers and returns their sum.

  • Can the problem be restated in our own words?
    • Implement addition
  • What are the inputs that are needed for the problem?
    • Integer, Float, etc.
  • What are the outputs that come from the problem?
    • Integer, Float, etc.
  • Can the outputs be determined from the inputs? In other words, do we have enough information to solve the given problem?
    • Yes
  • What should the important pieces of data be labeled?
    • Add, Sum

Step 2 – Explore Examples

Once we have understood the given problem, we can look up various examples related to it. The examples should cover all situations that can be encountered while the implementation.

  • Start with simple examples.
  • Progress to more complex examples.
  • Explore examples with empty inputs.
  • Explore examples with invalid inputs.

Example : Write a function that takes a string as input and returns the count of each character

# Start with simple examples
charCount("bbbb")
# {b: 4}
charCount("hello")
# {h: 1, e: 1, l: 2, o: 1}

# Progress to more complex examples
charCount("My name is Rachel")

# Explore examples with empty inputs
charCount("")

# Explore examples with invalid inputs
charCount(10)

Step 3 – Breaking the Problem Down

After exploring examples related to the problem, we need to break down the given problem. Before implementation, we write out the steps that need to be taken to solve the question.

Example : Write a function that takes a string as input and returns the count of each character

def charCount(tempString):
    # Declare an object to return at the end
    # Loop over the string
        # If the char is a letter and it is in our object, add one to the value
        # If the char is a letter and it is not in our object, 
        # add that char to our object with the value of one
    # Return the object

Step 4 – Solving or Simplification

Once we have laid out the steps to solve the problem, we try to find the solution to the question. If the solution cannot be found, try to simplify the problem instead.

The steps to simplify a problem are as follows:

  • Find the core difficulty
  • Temporarily ignore the difficulty
  • Write a simplified solution
  • Then incorporate that difficulty

Example : Write a function that takes a string as input and returns the count of each character

def charCount(tempString):
    # Declare an object to return at the end
    result = {}
    # Loop over the string
    for i in tempString:
        # If the char is a letter and it is in our object, add one to the value
        if i in result:
            result[i] += 1
        # If the char is a letter and it is not in our object, 
        # add that char to our object with the value of one
        else:
            result[i] = 1
    # Return the object
    return result

print(charCount("hello"))

#Output
{'h': 1, 'e': 1, 'l': 2, 'o': 1}

Step 5 – Looking back and Refactoring

Since we have completed the implementation of the problem, we now look back at the code and refactor it if required. It is an important step to refactor the code so as to improve efficiency.

The following questions can be helpful while looking back at the code and refactoring:

  • Can we check the result?
  • Can we derive the result differently?
  • Can we understand it at a glance?
  • Can we use the result or mehtod for some other problem?
  • Can you improve the performance of the solution?
  • How do other people solve the problem?

Example : Write a function that takes a string as input and returns the count of each character

def charCount(tempString):
    # Declare an object to return at the end
    result = {}
    # Loop over the string
    for i in tempString.lower():
        # If the char is a lowercase letter and it is in our object, add one to the value
        if isinstance(i, str) and not(i.isspace()):
            if i in result:
                result[i] += 1
        # If the char is a lowercase letter and it is not in our object, 
        # add that char to our object with the value of one
        else:
            result[i] = 1
    # Return the object
    return result

print(charCount("hello"))

#Output
{'h': 1, 'e': 1, 'l': 2, 'o': 1}