Fork me on GitHub

Project Notes

#409 flippedy

Using python to count vowels and flip words; cassidoo’s interview question of the week (2026-01-26).

Notes

The interview question of the week (2026-01-26):

You are given a string consisting of lowercase words, each separated by a single space. Determine how many vowels appear in the first word. Then, reverse each following word that has the same vowel count.

Examples:

flippedy("cat and mice")
> "cat dna mice"

flippedy("banana healthy")
> "banana healthy"

Thinking about the Problem

Seems pretty straight-forward: count the vowels in each word:

  • vowels: aeiou
  • don’t forget: case-insensitive

There is a typical requirement ambiguity however: “Determine how many vowels appear in the first word” could mean two different things:

  • count the number of characters that are vowels i.e “Wheee!” == 3
  • OR count how many vowels appear in the word i.e “Wheee!” == 1

The examples don’t answer this definitively. I’ll go with the first interpretation

A First Go

Breaking the problem down into 2 simple parts:

  • count vowels in a word
  • iterate the words, flipping as required

For a first go, I’ll just used list comprehensions:

def vowel_count(word):
    vowels = 'aeiouAEIOU'
    return sum(1 for char in word if char in vowels)

def flippedy(input):
    words = input.split()
    first_word = words.pop(0)
    target_count = vowel_count(first_word)
    flipped_words = [word[::-1] if vowel_count(word) == target_count else word for word in words]
    return ' '.join([first_word] + flipped_words)

The expression word[::-1] is a neat way of reversing a string in python.

And that works nicely:

$ ./challenge.py "cat and mice"
cat dna mice
$ ./challenge.py "banana healthy"
banana healthy

Refining the Code

The list comprehension is a bit ungainly for counting vowels. How about a regex approach instead:

def vowel_count(word):
    return len(re.findall(r'[aeiou]', word, re.IGNORECASE))

Tests

I’ve setup some validation in test_challenge.py:

$ ./test_challenge.py
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Example Code

Final code is in challenge.py:

#! /usr/bin/env python
from sys import argv
import re

def vowel_count(word):
    return len(re.findall(r'[aeiou]', word, re.IGNORECASE))

def flippedy(input):
    words = input.split()
    first_word = words.pop(0)
    target_count = vowel_count(first_word)
    flipped_words = [word[::-1] if vowel_count(word) == target_count else word for word in words]
    flipped_words.insert(0, first_word)
    return ' '.join(flipped_words)

if __name__ == '__main__':
    if len(argv) == 2:
        print(flippedy(argv[1]))
    else:
        print("Usage: challenge.py '<string>'")

Credits and References

About LCK#409
pythoncassidoo

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader