# Programming with a Sporadic Shift Key

There's something wrong with your keyboard. The Shift key has a mind of its own. Every time you type a character you have no idea whether it will come out shifted or not (though it is 50-50). Besides the standard character keys, nothing on the keyboard is functional. There is no mouse or other means of input.

Somehow you know the only way to fix things is to write a program that outputs Dear Computer, please stop giving me shift! to stdout. Luckily your IDE is open and you are capable of running a program, but of course as you type it you won't know which characters will be shifted.

What sequence of key-presses would you use to write a program that has the best possible chance of working on the first try?

# Details

You are using a standard QWERTY keyboard, so there are 50 character keys you can press.

Unshifted versions (47 only):

1234567890-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./


Shifted versions (47 only):

~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?  The last 3 keys are Enter, Tab, and Space, which are the same shifted and unshifted. A sequence of N of these characters has 2N - (whitespace char count) ways it might have been output if you had typed them with your faulty keyboard. For example, typing A Space m might have yielded a m or A m or a M or A M. Write a program these characters and look at all of its 2N - (whitespace char count) possible shift combinations. The more combinations that output Dear Computer, please stop giving me shift! the better. Your score is the number of working combinations (valid programs) divided by the total number of combinations. The highest score wins. # Notes • For valid programs, printing precisely Dear Computer, please stop giving me shift! and nothing else to stdout should be the only side effect. • Valid programs should not take input. • Invalid programs can do anything whatsoever. • Comments may be used anywhere. • Whitespace answers cannot win because getting a 100% score is (relatively) trivial. You may still submit a Whitespace solution for fun. • Your program must be at most 1024 characters long. Update: Changed Stop giving me shift! to Dear Computer, please stop giving me shift! to allow for more complex answers. Existing answers may stay the way they are if desired. • +1, it's a great challenge, but I wish you'd given us a longer output string. I've thought of several clever tricks, but all of them take more than 18 characters in all the languages I know. – Nathaniel Oct 17 '14 at 6:04 • @Calvin'sHobbies but... what about the current answers? – John Dvorak Oct 17 '14 at 6:13 • @Calvin'sHobbies It's not! And the 1024 limitation prevents that very well too. given all this, I am voting for this to close as unclear what you are asking. Please do not change spec which gives disadvantage to most of the answers – Optimizer Oct 17 '14 at 7:05 • @Calvin'sHobbies Too bad there's the 1024 char limit. I'm going to need to rethink my answer... – Justin Oct 17 '14 at 7:05 • @Optimizer At least they won't win. – jimmy23013 Oct 17 '14 at 9:31 ## 13 Answers # Applescript, 20 (100%) I believe I can claim a perfect score here: • The Applescript Editor (my IDE) automatically converts all keywords to lower case upon compiling/running • Furthermore, defined variable and handler names are case insensitive - For example if a handler myFunc is defined, then the IDE will automatically convert MYFUNC, myfunc, MyFuNc, etc references to myFunc • I have only used alphabetic characters, spaces and newlines, so I don't need to worry about shifted numbers and punctuation characters. Here is is: global f on j at a set end of a to j end on c at a j at a j at a end on k at a repeat with q in system info j at a end end on w at a set d to count a j at a return string id d end on z at a set end of f to a end set h to space set y to h as list k at y k at y set x to w at y c at y c at y c at y c at y c at y set q to w at y k at y c at y c at y copy y to b c at y set s to w at y set d to w at y set f to d as list k at b k at b set a to w at b c at b j at b set e to w at b set y to w at b set g to w at b set d to w at b set i to w at b c at b set l to w at b set m to w at b set n to w at b set o to w at b set p to w at b j at b set r to w at b z at e z at a z at r z at h z at s set s to w at b set t to w at b set u to w at b set v to w at b z at o z at m z at p z at u z at t z at e z at r z at q z at h z at p z at l z at e z at a z at s z at e z at h z at s z at t z at o z at p z at h z at g z at i z at v z at i z at n z at g z at h z at m z at e z at h z at s z at d z at i z at y z at t z at x f as text  Thanks to the help of @kernigh and @paradigmsort, this is now 1020 bytes, just squeaking in under the 1024 byte limit! ### Explanation: • The characters for output string are generated using string id <n>, which returns the character corresponding to the ascii value n • Because we are avoiding digits, each n has has to be generated by more fundamental means. Specifically we generate each n by counting a list, and then adding another item to that list. The repeat with q in system info allows us to do this 16 times, as system info always returns a 16-item list. • Using a similar technique, we add each character of the final string in turn to a list. • Finally that last list is coerced to text and printed. ### Output: Using the osascript interpreter, but the Applescript Editor works just as well: $ # Interpret as-is:
$osascript dearcase.scpt Dear Computer, please stop giving me shift!$
$# Interpret all lower case:$ tr A-Z a-z < dearcase.scpt | osascript
Dear Computer, please stop giving me shift!
 # Interpret all upper case:
$tr a-z A-Z < dearcase.scpt | osascript Dear Computer, please stop giving me shift!$
$# Interpret random case for each letter:$ while read; do for ((i=0;i<${#REPLY};i++)); do c="${REPLY:i:1}"; if ((RANDOM%2)); then printf "%s" "$(tr a-z A-Z <<< "$c")"; else printf "%s" "$(tr A-Z a-z <<< "$c")"; fi; done; echo; done < dearcase.scpt | osascript
Dear Computer, please stop giving me shift!
$ • Very very cool! I hope you can get it to 1024. Mind explaining the code a bit? – Calvin's Hobbies Oct 17 '14 at 20:08 • @Calvin'sHobbies Applescript is supposed to be one of the most readable languages - you really don't understand this? ;-) I'll try to add an explanation in a bit... – Digital Trauma Oct 17 '14 at 20:10 • Please post the golfed version, with no indentation and no name in end name. As is, I must use sed -e 's/^ *//' -e 's/^end.*/end/' dearcase.scpt | wc -c to count it. Also, you can get down from 1044 to 1037 by changing count of a to count a, and deleting log from log f as text. I test with an old OS X 10.4 system, where log command has empty result; osascript only prints the result, not the event log, so I get no output unless I delete log. – kernigh Oct 17 '14 at 20:39 • Changing c to call j twice instead of three times and updating the call sites as needed saves 7 characters and gets you under the limit. – paradigmsort Oct 17 '14 at 22:25 • # Interpret all upper case:$ tr A-Z a-z - The args are backwards, right? – Izkata Oct 18 '14 at 17:21

# PHP, 2^-12

echo ucwords(strtolower('Dear Computer, ')).strtolower('please stop giving me shift!');


PHP being PHP, capitalization of echo, ucwords, and strtolower don't matter. The calls to ucwords and strtolower ensure that the case of the strings won't change the output.

Therefore, the only characters that can't be changed are ((,)).(!); (10 characters).

Each pair of quotes also has a 50% chance of being valid ('' and "" are valid, but '" and "' are not), therefore each adding another power of two.

• @edc65 ucwords capitalizes the first letter of each word – es1024 Oct 17 '14 at 7:45
• a good IDE will add the correct closing quotes for you (you'll only ever end up with '' or "", as well as the closing parentheses, so though you still have a 50% chance for (, the ) should be automatic. – WOUNDEDStevenJones Oct 17 '14 at 20:47
• shoot, you can't use any sort of navigation keys (arrows), so even if your IDE did autocomplete correctly, you'd need to type that same character again or else it will insert the new (wrong) one, like this: strtolower("Dear Computer, '"0)... – WOUNDEDStevenJones Oct 17 '14 at 21:11
• @WOUNDEDStevenJones I believe relying on IDE features would be counted as cheating. – Ingo Bürk Oct 18 '14 at 9:53
• What about <? at the start of the program? – Michael Mior Oct 20 '14 at 13:32

# CJam, 2-7 2-12 chance

'D"ear Komputer, please stop giving me shift!"el4'Ct


It has similar idea as Quincunx's first answer, but in CJam.

• Is this intentionally a K instead of C? – Paŭlo Ebermann Oct 19 '14 at 11:22
• @PaŭloEbermann That character is independent. It can also be a space or something else. – jimmy23013 Oct 19 '14 at 11:43

# Whitespace (645 bytes, 2^0 probability)

Since this program only uses tabs, spaces and newlines (which are all unaffected by shifting), it gives a 100% success rate.





Program run

• But note that this is off-competition as per the rules. – Ingo Bürk Oct 21 '14 at 16:40

# CJam, 2-9 chance, 739 bytes

"
(lines of space characters)
"N/2fb:c


base64:

IiAJICAgIAkgCQkgICAJICAKICAgCSAJCSAgIAkJCQkgCQoJCQkJIAkgIAkJIAkJICAJCiAJICAg
IAkgCQkgICAgCSAKICAgCSAJCSAgCSAgICAgIAogCQkJCSAJICAJCSAJIAkJCgkJCSAJICAJCSAJ
CSAJCQkKIAkgCQkgIAkgICAgIAkgCQoJIAkJICAJICAgIAkgICAgCgkgIAkJIAkJCQkgIAkJIAkK
CQkgCSAgCQkgCQkJIAkgIAogICAJIAkJICAgCQkJCSAJCiAJICAgIAkgCQkgICAgCSAKIAkJCQkg
CSAgCQkgCQkgIAogICAJIAkJICAJICAgICAgCgkgCQkgIAkgICAgCSAgICAKCSAgCSAgICAJIAkg
CQkgIAogICAJIAkJICAgCQkJCSAJCgkJCQkgCSAgCQkgCQkgIAkKICAgIAkgCQkgICAJCSAJCQog
ICAJIAkJICAgCQkJCSAJCiAgIAkgCQkgIAkgICAgICAKICAgIAkgCQkgICAJCSAJCQoJCSAJICAJ
CSAJCQkgCSAgCgkJCSAJICAJCSAJCSAJCQkKCSAJCSAgCSAgICAJICAgIAogICAJIAkJICAJICAg
ICAgCgkgCSAgCQkgCQkJIAkJCQkKICAJCSAJCQkJIAkgICAgCQogCQkgIAkgICAgCSAgCQkgCiAg
CQkgCQkJCSAJICAgIAkKICAJICAgIAkgCSAJCQkJIAoJIAkgIAkJIAkJCSAJCQkJCiAgIAkgCQkg
IAkgICAgICAKIAkgCQkgIAkgICAgIAkgCQogICAJIAkJICAgCQkJCSAJCiAgIAkgCQkgIAkgICAg
ICAKICAgIAkgCQkgICAJCSAJCQogCQkgCQkJCSAJICAJICAgCiAgCQkgCQkJCSAJICAgIAkKCQkg
CQkJCSAJICAJIAkJIAoJCSAJICAJCSAJCQkgCSAgCgkJIAkJCQkgCSAgCQkgIAkiTi8yZmI6Yw==


Based on the idea of Optimizer and Quincunx's answer.

# Python 2, 2-20 chance

print'S'+'top giving me shift!'.lower()


print, S, +, !, and .lower() must all be the correct version; that's 18 chars. The two quotes for the strings must also align, that makes two more powers of two.

For any of the top giving me shift, if it is converted to the capital version, we convert it to lowercase and it works properly.

Sadly, I can't make use of Python's nifty capitalize(), because that takes one more character.

• If it helps, the phrase is now longer (but not capitalized normally). – Calvin's Hobbies Oct 17 '14 at 6:21
• @Calvin'sHobbies I saw that. It doesn't help; it harms. – Justin Oct 17 '14 at 6:29

# VisualBasic.net 2^-18

Critical chars: .("DearC"+",!".())

Sub Main
console.write("Dear C"+"omputer, please stop giving me shift!".tolower())
End Sub


Tested in LINQPad, Language = "VB Program"

• I think visual studio would auto-correct the casing for you. – Pharap Oct 18 '14 at 10:28
• @Pharap but the casing outside quotes is not the problem... VS would not correct a '(' shifted to '9' – edc65 Oct 18 '14 at 10:38
• In that case, something more useful: you can get rid of the potential case of getting > instead of . by changing Console.Write(string) to Write(string) and swapping (string).ToLower() out for LCase(string). – Pharap Oct 18 '14 at 11:02

# Pyth, 2**-15

+"S"$"top giving me shift!".lower()  I can't seem to find Pyth's version of lower(), so I'll borrow from Python. The characters that can't be swapped are +"S"$, !, and .lower(), for a total of 14 chars. The quotes after the \$ must match, adding another power of 2.

• Pyth doesn't have a lower() - it wasn't getting used enough. I should probably add it back in. – isaacg Oct 20 '14 at 1:30

# Rant, 2-21

[caps:first][?[caps:word][?[caps:lower]Dear Computer][caps:lower], please stop giving me shift!]


A series of metapatterns and caps calls force proper formatting. Function names and arguments are case insensitive.

Online version

## CJam, 2-13 chance

As per the updated string (696 bytes).

"

"N/:,2/Kfb:c


Only ""N/:,2/Kfb:c are at risk right now.

Try it online here and since this text editor is eating up all the spaces, here is the gist with the correct code.

## VB.NET, 2^-12 2^-11

2-12

Module All
Sub Main
Console.WriteLine StrConv("Dear Computer, ", vbTuesday) & "please stop giving me shift!".ToLower
End Sub
End Module


2-11

Imports System.Console

Module All
Sub Main
Write StrConv("Dear Computer, ", vbTuesday)
WriteLine "please stop giving me shift!".ToLower
End Sub
End Module

I thought about my answer before getting to the '50 characters including only Enter, Tab, Space' rule. So mine is not valid.

Seems having case insensitive programming languages is the key here. Alternatively, if you could use a Real standard QWERTY keyboard (including Caps Lock, the other Shift, Backspace, and the Numpad Period) there would only be 2 necessary 50% chances which would only result in 2 additional key presses per failure. My invalid answer for a C# Console Application pointing out actual key presses:

HoldShift(C CpsLockOn onsole)

NumPadPeriod

HoldShift(
CpsLockOff W CpsLockOn rite("
CpsLockOff D CpsLockOn ear space
CpsLockOff C CpsLockOn omputer
)

, (repeat with Backspace until correct)

HoldShift( please stop giving me shift!"))

; (repeat with Backspace until correct)


76 minimum presses with only 2 necessary 50% chances

# Excel, 2-11

=PROPER("Dear Computer, ")&LOWER("please stop giving me shift!")


Case of PROPER and LOWER don't matter. Entering a formula starting with + automatically inserts required =. So only (, ", ,, ), &, !` can break.