package com.madbean.kata8.readable; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * My attempt at a "readable" Kata 8 * * @author Matt Quail * @see Kata 8 */ public class KataEightReadable { private static final int FIND_WORD_SIZE = 6; private static final File WORDLIST_FILE = new File("d:/matt/dev/kata8/wordlist.txt"); /** set of words in the wordlist */ private Set words = new HashSet(); public static void main(String[] args) throws IOException { KataEightReadable program = new KataEightReadable(); program.readwords(); program.scanForConcatenatedWords(); } private void readwords() throws IOException { // every line in the file is a word, just read them in BufferedReader in = new BufferedReader(new FileReader(WORDLIST_FILE)); String word; while (null != (word = in.readLine())) { // the example in Kata 8 suggests that we are ment to // ignore case; so just make everything lowercase word = word.toLowerCase(); words.add(word); } in.close(); } private void scanForConcatenatedWords() { int matchCount = 0; for (Iterator i = words.iterator(); i.hasNext();) { String word = (String) i.next(); if (word.length() != FIND_WORD_SIZE) { // too big, or too small, we only want to look at words // of a particular size continue; } // look at the leading substrings of 'word' (and the // corresponding trailing substring), and see if said // leading and trailing substrings are actual words final int leadingMinLen = 1; final int leadingMaxLen = word.length() - 1; for (int leadingSize = leadingMinLen; leadingSize <= leadingMaxLen; leadingSize++) { String leadingWord = word.substring(0, leadingSize); String trailingWord = word.substring(leadingSize); if (words.contains(leadingWord) && words.contains(trailingWord)) { // 'word' is the concatenation of 'leadingWord' and 'trailingWord' System.out.println(leadingWord + " + " + trailingWord + " = " + word); matchCount++; } } } System.out.println("number found: " + matchCount); } }