Hackbright Code Challenges

Replace Vowels: Solution

Replace Vowels: Solution

Problem

Replace Vowels

Whiteboard

Easier

Concepts

Loops, Lists

Download

replace-vowels-solution.zip


replacevowels.py
def replace_vowels(chars):
    """Given list of chars, return a new copy, but with vowels replaced by '*'."""

    # START SOLUTION

    vowels = {'a', 'e', 'i', 'o', 'u'}
    new = []

    for char in chars:

        if char.lower() in vowels:
            char = '*'

        new.append(char)

    # You could also solve this with list comprehension:
    #
    #   return ['*' if char in vowels else char for char in chars]

    return new

Advanced: Runtime Questions

  • What if you were given a string instead of a list of characters, and you were asked to return a string with the vowels replaced? Can you keep the same O(n) runtime?

  • What if the function were given a list of characters to replace, rather than always replacing only vowels? What would the runtime be?