9  Convert to Code (Step 5)

AI is at its most useful when you already know what you want. That is not a coincidence.

The fifth step of our methodology, converting pseudocode to actual code, is where your solution takes executable form. This is often the step where AI assistance becomes most valuable, but it’s also where maintaining human understanding is most critical.

9.1 Strategies for Code Implementation

9.1.1 From Pseudocode to Code: A Systematic Approach

Converting pseudocode to code should be a methodical process:

  1. Start with structure - Convert the overall organisation and function definitions
  2. Fill in the logic - Implement the core algorithms and control flow
  3. Add error handling - Incorporate validation and exception handling
  4. optimise - Refine the implementation for efficiency and readability
  5. Document - Add appropriate comments and documentation

This incremental approach ensures that you maintain control of the implementation while still leveraging AI for efficiency.

9.1.2 Incremental Implementation

Rather than converting all pseudocode at once, implement in manageable chunks:

// Pseudocode
FUNCTION findLongestPalindrome(string)
    FUNCTION expandAroundCenter(left, right)
        // Expansion logic
    END FUNCTION
    
    // Main logic using expandAroundCenter
END FUNCTION

Implement one function at a time:

def find_longest_palindrome(s):
    # Implementation will go here
    pass

def expand_around_center(s, left, right):
    # First, implement this helper function
    while left >= 0 and right < len(s) and s[left] == s[right]:
        left -= 1
        right += 1
    
    # Return the palindrome bounds (start, length)
    return left + 1, right - left - 1

Then integrate the pieces:

def find_longest_palindrome(s):
    if not s:
        return ""
    
    start = 0
    max_length = 1
    
    for i in range(len(s)):
        # Now use the helper function for both odd and even cases
        odd_start, odd_length = expand_around_center(s, i, i)
        even_start, even_length = expand_around_center(s, i, i + 1)
        
        # Update if a longer palindrome is found
        if odd_length > max_length:
            start = odd_start
            max_length = odd_length
            
        if even_length > max_length:
            start = even_start
            max_length = even_length
    
    return s[start:start + max_length]

9.1.3 Language-Specific Considerations

Different programming languages offer various idioms and features that can improve your implementation:

9.1.3.1 Python

# Use list comprehensions for concise data transformation
lengths = [expand_around_center(s, i, i)[1] for i in range(len(s))]

# Leverage built-in functions
max_length = max(lengths)

# Use tuple unpacking for multiple return values
start, length = expand_around_center(s, i, i)

9.1.3.2 JavaScript

// Use destructuring for multiple return values
const [start, length] = expandAroundCenter(s, i, i);

// Use map for transformations
const lengths = Array.from({length: s.length}, (_, i) => 
    expandAroundCenter(s, i, i)[1]
);

// Use spread operator for finding maximum
const maxLength = Math.max(...lengths);

9.1.3.3 Java

// Use records (Java 16+) for returning multiple values
record PalindromeResult(int start, int length) {}

// Use streams for transformations
int maxLength = IntStream.range(0, s.length())
    .map(i -> expandAroundCenter(s, i, i).length())
    .max()
    .orElse(0);

9.2 Working with AI Code Suggestions

AI assistants excel at generating code, but using them effectively requires a thoughtful approach.

9.2.1 Prompting for Implementation

When asking AI to implement code, provide context from previous steps. The key is to give the AI your pseudocode and design decisions so it generates code that matches what you’ve already reasoned through, not whatever it might invent on its own.

You:

I'd like to implement our longest palindromic substring algorithm in Python based on the pseudocode we developed. Here's a reminder of our approach:

1. We'll check each character position as a potential center for a palindrome
2. For each center, we'll expand outward as long as we have matching characters
3. We'll check both odd-length palindromes (single character center) and even-length palindromes (between character center)
4. We'll keep track of the longest palindrome found

Could you provide a clean Python implementation with good variable names and documentation?
TipBring your pseudocode to the prompt

Notice how the human restated the algorithm’s key steps before asking for code. This is Step 5 building directly on Step 4 – the pseudocode provides constraints that keep the AI’s output aligned with the solution you designed, not one the AI might prefer.

AI:

Here's a clean implementation of the longest palindromic substring algorithm in Python:

def longest_palindromic_substring(s):
    """
    Find the longest palindromic substring in a given string.
    
    Args:
        s: Input string
        
    Returns:
        The longest palindromic substring within the input string
    """
    if not s:
        return ""
    
    start = 0  # Starting index of longest palindrome
    max_length = 1  # Length of longest palindrome
    
    def expand_around_center(left, right):
        """
        Expand around a potential palindrome center and return the bounds.
        
        Args:
            left: Left index of the potential palindrome center
            right: Right index of the potential palindrome center
            
        Returns:
            Tuple of (start_index, length) of the palindrome
        """
        # Expand as long as we stay in bounds and have matching characters
        while left >= 0 and right < len(s) and s[left] == s[right]:
            current_length = right - left + 1
            
            # Update longest palindrome if current one is longer
            if current_length > max_length:
                nonlocal start, max_length
                start = left
                max_length = current_length
                
            # Expand outward
            left -= 1
            right += 1
    
    # Check every possible center position
    for i in range(len(s)):
        # Odd length palindromes (single character center)
        expand_around_center(i, i)
        
        # Even length palindromes (between character center)
        if i < len(s) - 1:
            expand_around_center(i, i + 1)
    
    # Extract and return the longest palindromic substring
    return s[start:start + max_length]

This implementation follows the center expansion approach we discussed. It has:

1. Clear variable names (`start`, `max_length`, etc.)
2. Comprehensive documentation with docstrings
3. A helper function to handle the expansion logic
4. Proper handling of both odd and even length palindromes
5. Efficient tracking of the longest palindrome found

The time complexity is O(n²) where n is the length of the string, and the space complexity is O(1) since we only use a constant amount of extra space.

The AI produced a working implementation – but working is not the same as finished. Because you understand the algorithm from earlier steps, you’re in a position to evaluate the design choices, not just whether it runs.

9.2.2 Evaluating AI-Generated Code

Always critically evaluate code generated by AI:

  1. Correctness - Does it implement the algorithm as intended?
  2. Edge cases - Does it handle all the edge cases we identified?
  3. Performance - Is it reasonably efficient?
  4. Readability - Is it clearly written and well-documented?
  5. Idiomaticity - Does it follow language-specific best practices?

If there are issues, provide specific feedback. Vague responses like “looks good” or “can you fix it?” don’t give the AI enough to work with. Point to specific lines and explain what you’d change and why.

You:

This implementation looks generally good, but I noticed two potential issues:

1. The helper function updates the global variables directly. It might be cleaner to have it return the palindrome bounds instead, making the function more reusable.

2. We don't need to check for even-length palindromes when i is the last character since there's no character after it.

Could you address these issues in a revised implementation?
NoteReview means engaging, not just accepting

The human reviewed the code and raised two specific concerns – one about design (returning values vs. modifying globals) and one about correctness (unnecessary check). This kind of targeted feedback is only possible because the human understood the algorithm before seeing the code.

AI:

You've made excellent observations. Here's a revised implementation that addresses both issues:

def longest_palindromic_substring(s):
    """
    Find the longest palindromic substring in a given string.
    
    Args:
        s: Input string
        
    Returns:
        The longest palindromic substring within the input string
    """
    if not s:
        return ""
    
    start = 0  # Starting index of longest palindrome
    max_length = 1  # Length of longest palindrome
    
    def expand_around_center(left, right):
        """
        Expand around a potential palindrome center and return the bounds.
        
        Args:
            left: Left index of the potential palindrome center
            right: Right index of the potential palindrome center
            
        Returns:
            Tuple of (start_index, length) of the palindrome
        """
        # Expand as long as we stay in bounds and have matching characters
        while left >= 0 and right < len(s) and s[left] == s[right]:
            # Expand outward
            left -= 1
            right += 1
        
        # Return palindrome bounds (start is left+1 because left went one step too far)
        # Length is (right-1) - (left+1) + 1 = right - left - 1
        return left + 1, right - left - 1
    
    # Check every possible center position
    for i in range(len(s)):
        # Odd length palindromes (single character center)
        odd_start, odd_length = expand_around_center(i, i)
        if odd_length > max_length:
            start = odd_start
            max_length = odd_length
        
        # Even length palindromes (between character center)
        # Only check if we're not at the last character
        if i < len(s) - 1:
            even_start, even_length = expand_around_center(i, i + 1)
            if even_length > max_length:
                start = even_start
                max_length = even_length
    
    # Extract and return the longest palindromic substring
    return s[start:start + max_length]

The changes I've made:

1. Modified `expand_around_center` to return the palindrome bounds instead of modifying global variables directly. This makes the function more reusable and easier to test.

2. Added an explicit check to avoid trying to form even-length palindromes when at the last character, which would be unnecessary.

3. Updated the main loop to capture the returned values from `expand_around_center` and update our tracking variables only if a longer palindrome is found.

These changes make the code more modular and eliminate the unnecessary check, while maintaining the correct functionality.

The revised version is cleaner because the human drove the design decisions. The AI handled the mechanical work of rewriting the function, but the human identified what needed to change. That’s the right division of labour at this step.

9.2.3 Handling Unfamiliar Syntax or Libraries

When AI suggests code that uses unfamiliar syntax or libraries, ask for explanations rather than blindly accepting them. This is an opportunity to learn, not just to ship.

You:

In the implementation, you're returning a tuple and then unpacking it with:

odd_start, odd_length = expand_around_center(i, i)

I'm not familiar with this Python syntax. Could you explain how tuple unpacking works and why it's useful here?

AI:

Sure! This is called "tuple unpacking" or "multiple assignment" in Python, and it's a very convenient feature.

### How Tuple Unpacking Works

When a function returns multiple values in Python, they're automatically packaged as a tuple. Tuple unpacking allows you to assign each value in the tuple to a separate variable in a single line of code.

In this case, `expand_around_center(i, i)` returns a tuple of two values: `(start_index, length)`. The unpacking syntax:

odd_start, odd_length = expand_around_center(i, i)

is equivalent to:

result = expand_around_center(i, i)
odd_start = result[0]
odd_length = result[1]

### Why It's Useful Here

Tuple unpacking is particularly useful in this algorithm because:

1. **Clarity**: It makes it immediately clear what values we're getting back from the function

2. **Conciseness**: It reduces three lines of code to one, making the code more readable

3. **Intent**: It shows that these two values are related and come from the same operation

This pattern is very common in Python, especially when functions naturally produce multiple related values. It's considered a Pythonic way to handle multiple return values without creating custom classes or using more complex data structures.

Would you like me to show you some other common patterns for tuple unpacking in Python?
TipAsking “how does this work?” is not a weakness

If the AI introduces syntax you don’t recognise, stop and ask. You’ll use that pattern again, and understanding it now means you won’t need to ask next time. Copying code you don’t understand is borrowing knowledge you’ll have to repay later – with interest.

9.3 Maintaining Code Quality

AI-generated code can be a starting point, but ensuring high-quality code remains a human responsibility.

9.3.1 Idiomatic Code

Ensure that code follows the conventions and best practices of the target language:

# Non-idiomatic Python
i = 0
while i < len(collection):
    item = collection[i]
    process(item)
    i = i + 1

# Idiomatic Python
for item in collection:
    process(item)

Ask AI to make code more idiomatic. This is a case where a little delegation is fine – once you understand the logic, asking the AI to polish syntax into language-native patterns is a good use of its strengths.

You:

This code works, but I'd like it to be more idiomatic Python. Can you suggest any improvements?
NoteDelegating boilerplate vs. delegating thinking

Asking the AI to make working code more idiomatic is appropriate delegation. The understanding came from earlier steps; you’re now asking for syntactic polish. That’s different from asking the AI to write the solution from scratch.

9.3.2 Error Handling

Ensure the code handles errors gracefully:

def longest_palindromic_substring(s):
    # Add input validation
    if not isinstance(s, str):
        raise TypeError("Input must be a string")
    
    if not s:
        return ""
    
    # Rest of implementation...

9.3.3 Performance optimisation

Look for opportunities to optimise performance without sacrificing readability:

# Original implementation
for i in range(len(s)):
    # Only check centers with potential to beat current max
    if min(i, len(s) - i - 1) * 2 + 1 <= max_length:
        continue  # Skip this center if it can't yield a longer palindrome
    
    # Process this center...

9.3.4 Documentation and Comments

Ensure code is well-documented:

def longest_palindromic_substring(s):
    """
    Find the longest palindromic substring in a given string.
    
    This function uses the center expansion approach, which has O(n²) time complexity
    and O(1) space complexity.
    
    Args:
        s (str): The input string to process
        
    Returns:
        str: The longest palindromic substring. If multiple palindromes have the
             same maximum length, returns the first one found.
             
    Raises:
        TypeError: If input is not a string
        
    Examples:
        >>> longest_palindromic_substring("babad")
        "bab"  # "aba" would also be a valid return value
        
        >>> longest_palindromic_substring("cbbd")
        "bb"
    """
    # Implementation...

9.4 Practical Exercise: Implementing Our Solution

Let’s apply Step 5 to our ongoing example of finding the longest palindromic substring:

9.4.1 Converting the Pseudocode

Starting with our pseudocode from the previous chapter:

FUNCTION findLongestPalindromicSubstring(string)
    IF string is empty THEN
        RETURN empty string
    END IF
    
    SET startIndex = 0
    SET maxLength = 1
    
    FUNCTION expandAroundCenter(left, right)
        WHILE left >= 0 AND right < length of string AND string[left] = string[right]
            currentLength = right - left + 1
            IF currentLength > maxLength THEN
                SET maxLength = currentLength
                SET startIndex = left
            END IF
            DECREMENT left
            INCREMENT right
        END WHILE
    END FUNCTION
    
    FOR i = 0 TO length of string - 1
        // Check odd length palindromes
        expandAroundCenter(i, i)
        
        // Check even length palindromes
        IF i < length of string - 1 THEN
            expandAroundCenter(i, i + 1)
        END IF
    END FOR
    
    RETURN substring of string from startIndex to startIndex + maxLength - 1
END FUNCTION

We’ll implement this in Python, incorporating the improvements we’ve discussed.

9.4.2 Final Implementation

def longest_palindromic_substring(s):
    """
    Find the longest palindromic substring in a given string.
    
    Args:
        s (str): The input string to process
        
    Returns:
        str: The longest palindromic substring
        
    Examples:
        >>> longest_palindromic_substring("babad")
        "bab"  # Note: "aba" would also be a valid return value
        >>> longest_palindromic_substring("cbbd")
        "bb"
    """
    # Handle empty input
    if not s:
        return ""
    
    start = 0
    max_length = 1
    
    # Helper function to expand around a center
    def expand_around_center(left, right):
        """
        Expand around a potential palindrome center and find the longest palindrome.
        
        Args:
            left (int): Left position of the center
            right (int): Right position of the center
            
        Returns:
            tuple: (start_index, length) of the palindrome
        """
        # Expand as long as characters match and we're in bounds
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        
        # Return palindrome bounds
        # left+1 because we went one position too far left
        # right-left-1 calculates the length
        return left + 1, right - left - 1
    
    # Check each potential center
    for i in range(len(s)):
        # Skip centers that can't yield longer palindromes
        remaining_chars = min(i, len(s) - i - 1)
        if remaining_chars * 2 + 1 <= max_length:
            continue
        
        # Check odd-length palindrome
        odd_start, odd_length = expand_around_center(i, i)
        if odd_length > max_length:
            start = odd_start
            max_length = odd_length
        
        # Check even-length palindrome
        if i < len(s) - 1:
            even_start, even_length = expand_around_center(i, i + 1)
            if even_length > max_length:
                start = even_start
                max_length = even_length
    
    # Return the longest palindromic substring
    return s[start:start + max_length]

9.4.3 Testing the Implementation

After implementing our solution, we should test it with the cases we identified earlier:

# Test with various inputs
test_cases = [
    "",                # Empty string
    "a",               # Single character
    "aa",              # Two identical characters
    "abc",             # No palindromes longer than 1
    "babad",           # Odd-length palindrome
    "cbbd",            # Even-length palindrome
    "racecar",         # Entire string is a palindrome
    "aabbaa"           # Multiple palindromes
]

for test in test_cases:
    result = longest_palindromic_substring(test)
    print(f"Input: {test}, Output: {result}")

9.5 Key Takeaways

  • Converting pseudocode to code should be an incremental, systematic process
  • Take advantage of language-specific features and idioms for cleaner implementations
  • When using AI-generated code, always evaluate it critically for correctness, efficiency, and readability
  • Focus on writing idiomatic code with proper error handling and documentation
  • Use AI to explain unfamiliar syntax or patterns rather than just accepting them
  • Test your implementation against a variety of inputs, especially edge cases