Two Word Games

October 9, 2012

It’s been a while since we played word games. We have two today:

1) Find all the words in a dictionary that contain exactly five vowels (a, e, i, o and u) in ascending order.

2) Find all the words in a dictionary of length at least six that contain letters in strictly ascending alphabetical order.

These games are easy to play using regular expressions, so you should solve them without regular expressions, using only simple string manipulations. If your system doesn’t provide a dictionary, you can find one at

Your task is to play the two games. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

9 Responses to “Two Word Games”

  1. […] today’s Programming Praxis exercise, our goal is to find all the words in a dictionary that satisfy two […]

  2. My Haskell solution (see for a version with comments):

    ascVowels :: String -> Bool
    ascVowels = (== "aeiou") . filter (`elem` "aeiou")
    sixAsc :: Ord b => [b] -> Bool
    sixAsc s = length s == 6 && and (zipWith (<) s $ tail s)
    main :: IO ()
    main = do ws <- fmap lines $ readFile "354984si.ngl"
              mapM_ putStrLn $ filter ascVowels ws
              putStrLn "---"
              mapM_ putStrLn $ filter sixAsc ws
  3. Paul said
    import itertools as IT
    vowels = list("aeiou")
    ord_vowels = lambda s: [si for si in s if si in vowels] == vowels
    ord_chars_gt6 = lambda s: 6 <= len(s) == len(set(s)) and sorted(s) == list(s)
    def gen_words(fname):
        for line in open(FNAME):
            yield line.split()[0]
    #example for ord_chars_gt6    
    for w in IT.ifilter(ord_chars_gt6, gen_words(FNAME)):
        print w
  4. kawas said
    (ns two-word-games  (:require [ :as jio]))
    (defn five-vowels-in-order? [word]
      (when (= [\a \e \i \o \u] (filter #{\a \e \i \o \u} word)) word))
    (defn six-and-sorted? [word]
      (and (= 6 (count word)) (apply <= (map int word))))
    (defn game [dict-fp predicate-fn]
      (with-open [rdr (jio/reader dict-fp)]
        (doseq [w (filter predicate-fn (line-seq rdr))] (println w))))
    (game "words.txt" five-vowels-in-order?)
    (game "words.txt" six-and-sorted?)
  5. PG said

    public class WordGame1
    public List Execute()
    string line;
    var results = new List();
    var file = LoadFile();
    while ((line = file.ReadLine()) != null)
    if (ContainsAllFiveVowels(line))

    return results;

    private static StreamReader LoadFile()
    var assembly = Assembly.GetExecutingAssembly();
    return new StreamReader(assembly.GetManifestResourceStream(“ProgrammingPraxis.Content.354984si.ngl”));

    private static bool ContainsAllFiveVowels(string textString)
    return Vowels.All(vow => textString.IndexOf(vow, StringComparison.CurrentCultureIgnoreCase) >= 0);

    private static IEnumerable Vowels
    get { return new List {“a”, “e”, “i”, “o”, “u”}; }

  6. […] day, another post from Programming Praxis. Today they posted a word game that seems simple enough: first find all […]

  7. JP said

    That was fun. Most of the work went into loading the file and iterating through them line by line (I miss Python’s for line in open(filename, 'r'):), but the rest was pretty straight forward.

    My solution in Racket / Scheme

  8. A Python solution:

    def is_sorted(seq):
        last = None
        for x in seq:
            if last is None:
                last = x
                if x < last:
                    return False
                last = x
        return True
    def vowels(s):
        return reduce(lambda s, c: s + c if c in 'aeiou' else s, s, '')
    def map_file(fn, filename):
        result = []
        with open(filename) as infile:
            for line in infile:
                line = line.strip()
                if fn(line):
        return result
    def exercise1(filename):
        def fn(line):
            return len(line) > 5 and vowels(line) == 'aeiou'
        return map_file(fn, filename)
    def exercise2(filename):
        def fn(line):
            return len(line) > 5 and is_sorted(line)
        return map_file(fn, filename)
    from pprint import pprint
  9. OK, those are great. But of course you can take a low-tech approach to word games, like here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 709 other followers

%d bloggers like this: