Left-Handed Words
December 6, 2013
On a standard English keyboard, the letters that a touch-typist strikes with the fingers of the left hand are Q, W, E, R, T on the top row, A, S, D, F, G on the home row, and Z, X, C, V, B on the bottom row. For instance, words like FAST and ZEBRAS are left-handed words; PACKAGE and SOUTH are not.
Your task is to write a program that finds words that can be spelled using only letters that are struck with the fingers of the left hand; perhaps you are writing an exercise for a typing book. 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.
This grep takes running text as input and prints each match on its own line. The -o does that.
grep -Eio '\b[qwertasdfgzxcvb]+\b'
I wanted to use the -w option but it had a problem with a couple of (right-handed) Finnish letters. It’s possible that the boundary markers in the above expression still have a related problem with the same letters. Maybe in yet another decade letters will finally be letters, reliably.
Finnish seems to be a fairly right-handed language. We use all of the right-handed letters all the time (except the Swedish å), we don’t use qwzxc natively at all, and bf are somewhat marginal.
In Python. A good opportunity to look at regular expressions. I seldom use these.
I decided to forgo regular expressions for a simple set.
left_chars = set('qwertasdfgzxcv')
left_words = []
for word in open('/usr/share/dict/words'):
if all([c in left_chars for c in word.strip().lower()]):
left_words.append(word)
print(left_words)
def valid_word(word1):
validW = “qwertasdfgzxcvb”
for index1 in range(len(word1)):
if word1[index1] not in validW:
return False
elif index1 == len(word1)-1:
print word1
valid_word(“farter”)
In Haskell.
http://codepad.org/Dqtj2BhE
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LeftHandWords {
public boolean isLeftHandWord(String word) {
Pattern pattern = Pattern.compile("[yuiophjklnm]");
Matcher matcher = pattern.matcher(word);
if (matcher.find()) {
System.out.println("It’s not a left-handed word.");
System.out
.printf("I found the text \"%s\" starting at index %d and ending at index %d.%n",
matcher.group(), matcher.start(), matcher.end());
return true;
} else {
System.out.println("It’s a left-handed word.");
return false;
}
}
public static void main(String[] args) {
LeftHandWords lhw = new LeftHandWords();
lhw.isLeftHandWord("hello");
}
}
Clojure…
In guile scheme: http://community.schemewiki.org/?left-handed
easier clojure: