Start learning free

LESSON 151 OF 410 · LOOPING OVER LIST INDICES

Get Full Names in Python (Time Complexity: O(n))

Practice Read the full explanation

Given two lists of strings, the first names and the last names (given in this order, called firstNames and lastNames in the example), return a list containing the full names (separated by one space)

Example:

Input:   firstNames = ["John","Andy","Mary"]
         lastNames = ["Doe","Smith","Johnson"]

Output:  ["John Doe", "Andy Smith", "Mary Johnson"]
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · LOOPING OVER LIST INDICES

    Get Full Names in Python, explained

    Understanding the Problem

    The core challenge of this problem is to combine two arrays of strings into a single array of full names. Each full name is formed by concatenating the corresponding elements from the firstNames and lastNames arrays, separated by a space.

    This problem is significant in many real-world applications, such as generating user profiles, creating contact lists, or formatting names for display purposes. A common pitfall is to assume that the arrays are always of the same length without validating this assumption.

    Approach

    To solve this problem, we can use a simple loop to iterate through the arrays and concatenate the corresponding elements. Here’s a step-by-step approach:

    1. Initialize an empty list to store the full names.
    2. Iterate through the indices of the firstNames array.
    3. For each index, concatenate the corresponding elements from firstNames and lastNames with a space in between.
    4. Append the concatenated string to the list of full names.
    5. Return the list of full names.

    This approach ensures that we process each element exactly once, making it efficient with a time complexity of O(n), where n is the number of elements in the arrays.

    Algorithm

    Here’s a detailed breakdown of the algorithm:

    1. Initialize an empty list fullNames.
    2. Loop through the range of the length of firstNames (assuming both arrays are of the same length).
    3. For each index i, concatenate firstNames[i] and lastNames[i] with a space in between.
    4. Append the concatenated string to fullNames.
    5. Return fullNames.

    Code Implementation

    def get_full_names(firstNames, lastNames):
        # Initialize an empty list to store full names
        full_names = []
        
        # Iterate through the indices of the firstNames array
        for i in range(len(firstNames)):
            # Concatenate the corresponding elements from firstNames and lastNames
            full_name = firstNames[i] + " " + lastNames[i]
            # Append the full name to the list
            full_names.append(full_name)
        
        # Return the list of full names
        return full_names
    
    # Example usage
    firstNames = ["John", "Andy", "Mary"]
    lastNames = ["Doe", "Smith", "Johnson"]
    print(get_full_names(firstNames, lastNames))  # Output: ["John Doe", "Andy Smith", "Mary Johnson"]
    

    Complexity Analysis

    The time complexity of this approach is O(n), where n is the number of elements in the firstNames array. This is because we iterate through each element exactly once. The space complexity is also O(n) due to the storage of the full names in a new list.

    Edge Cases

    Potential edge cases include:

    • Empty arrays: Both firstNames and lastNames are empty. The function should return an empty list.
    • Arrays of different lengths: This problem assumes that both arrays are of the same length. If they are not, the function should handle this gracefully, possibly by raising an error or returning a message indicating the mismatch.

    Example of handling empty arrays:

    print(get_full_names([], []))  # Output: []
    

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Basic test case with typical input.
    • Edge case with empty arrays.
    • Edge case with arrays of different lengths (if applicable).

    Example test cases:

    assert get_full_names(["John", "Andy", "Mary"], ["Doe", "Smith", "Johnson"]) == ["John Doe", "Andy Smith", "Mary Johnson"]
    assert get_full_names([], []) == []
    # Add more test cases as needed
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Break down the problem into smaller, manageable steps.
    • Think about edge cases and how to handle them.
    • Write clean, readable code with comments to explain your thought process.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed how to solve the problem of concatenating two arrays of strings to form full names. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free