Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Last active February 28, 2019 14:38
Show Gist options
  • Save Radcliffe/50f6f494cdc011147d52284d2c992485 to your computer and use it in GitHub Desktop.
Save Radcliffe/50f6f494cdc011147d52284d2c992485 to your computer and use it in GitHub Desktop.
Qwerty to Dvorak translator: Explaining XKCD 1787.
#!/usr/bin/env python
# This little Python script was written to explain XKCD comic #1787 (Voice Commands).
# In the comic, Cue Ball has reprogrammed his phone to use the Dvorak keyboard layout
# for voice recognition. (See http://xkcd.com/1787/)
# For those who don't know, the standard keyboard layout in the United States is called
# QWERTY, after the first six letters in the first row of letters. But there are a few
# enthusiasts who advocate an alternative layout, called Dvorak. These layouts have the
# same characters, but in different positions, as shown below.
qwerty_layout = ("`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"
"~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?")
dvorak_layout = ("`1234567890[]',.pyfgcrl/=\\aoeuidhtns-;qjkxbmwvz"
"~!@#$%^&*(){}\"<>PYFGCRL?+|AOEUIDHTNS_:QJKXBMWVZ")
# Cue Ball speaks the nonsense phrase "svat ussupd ;dlh a kdbk" into his phone. To decode
# this message, we must find the position of each character in the QWERTY layout, and
# replace it with the corresponding character in the Dvorak layout.
q2d_dict = dict(zip(qwerty_layout, dvorak_layout))
def qwerty_to_dvorak(text):
return ''.join(
q2d_dict[char] if char in q2d_dict else char
for char in text)
print (qwerty_to_dvorak("svat ussupd ;dlh a kdbk"))
# The expected output of this script is "okay google send a text".
@aldot
Copy link

aldot commented Jan 25, 2017

I'm unable to create a pull-request for a gist, but please see
https://gist.github.com/aldot/87df1796a195fbba37a385a78340fba0/revisions#diff-f7b0eefd3291fb8f225b75e1f4bd1dbd
resp.consider the patch below.
Cheers :)

From b6d585698f71832456eadf3200d2e83c8a778914 Mon Sep 17 00:00:00 2001
From: Bernhard Reutner-Fischer [email protected]
Date: Wed, 25 Jan 2017 12:18:23 +0100
Subject: [PATCH] make it more pythonic

The .get() method with a default value is the pythonic way of writing
d[i] if i in d else i

Signed-off-by: Bernhard Reutner-Fischer [email protected]

qwerty-to-dvorak.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/qwerty-to-dvorak.py b/qwerty-to-dvorak.py
index 3b6b17d..9defaed 100644
--- a/qwerty-to-dvorak.py
+++ b/qwerty-to-dvorak.py
@@ -23,9 +23,9 @@ q2d_dict = dict(zip(qwerty_layout, dvorak_layout))

def qwerty_to_dvorak(text):
return ''.join(

  •    q2d_dict[char] if char in q2d_dict else char
    
  •    q2d_dict.get(char, char)
       for char in text)
    

print (qwerty_to_dvorak("svat ussupd ;dlh a kdbk"))

-# The expected output of this script is "okay google send a text".
\ No newline at end of file
+# The expected output of this script is "okay google send a text".

2.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment