Created
June 28, 2016 19:58
-
-
Save mjec/108fd486b9b09a022a8daaa838182fff to your computer and use it in GitHub Desktop.
Split a string into two English words, if possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A solution to the two word version of | |
# http://thenoisychannel.com/2011/08/08/retiring-a-great-interview-problem | |
# in bash, because I can. | |
length_of_input=`echo -n "$1" | wc -c` | |
for len in `seq $length_of_input` | |
do | |
left=`echo -n "$1" | cut -c1-$len` | |
let "rem = $len + 1" | |
right=`echo -n "$1" | cut -c$rem-` | |
grep ^$left$ /usr/share/dict/words >/dev/null | |
left_in=$? | |
grep ^$right$ /usr/share/dict/words >/dev/null | |
right_in=$? | |
if [ "$left_in" -eq 0 ] && [ "$right_in" -eq 0 ] | |
then | |
echo $left $right | |
break | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment