Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Remove Vowels and Consonants

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Remove Vowels and Consonants. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Capital Group research workflows use compact text labels that may contain letters, digits, spaces, and punctuation. Implement a function that removes vowels, consonants, or both while preserving every retained character in its original order.

Formal Specification

Given an ASCII string s and two Boolean flags, return a new string:

  1. Remove an ASCII vowel when remove_vowels is True. Vowels are a, e, i, o, and u, regardless of case.
  2. Remove an ASCII consonant when remove_consonants is True.
  3. Preserve letters that are not selected for removal.
  4. Always preserve digits, whitespace, punctuation, and other non-ASCII characters.
  5. The input string must not be modified. Return a string.

You may use a single left-to-right scan. Do not use regular expressions.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain ASCII letters, digits, whitespace, punctuation, and Unicode characters
  • remove_vowels and remove_consonants are Boolean values
  • Preserve the relative order of all retained characters

Function Signature

def filter_letters(s, remove_vowels, remove_consonants):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output