227
\$\begingroup\$

What happens when the CapsLock key on your keyboard doesn't have a notch in it?

"This hPPENS."

The goal of this program is to consistently emulate keyboard misses where each A press is replaced with CapsLock. Uppercase 'A's from the source should yield the same effect. When CapsLock is enabled, capitalization is reversed.

Test Cases

"The quick brown fox jumps over the lazy dog."
-> "The quick brown fox jumps over the lZY DOG."

"Compilation finished successfully."
-> "CompilTION FINISHED SUCCESSFULLY."

"What happens when the CapsLock key on your keyboard doesn't have a notch in it?"
-> "WhT Hppens when the CPSlOCK KEY ON YOUR KEYBOrd doesn't hVE  notch in it?"

"The end of the institution, maintenance, and administration of government, is to secure the existence of the body politic, to protect it, and to furnish the individuals who compose it with the power of enjoying in safety and tranquillity their natural rights, and the blessings of life: and whenever these great objects are not obtained, the people have a right to alter the government, and to take measures necessary for their safety, prosperity and happiness."
-> "The end of the institution, mINTENnce, ND dministrTION OF GOVERNMENT, IS TO SECURE THE EXISTENCE OF THE BODY POLITIC, TO PROTECT IT, nd to furnish the individuLS WHO COMPOSE IT WITH THE POWER OF ENJOYING IN Sfety ND TRnquillity their nTURl rights, ND THE BLESSINGS OF LIFE: nd whenever these greT OBJECTS re not obtINED, THE PEOPLE Hve  RIGHT TO lter the government, ND TO Tke meSURES NECESSry for their sFETY, PROSPERITY nd hPPINESS."

"aAaaaaAaaaAAaAa"
-> "" (Without the notch, no one can hear you scream)

"CapsLock locks cAPSlOCK"
-> "CPSlOCK LOCKS CPSlOCK"

"wHAT IF cAPSlOCK IS ALREADY ON?"
-> "wHt if CPSlOCK IS lreDY ON?"

The winning criterion is, as usual, the size of the submitted program's source code.

\$\endgroup\$
20
  • 124
    \$\begingroup\$ Welcome to the site! This is a nice first challenge, and unfortunately very relatable for me and my fT FINGERS. \$\endgroup\$
    – DJMcMayhem
    Mar 14, 2018 at 18:29
  • 99
    \$\begingroup\$ If only the enter key also had a notch in it so this wouldn' \$\endgroup\$
    – 12Me21
    Mar 14, 2018 at 19:33
  • 85
    \$\begingroup\$ t happen....... \$\endgroup\$
    – 12Me21
    Mar 14, 2018 at 19:33
  • 30
    \$\begingroup\$ Literally joined this site to upvote "Without the notch, no one can hear you scream" \$\endgroup\$
    – lucasvw
    Mar 15, 2018 at 21:10
  • 9
    \$\begingroup\$ I LSO WISH THt my keyboRD Hd a bigger bCKSPce kwt==et=y \$\endgroup\$ Mar 16, 2018 at 20:58

63 Answers 63

1 2
3
0
\$\begingroup\$

MMIX, 60 bytes (15 instrs)

E3020000 83030000 E7000001
CBFF0320 25FFFF41 5AFF0003
C7020220 F1FFFFFA 33FFFF1A
48FF0002 C6030302 A3030100
E7010001 5B03FFF4 F8000000

Disassembly and explanation:

fun SETL  $2,0              // E3020000: mask = 0
0H  LDBU  $3,$0,0           // 83030000: loop: chr = *src (src, dst are both char *)
    INCL  $0,1              // E7000001: src++
    ANDN  $255,$3,32        // CBFF0320: tmp = chr & ~32
    SUB   $255,$255,'A'     // 25FFFF41: tmp -= 'A'
    PBNZ  $255,1F           // 5AFF0003: iflikely(!tmp) goto nota
    XOR   $2,$2,32          // C7020220: mask ^= 32
    JMP   0B                // F1FFFFFA: goto loop
1H  CMPU  $255,$255,26      // 33FFFF1A: nota: tmp = tmp <=> 26
    BNN   $255,1F           // 48FF0002: if(tmp > 0) goto notlet
    XOR   $3,$3,$2          // C6030302: chr ^= mask
1H  STBU  $3,$1,0           // A3030100: notlet: *dst = chr
    INCL  $1,1              // E7010001: dst++
    PBNZ  $3,0B             // 5B03FFF4: iflikely(chr) goto loop
    POP   0,0               // F8000000: return

This was heavily inspired by corvus_192's answer.

It's essentially the same algorithm, except with multiple simplifications due to MMIX's symmetric register architecture.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 199 133 bytes

import Data.Char
l c|isUpper c=toLower c|1<2=toUpper c
f _""=""
f b(c:cs)|toLower c=='a'=f(not b)cs|1<2=(if b then l else id)c:f b cs

Run using f False "<string>"

Ungolfed version:

import Data.Char

flipCase c
    | isUpper c = toLower c
    | otherwise = toUpper c

f "" _ = ""
f b (c:cs)
    | toLower c == 'a' = f cs $ not b
    | True = (if b then flipCase else id) c:f b cs
\$\endgroup\$
3
  • \$\begingroup\$ You can replace cs by a single letter variable name and define f as an infix function. \$\endgroup\$
    – Laikoni
    Mar 20, 2018 at 11:06
  • \$\begingroup\$ Also note that when requiring extra arguments for the function, those need to be counted too. In this case the default would be to add 8 bytes to the byte count for f False and a newline. \$\endgroup\$
    – Laikoni
    Mar 20, 2018 at 11:10
  • \$\begingroup\$ Instead of using a boolean it is shorter to use 0 and 1. not b then becomes 1-b, b in the conditional becomes b>0 and f False just f 0. \$\endgroup\$
    – Laikoni
    Mar 20, 2018 at 11:14
0
\$\begingroup\$

Zsh, 97 107 bytes

for c (${(s..)1})case $c {a|A)((j^=1));;[b-z])((j))&&c=$c:u;|[B-Z])((j))&&c=$c:l;|*)s+=$c;}
<<<$s

-10 thanks to @GammaFunction. Try it Online!   107bytes

\$\endgroup\$
2
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.