Programming with a Sporadic Shift Key

39

7

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.

Calvin's Hobbies

Posted 2014-10-17T04:50:22.627

Reputation: 84 000

Why's the 1024-character restriction? – John Dvorak – 2014-10-17T05:20:52.753

@JanDvorak Without a limit there might be ways to make the limit of valid programs approach 100% as the size of the program increases. (Though that would be pretty cool.) – Calvin's Hobbies – 2014-10-17T05:24:55.850

I can't think of any way to improve the baseline score of 2^-(18 + whatever the language needs) – John Dvorak – 2014-10-17T05:32:18.967

Please ping me once there is a better answer than mine – John Dvorak – 2014-10-17T05:34:00.750

@JanDvorak Without the restriction, you could do something like exec(min(["code","code","code",...])), with a ginormous number of copies of your code to run a lower-cased version of it basically guaranteed. – xnor – 2014-10-17T06:00:46.580

2+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 – 2014-10-17T06:04:17.757

@Nathaniel I was just wishing that too,. It's only been an hour, I may change it. – Calvin's Hobbies – 2014-10-17T06:06:57.870

2@Calvin'sHobbies but... what about the current answers? – John Dvorak – 2014-10-17T06:13:01.420

@xnor the problem with your suggestion is that each pair quotes is a 2^-1 to your score and so are the commas inbetween. Unless you do something like .words – John Dvorak – 2014-10-17T06:15:40.107

I have changed the phrase. Existing answers can stay, though not a ton may need to change in them. Apologies for rule change. (@Nathaniel) – Calvin's Hobbies – 2014-10-17T06:16:02.383

@Calvin'sHobbies Now the score becomes a non standard as the previous string will always result in a lower score. Can you either switch back to older string or mandate the use of new one ? – Optimizer – 2014-10-17T06:52:08.740

@Optimizer Sorry but it would be unfair to the original answers to force the change. All new answers must use the new string through, and it may be possible to beat the best old answer. – Calvin's Hobbies – 2014-10-17T07:03:16.773

1@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 – 2014-10-17T07:05:31.250

1@Calvin'sHobbies Too bad there's the 1024 char limit. I'm going to need to rethink my answer... – Justin – 2014-10-17T07:05:32.507

@Optimizer I have removed the 2^-7 one in the answer. So there isn't much trouble now. – jimmy23013 – 2014-10-17T08:14:53.820

@user23013 There are other answers based on the previous string too. – Optimizer – 2014-10-17T09:23:37.133

1@Optimizer At least they won't win. – jimmy23013 – 2014-10-17T09:31:50.893

I'm assuming you're not allowed or able to hold down the other shift key to force everything shifted? Or would doing so give you a 75% chance of being shifted and 25% of unshifted if both of your shift keys act identical? – WOUNDEDStevenJones – 2014-10-17T21:15:18.673

@WOUNDEDStevenJones No. Assume pressing either shift does nothing. The computer just thinks shift is being pressed randomly. – Calvin's Hobbies – 2014-10-17T21:22:15.187

1For once in my life, knowledge of VB is more useful than knowledge of C++. – Pharap – 2014-10-18T10:24:31.443

A perfect time to use WhiteSpace?

– recursion.ninja – 2014-10-20T01:03:49.227

@awashburn That's not allowed (for winning). See rules. – Calvin's Hobbies – 2014-10-20T01:23:58.033

Answers

46

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!
$ 

Digital Trauma

Posted 2014-10-17T04:50:22.627

Reputation: 64 644

2Very very cool! I hope you can get it to 1024. Mind explaining the code a bit? – Calvin's Hobbies – 2014-10-17T20:08:06.870

2@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 – 2014-10-17T20:10:33.917

2Please 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 – 2014-10-17T20:39:42.927

3Changing 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 – 2014-10-17T22:25:54.133

3# Interpret all upper case: $ tr A-Z a-z - The args are backwards, right? – Izkata – 2014-10-18T17:21:59.280

@Izkata Yes, copy/paste error - good catch. – Digital Trauma – 2014-10-18T21:07:11.543

16

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.

es1024

Posted 2014-10-17T04:50:22.627

Reputation: 8 953

2@edc65 ucwords capitalizes the first letter of each word – es1024 – 2014-10-17T07:45:33.260

1a 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 – 2014-10-17T20:47:32.200

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 – 2014-10-17T21:11:15.557

1@WOUNDEDStevenJones I believe relying on IDE features would be counted as cheating. – Ingo Bürk – 2014-10-18T09:53:35.177

1What about <? at the start of the program? – Michael Mior – 2014-10-20T13:32:41.033

4

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.

jimmy23013

Posted 2014-10-17T04:50:22.627

Reputation: 34 042

Is this intentionally a K instead of C? – Paŭlo Ebermann – 2014-10-19T11:22:18.560

@PaŭloEbermann That character is independent. It can also be a space or something else. – jimmy23013 – 2014-10-19T11:43:04.187

4

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

Frxstrem

Posted 2014-10-17T04:50:22.627

Reputation: 676

1But note that this is off-competition as per the rules. – Ingo Bürk – 2014-10-21T16:40:26.937

3

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.

Justin

Posted 2014-10-17T04:50:22.627

Reputation: 19 757

If it helps, the phrase is now longer (but not capitalized normally). – Calvin's Hobbies – 2014-10-17T06:21:46.270

@Calvin'sHobbies I saw that. It doesn't help; it harms. – Justin – 2014-10-17T06:29:38.303

3

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.

jimmy23013

Posted 2014-10-17T04:50:22.627

Reputation: 34 042

2

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"

edc65

Posted 2014-10-17T04:50:22.627

Reputation: 31 086

I think visual studio would auto-correct the casing for you. – Pharap – 2014-10-18T10:28:41.193

@Pharap but the casing outside quotes is not the problem... VS would not correct a '(' shifted to '9' – edc65 – 2014-10-18T10:38:16.287

1In 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 – 2014-10-18T11:02:23.917

1

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.

Justin

Posted 2014-10-17T04:50:22.627

Reputation: 19 757

Pyth doesn't have a lower() - it wasn't getting used enough. I should probably add it back in. – isaacg – 2014-10-20T01:30:25.330

1

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.

Optimizer

Posted 2014-10-17T04:50:22.627

Reputation: 25 836

1

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

Berkin

Posted 2014-10-17T04:50:22.627

Reputation: 61

1

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

Qwertiy

Posted 2014-10-17T04:50:22.627

Reputation: 2 697

0

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.

Wernisch

Posted 2014-10-17T04:50:22.627

Reputation: 2 534

0

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

Mike

Posted 2014-10-17T04:50:22.627

Reputation: 11