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 http://icon.shef.ac.uk/Moby/mwords.html.
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.
[…] today’s Programming Praxis exercise, our goal is to find all the words in a dictionary that satisfy two […]
My Haskell solution (see http://bonsaicode.wordpress.com/2012/10/09/programming-praxis-two-word-games/ 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 wsimport 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(ns two-word-games (:require [clojure.java.io :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?)public class WordGame1
{
public List Execute()
{
string line;
var results = new List();
var file = LoadFile();
while ((line = file.ReadLine()) != null)
{
if (ContainsAllFiveVowels(line))
results.Add(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”}; }
}
}
[…] day, another post from Programming Praxis. Today they posted a word game that seems simple enough: first find all […]
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
A Python solution:
def is_sorted(seq): last = None for x in seq: if last is None: last = x else: 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): result.append(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 pprint(exercise1('words.lst')) pprint(exercise2('words.lst'))OK, those are great. But of course you can take a low-tech approach to word games, like here: http://daisybrain.wordpress.com/2012/03/09/wordarchy/