Skip to content

Instantly share code, notes, and snippets.

@bheeshmar
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bheeshmar/6733b384e60336a0d148 to your computer and use it in GitHub Desktop.
Save bheeshmar/6733b384e60336a0d148 to your computer and use it in GitHub Desktop.
Word splitter solution in bash
#!/bin/bash
# call with ./splitwords.bash thisisawesome
find_substrings() {
local remainder=$1
matched=$2
# echo "in find_substrings '${remainder}' (${matched[@]})"
if [[ -z $remainder ]]; then
echo "Found: ${matched[@]}"
exit 0
fi
for sub in ${sorted_subwords[@]}; do
local subl=$sub
local prefix_removed=${remainder#$subl}
# echo "sub: '${subl}' prefix_removed: '${prefix_removed}' remainder: '${remainder}'"
if [[ $remainder = $prefix_removed ]]; then
# echo "${remainder} does not start with ${subl}"
:
else
matched=("${matched[@]}" "${subl}")
# echo "calling find_substrings with '${prefix_removed}' (${matched[@]})"
find_substrings $prefix_removed $matched
fi
done
}
to_split=$1
letter_filter=$(echo ${to_split}| grep -o . | sort -u | paste -s -d '\0' -)
grep -w -e "[${letter_filter}]\+" /usr/share/dict/words > /tmp/words_with_matching_letters
while read w; do
[[ ${to_split} =~ $w ]] && echo ${BASH_REMATCH}
done </tmp/words_with_matching_letters >/tmp/possible_subwords
subwords=($(</tmp/possible_subwords))
IFS=$'\n' GLOBIGNORE='*' sorted_subwords=($(printf '%s\n' ${subwords[@]} | awk '{ print length($0) " " $0; }' | sort -nr | cut -d ' ' -f 2-))
echo ${sorted_subwords[@]}
declare -a matched_words
find_substrings ${to_split} $matched_words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment