Display Peter's keyboard pattern

8

1

Assume an simple keyboard with this layout:

1 2 3 4 5 6 7 8 9 0
A B C D E F G H I J
K L M N O P Q R S T
U V W X Y Z . , ? !

Peter's keyboard pattern can be generated by starting at the top left of the keyboard, and displays the first three characters and a newline. It shifts over one character and displays the second, third, and fourth key. Once it reaches the end of a row it continues at the end of the next row and goes backwards, until it reaches the start of that row and then goes forward on the next row, and so on until it reaches the beginning of the last row.

This is Peter's keyboard pattern:

123
234
345
456
567
678
789
890
90J
0JI
JIH
IHG
HGF
GFE
FED
EDC
DCB
CBA
BAK
AKL
KLM
LMN
MNO
NOP
OPQ
PQR
QRS
RST
ST!
T!?
!?,
?,.
,.Z
.ZY
ZYX
YXW
XWV
WVU

Write a program which accepts no input and displays Peter's keyboard pattern. The program must be smaller than 152 bytes, i.e. the size of the string it outputs.

This is code golf, so the shortest solution wins.

Peter Olson

Posted 2011-12-02T03:10:28.517

Reputation: 7 412

The 152 byte limit seems unnecessary--hardcoding the output with no compression whatsoever is bad golfing anyways. It could also potentially bar languages such as brainf**k, where large programs are almost always the occurance but there still could be some clever golfing going on. – Zwei – 2016-10-03T22:36:08.003

Even in Golfscript I'm struggling to come up with a shorter way of generating the 40-character base string. – Peter Taylor – 2011-12-02T08:18:20.830

@PeterTaylor I was expecting somebody to use some sort of list comprehension, like [1..9 0 J..A K..T ! ? , . Z..U]. – Peter Olson – 2011-12-03T17:02:28.753

You can use echo {1..9} "0" {J..A} {K..T} '!?,.' {Z..U}|sed 's/ //g' in bash, but need already 13 chars to append a sed-command, to remove blanks. This makes 57 chars, and no triple has been build so far. With sed's hold-command, it should be possible, but in 6 chars, to beat the perl-solution? – user unknown – 2011-12-09T00:38:43.957

Answers

7

Perl, 63 chars

say for"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"=~/(?=(...))/g

This solution uses the say function (available since Perl 5.10.0 with the -E switch, or with use 5.010). Without it, the best I can do is 67 chars (and an extra newline at the end of the output):

print"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"=~/(?=(...))/g,$,=$/

Earlier 65-char solution:

s//1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU/;s!(?=(...))!say$1!eg

Replacing say$1 with print$1.$/ lets the code run on older perls, at the cost of 5 extra characters.

Ilmari Karonen

Posted 2011-12-02T03:10:28.517

Reputation: 19 513

you beat me for few characters. +1. – Toto – 2011-12-02T13:20:18.953

2

Go for the 63-char solution directly. meta discussion

– J B – 2011-12-02T15:20:34.430

6

J, 66 62 45 characters

Turns out I was being too clever by half.

3]\'1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU'

is all I needed all along.

Previously:

_2}.|:>(];1&|.;2&|.)'1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU'

and:

38 3$($:@}.,~3$])^:(3<#)'1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU'

Gareth

Posted 2011-12-02T03:10:28.517

Reputation: 11 678

6

APL, 43 characters

⍪3,/(1⌽⎕D),(⌽10↑⎕A),(10↑10⌽⎕A),'!?,.',6↑⌽⎕A

This works on Dyalog APL. I managed to save a couple characters by generating the output string (everything after ). I'll write up an explanation when I get some time!

Here's how the output looks:

      ⍪3,/(1⌽⎕D),(⌽10↑⎕A),(10↑10⌽⎕A),'!?,.',6↑⌽⎕A
 123 
 234 
 345 
 456 
 567 
 678 
 789 
 890 
 90J 
 0JI 
 JIH 
 IHG 
 HGF 
 GFE 
 FED 
 EDC 
 DCB 
 CBA 
 BAK 
 AKL 
 KLM 
 LMN 
 MNO 
 NOP 
 OPQ 
 PQR 
 QRS 
 RST 
 ST! 
 T!? 
 !?, 
 ?,. 
 ,.Z 
 .ZY 
 ZYX 
 YXW 
 XWV 
 WVU

Explanation, from right to left:

  • First, we construct the string seen in all of the other entries:
    • 6↑⌽⎕A: ⎕A gives us a string of the uppercase alphabet, from A to Z. We then reverse it () and take () the first 6 characters of it, giving us 'ZYXWVU'. (In retrospect, this approach doesn't end up saving any characters, but I'll leave it because it fits in better.)
    • '!?,.',: We concatenate (,) the string '!?,.' with the previous result.
    • (10↑10⌽⎕A),: We take the first 10 characters (10↑) of the alphabet, which is first rotated ten times (10⌽), and concatenate this with the previous result. As an example of rotation, 5⌽'abcdef' (the string 'abcdef' rotated 5 times) gives us 'cdefab'.
    • (⌽10↑⎕A),: Take the first 10 characters of the alphabet and reverse them, and concatenate this with the previous string.
    • (1⌽⎕D),: ⎕D gives us the digits as a string, from 0 to 9, inclusive. We then rotate ('⌽') this string by 1, yielding '1234567890'. As before, we concatenate this with the previous result.
  • Now that we have our string built (which saved us 3 characters from the APL representation, due to surrounding quotes), we do some magic to build our final output:
    • 3,/: We take the string, in groups of three characters, and concatenate them.
    • To finish it off, we use to ravel our vector of strings (really a vector of character vectors) along the first dimension. This has the effect of changing its shape from 38 to 38 1 (a 38x1 matrix).

Dillon Cower

Posted 2011-12-02T03:10:28.517

Reputation: 2 192

4

R (75 characters)

embed(strsplit("1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU","")[[1]],3)[,3:1]

What does this do?

  • strsplit splits a string into substrings, in this case individual characters
  • embed is a really useful function that turns a vector into an array of overlapping elements
  • The rest is just indexing and sorting columns in the right order.

This produces and prints an array of characters:

> embed(strsplit("1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU","")[[1]],3)[,3:1]
      [,1] [,2] [,3]
 [1,] "1"  "2"  "3" 
 [2,] "2"  "3"  "4" 
 [3,] "3"  "4"  "5" 
 [4,] "4"  "5"  "6" 
 [5,] "5"  "6"  "7" 
 [6,] "6"  "7"  "8" 
 [7,] "7"  "8"  "9" 
 [8,] "8"  "9"  "0" 
 [9,] "9"  "0"  "J" 
[10,] "0"  "J"  "I" 
[11,] "J"  "I"  "H" 
[12,] "I"  "H"  "G" 
[13,] "H"  "G"  "F" 
[14,] "G"  "F"  "E" 
[15,] "F"  "E"  "D" 
[16,] "E"  "D"  "C" 
[17,] "D"  "C"  "B" 
[18,] "C"  "B"  "A" 
[19,] "B"  "A"  "K" 
[20,] "A"  "K"  "L" 
[21,] "K"  "L"  "M" 
[22,] "L"  "M"  "N" 
[23,] "M"  "N"  "O" 
[24,] "N"  "O"  "P" 
[25,] "O"  "P"  "Q" 
[26,] "P"  "Q"  "R" 
[27,] "Q"  "R"  "S" 
[28,] "R"  "S"  "T" 
[29,] "S"  "T"  "!" 
[30,] "T"  "!"  "?" 
[31,] "!"  "?"  "," 
[32,] "?"  ","  "." 
[33,] ","  "."  "Z" 
[34,] "."  "Z"  "Y" 
[35,] "Z"  "Y"  "X" 
[36,] "Y"  "X"  "W" 
[37,] "X"  "W"  "V" 
[38,] "W"  "V"  "U" 

Andrie

Posted 2011-12-02T03:10:28.517

Reputation: 687

+1 embed is very useful function indeed! – Tommy – 2011-12-02T18:34:57.080

3

Scala 70 chars:

"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU".sliding(3,1).mkString("\n")

user unknown

Posted 2011-12-02T03:10:28.517

Reputation: 4 210

2

Mathematica, 73

I independently arrived at the same method as everyone else:

Grid@Partition[Characters@"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU",3,1]

Mr.Wizard

Posted 2011-12-02T03:10:28.517

Reputation: 2 481

2

C, 98 104 characters

Since no one else has done so yet, I thought I would try actually generating the string on the fly, using some kind of run-length encoding for ascending and descending runs.

EDIT: After a number of iterations, here's finally a "clever" solution that ties with the shortest "dumb" solution in C at 98 characters:

char*s="0º.ÿK»Jº ü=ÿ*ø[×",a[99],*p=a,i;main(j){for(;i-=i<8?j=*s++,*s++:8;puts(p++))p[2]=j+=i%4-1;}

The code requires an 8-bit Latin-1 encoding (ISO-8859-1 or Windows-1252), which should work fine on all commonly used platforms, but saving the source with some less popular 8-bit code page, or UTF-8 will not work.

The data string was created by the following piece of emacs lisp:

(require 'cl)
(apply 'concat
  (let ((last-remainder 0))
    (loop for (begin count step twiddle)
          in '((?1 9 +1 4) (?0 1 +2 4) (?J 10 -1 4) (?K 10 +1 0)
               (?! 1 +1 4) (?? 1 +2 4) (?, 2 +2 4) (?Z 6 -1 8))
          append (prog1 (list (char-to-string (- begin step))
                              (char-to-string 
                               (logand 255 (- last-remainder
                                              (+ (* 8 (- count 1)) (+ step 1)
                                                 twiddle)))))
                   (setq last-remainder (+ (logand twiddle 7) step 1))))))

The "twiddle" bit is used to avoid unprintable ASCII control characters in the encoded data string, and to ensure that i==0 only at the end of the output.

C, 98 characters

However, even in C you can get a short program by simply printing consecutive triplets from the original string:

main(){char s[]="12,34567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU",*p=s+2;for(;*p=p[1];puts(p-3))*++p=0;}

han

Posted 2011-12-02T03:10:28.517

Reputation: 1 226

2

Groovy, 73

(0..37).each{println"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"[it..it+2]}

All the clever stuff I tried turned out to be longer than doing it the dumb way.

Armand

Posted 2011-12-02T03:10:28.517

Reputation: 499

2

Ruby 69 65 62

Based on Groovy example

38.times{|i|p"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"[i,2]}

Hauleth

Posted 2011-12-02T03:10:28.517

Reputation: 1 472

I think the convention here in the past for multiple answers in different languages has been to post one answer for each language. – Gareth – 2012-01-15T18:34:09.463

2

Haskell, 80

mapM_(putStrLn.take 3.(`drop`"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"))[0..37]

Angs

Posted 2011-12-02T03:10:28.517

Reputation: 4 825

1

Javascript, 103

for(a='1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU';a.length>2;a=a.substring(1))console.log(a.substr(0,3))

stephencarmody

Posted 2011-12-02T03:10:28.517

Reputation: 41

2An anonymous user suggested reducing this to 93 chars as for(a='1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU';a[3];a=a.slice(1))console.log(a.slice(0,3)) – Peter Taylor – 2011-12-22T07:33:06.440

1

Haskell, 94

main=putStr.unlines.take 38$take 3`map`iterate tail"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"

hammar

Posted 2011-12-02T03:10:28.517

Reputation: 4 011

1

Perl, 73 characters:

print substr('1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU',$_,3),$/for 0..37

Perl, 66 characters:

say substr"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU",$_,3for 0..37

when called by:

perl -E 'say substr"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU",$_,3for 0..37'

Toto

Posted 2011-12-02T03:10:28.517

Reputation: 909

1

D 124 chars

import std.stdio;void main(){enum s="1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU";
foreach(i;3..s.length){writeln(s[i-3..i)]);}}

ratchet freak

Posted 2011-12-02T03:10:28.517

Reputation: 1 334

1

JavaScript, 83 chars

for(i=0;i<38;)console.log('1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU'.substr(i++,3))

Decided to see if I could beat stephencarmody's solution. This is what I came up with.

Ilmari Karonen

Posted 2011-12-02T03:10:28.517

Reputation: 19 513

1

Python, 73

for i in range(38):print"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"[i:i+3]

Steven Rumbalski

Posted 2011-12-02T03:10:28.517

Reputation: 1 353

1

Smalltalk 102 99

1to:38 do:[:i|x:='1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU'copyFrom:i to:i+2.Transcript cr show:x]

I don't know Smalltalk very well so it's a little bit strange to me. It's compiles with gst others are not tested.

Hauleth

Posted 2011-12-02T03:10:28.517

Reputation: 1 472

1First trick: remove the space between 38 and do: -> 98 - second trick to remove another space, loop from 3to:40 and copyFrom:i-2to:i ->97 - third trick, don't use a variable x:=... show:x, just show:('...'copyFrom:i-2to:i) directly, that's -4 chars + 2 parenthesis ->95 – aka.nice – 2013-05-04T13:54:25.257

1

Q (66 63 chars)

(((!)38),'3)sublist\:"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"

skeevey

Posted 2011-12-02T03:10:28.517

Reputation: 4 139

1

Prolog, 131

This is probably why you never see Prolog entries. The newline is required (actually, any whitespace will do but there has to be whitespace).

z(S,L):-append(_,X,L),append(S,_,X),length(S,3).
:-forall(z(X,"1234567890JIHFEDCBAKLMNOPQRST!?,.ZYXWVU"),writef("%s\n",[X])),halt.

marinus

Posted 2011-12-02T03:10:28.517

Reputation: 30 224

0

Q, 63

{(x;3)sublist"1234567890JIHGFEDCBAKLMNOPQRST!?,.ZYXWVU"}'[(!)38]

tmartin

Posted 2011-12-02T03:10:28.517

Reputation: 3 917

0

///, 151 bytes

123
234
345
456
567
678
789
890
90J
0JI
JIH
IHG
HGF
GFE
FED
EDC
DCB
CBA
BAK
AKL
KLM
LMN
MNO
NOP
OPQ
PQR
QRS
RST
ST!
T!?
!?,
?,.
,.Z
.ZY
ZYX
YXW
XWV
WVU

Try it online!

Can't golf it more? This is, in a sense, <152 bytes.

Erik the Outgolfer

Posted 2011-12-02T03:10:28.517

Reputation: 38 134

This is not a serious contender by our rules. You make no effort to do better than the minimum score.

– Mego – 2016-10-04T15:48:35.680

I can't, that's why I posted it as-is. If you think you could do better, you can try. I don't know of any shorter solution, and, in fact, I did put some effort in, but realized that this isn't ever going to be further golfed down. – Erik the Outgolfer – 2016-10-04T16:24:01.240

I'm not convinced that this can be golfed at all in ///, but the spec says the program must be shorter than [...] the string it outputs, which isn't the case here. – Dennis – 2016-10-04T17:21:51.517

@Dennis No, it says the program must be smaller than 152 bytes (151 < 152). I think it's an error, or I read the post differently, but i.e. does not override the meaning of something, it just helps you to better understand it, in my sense. Also, I didn't include a trailing newline, maybe that's why this is 151 bytes. And yes, I read that section. – Erik the Outgolfer – 2016-10-05T14:18:31.880

0

Jelly, 41 40 bytes

ØAḣ⁵Ṛ
ØDṫ2;0;¢;“KLMNOPQRST!?,.ZYXWVU”ṡ3Y

Try it online!

Might (still) be golfable...

Erik the Outgolfer

Posted 2011-12-02T03:10:28.517

Reputation: 38 134