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.

Advertisement

Pages: 1 2

11 Responses to “Left-Handed Words”

  1. Jussi Piitulainen said

    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.

  2. Paul said

    In Python. A good opportunity to look at regular expressions. I seldom use these.

    import re
    p = re.compile(r'\b[QWERTASDFGZXCVB]+\b', re.IGNORECASE)            
    with open(fname) as f:
        for w in p.findall(f.read()):
            print w
    
  3. Danny said

    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)

  4. pythonist said

    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”)

  5. strawhatguy said
    (defun left-handed-letter-p (c)
      (find c "qwertasdfgzxcvb" :test #'char-equal))
    
    (defun left-handed-word-p (word)
      (every #'left-handed-letter-p word))
    
    (defun dict-file-to-left-handed-words (file)
      (with-open-file (dict file)
        (loop :for line := (read-line dict nil :eof)
           :until (eq line :eof)
           :if (left-handed-word-p line)
           :collect line)))
    
    (length (dict-file-to-left-handed-words "/usr/share/dict/words")) => 2797
    
  6. dhlzj said

    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");
    }

    }

  7. dhlzj said
    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");
    	}
    
    }
    
  8. wiltonsilva said

    Clojure…

    
    (def keys #{"q" "w" "e" "r" "t" "a" "s" "d" "f" "g" "z" "x" "c" "v" "b"})
    
    (defn all-true? [props]
      (every? true? props))
    
    (defn left-hand-word? [word]
      (all-true?  
        (map (fn [x] (contains? keys x)) 
             (drop 1 (clojure.string/split word #"")))))
    
    
  9. ChrisP said

    easier clojure:

    (map #(every? #{\q \w \e \r \t \a \s \d \f \g \z \x \c \v \b} %) 
     ["hello" "fast" "zylophone" "sweatervest" "south" "zebras"])
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: