# Character Cross

I was expecting to post something more complex as my first puzzle in PCG, but a particular, uh... homework question on Stack Overflow inspired me to post this. They want to:

print the following pattern for any given word that contains odd number of letters:

P           M
R       A
O   R
G
O   R
R       A
P           M

Notice that letters are a knight's move apart in the pattern you need to print. So, every other column is empty. -- (Thanks xnor for pointing this out.)

## Rules

1. Using C++ is prohibited. As I may link this question there.
2. You may use stdout, or any means of quickly outputting a string (e.g. alert() in JavaScript).
3. As always, shortest code wins.
• This is in the related posts on stack overflow: stackoverflow.com/q/5508110 Oct 29, 2014 at 11:26
• @flawr I think it means odd, as in not an even number. Oct 29, 2014 at 14:28
• God I am stupid, I thought it was kind of an obscure computer scientist abbreviation=) Oct 29, 2014 at 14:34
• @jpjacobs: Bytes, unless the question explicitly says otherwise. Oct 29, 2014 at 17:20
• Whole program or just a function? (real smart of me to answer first and make this question later...) Oct 29, 2014 at 20:23

## APL (373534 27)

↑{∊2↑¨⍵↑¨I}¨↓(+∨⌽)∘.=⍨⍳⍴I←⍞

It takes input from the keyboard, like so:

↑{∊2↑¨⍵↑¨I}¨↓(+∨⌽)∘.=⍨⍳⍴I←⍞
CODE-GOLF
C               F
O           L
D       O
E   G
-
E   G
D       O
O           L
C               F
• All of my upvotes belong to APL. Oct 29, 2014 at 16:33

# Python 2 - 94 90 89 88

s=input()
L=len(s)
R=range(L)
for i in R:print" ".join([s[j]," "][j!=i!=L+~j]for j in R)

Input:

"CODE-GOLF"

Output:

C               F
O           L
D       O
E   G
-
E   G
D       O
O           L
C               F
• I was just writing the same thing. My one improvement is i in[j,L+~j].
– xnor
Oct 29, 2014 at 7:08
• @xnor: Found an even shorter condition. ;) Oct 29, 2014 at 7:11
• Clever. You can still do L+~j though.
– xnor
Oct 29, 2014 at 7:12
• @xnor: Ah, you're right. I always forget the +~ trick... Oct 29, 2014 at 7:16
• Can you do for i in R:print" ".join([s[j]," "][j!=i!=L+~j]for j in R) to save a char? (I don't have access to Python 2 now to test.)
– xnor
Oct 29, 2014 at 7:20

# Pyth, 22

Vzjdm?@zd}N,dt-lzd\ Uz

Test:

$pyth -c 'Vzjdm?@zd}N,dt-lzd\ Uz' <<< "CODE-GOLF" C F O L D O E G - E G D O O L C F Explanation: (Implicit) z = input() (Implicit) d = ' ' Vz for N in range(len(z)): jd print(d.join( m map(lambda d: ?@zd z[d] if }N N in ,dt-lzd (d,len(z)-d-1) else \ " ", Uz range(len(z))))) • No bit complement in Pyth? – xnor Oct 29, 2014 at 20:16 • @xnor No, but it wouldn't help. Suppose ~ was bitwise not in Pyth. Then we could change t-lzd to +lz~d - still 5 characters. Oct 29, 2014 at 20:20 • Vz is a neat trick: I didn't know that U<string> gave range(len(<string>)). Oct 29, 2014 at 20:21 • Yeah, it's a nice feature. Works that way on lists too, but not tuples necessarily. EDIT: Oops, that's missing from the documentation. I'll add it. Sorry. Oct 29, 2014 at 20:23 ## Python 3: 75 chars s=input() i=n=len(s) while i:i-=1;a=[" "]*n;a[i]=s[i];a[~i]=s[~i];print(*a) For line i, we start with a list of spaces and set the entries i from the front and back to be equal to the letters of the input string. Then, we print the result. Python strings are immutable, so a must be a list of characters instead. The list a must be initialized inside the loop or the modifications will carry over between loops. We use print(*a) to print each character in the list, space separated, which requires Python 3. The output lines are symmetrical, so we can have i count down rather than up using a while loop. >>> CODE-GOLF C F O L D O E G - E G D O O L C F It also works for an even number of letters. >>> CODEGOLF C F O L D O E G E G D O O L C F • Converting this to Pyth, I get VlzJ*]dlz=@JN@zN=@Jt_N@_zNjdJ which is 29 bytes... I guess I'll have to try something else to beat APL. Also, +1 for proof that Python 3 can out-golf Python 2 (sometimes) Oct 29, 2014 at 13:37 • @fry Python 3 usually out-golfs 2 in my experience. Even if nothing changes anywhere else, most challenges give one input and expect one output, and len("raw_input()") + len("print ") > len("input()") + len("print()"). Oct 29, 2014 at 14:48 • @undergroundmonorail Well, the trouble with that is that input() is also valid in python 2 (it just acts somewhat differently), so unless the input for the challenge is quite strict, python 2 will usually win out. Oct 29, 2014 at 14:51 • Wow, even with Python 2 and print" ".join(a) your code is shorter (82) than mine. Oct 29, 2014 at 16:19 • @FryAmTheEggman See my answer - it's an adaptation of the other python answer, and it's much shorter than APL. Pyth really doesn't like assigning to indexes, unfortunately. Oct 29, 2014 at 20:03 # CJam, 27 25 bytes l_,S*:Sf{W):W2$tW~@tS}zN*

Try it online.

### Example run

$cjam <(echo 'l_,S*:Sf{W):W2$tW~@tS}zN*') <<< CROSS; echo
C       S
R   S
O
R   S
C       S

As the example in the answer, each line has trailing whitespace.

### How it works

" N := '\n'; R := []; S = ' '; W := -1 ";
l                            " Q := input()                         ";
_,S*:S                      " S := len(Q) * S                      ";
f{            }       " for each C in Q:                     ";
"   T := S                             ";
W):W                "   W += 1                             ";
2$t " T[W] := C "; W~@t " T[~W] := C "; " R += [T] "; S " R += [S] "; z " R := zip(R) "; N* " R := N.join(R) "; " print R "; • Sorry that this is basically a 'great answer!' comment, but I just had to say that it never, ever, would have occurred to me to build the box by columns instead of by rows. Oct 29, 2014 at 18:57 # Canvas, 5 bytes ＼＼：⇵ｎ Try it here! Explanation: \ create a diagonal of the input \ create an even steeper diagonal of that :⇵ reverse a copy of it vertically n overlap the two # Pure Bash, 94 bytes l=${#1}
for((;t<l*l;t++));{
((x=t%l))||echo

# JavaScript (E6) 101 95 129 136

Edit Wrong letter spacing. Fixed.
Edit Simpler and shorter using classic for loops
As a function, output via popup.

F=a=>{
for(l=a.length+1,i=s=o='';++i<l;s='\n')
for(j=0;++j<l;)
o+=s+((s=' ')+a)[j==i|i+j==l&&j]
}

Previous version using .map

--q<p
?B(q)+a[q]+B(p-q-1)+c
:B(p)+c+(p-q?B(q-p-1)+a[q]:'')
,B=n=>' '.repeat(n),q=a.length).join('\n'))

Test In FireFox/FireBug console

F('Java-Script')

Output

J                   t
a               p
v           i
a       r
-   c
S
-   c
a       r
v           i
a               p
J                   t
• This doesn't have enough horizontal spacing - there should be columns of all spaces between each column with letters. Oct 29, 2014 at 20:06

# Befunge-93, 68 71

:~:84*-!#@#v_\:2*\4+p1+
::+\4+g:!| >$1+0:>p1-:: 00+4\*2:\<>0p#:- ^#2g You can test it here. It'll come up with an input dialog box on each pass through ~; enter your word one character at a time (it does say the input is 'klunky' after all), ending with a space. It won't print to the console; this wouldn't be Befunge without a hint of self-modification, after all! Instead it will modify its own grid to display the message. After it's done, the grid will look something like this: ~:84*-!#@#v_\:2*\4+p1+ ::+\4+g:!| >$1+0:>p1-::
00+4\*2:\<>0p#:- ^#2g

c               f
o           l
d       o
e   g
-
e   g
d       o
o           l
c               f

(Note the noble sacrifice of the cell at (0,0), after we know the pointer won't go there anymore, for the purpose of storing a piece of data.)

It also works with inputs of even length. Note that, since Befunge-93 is limited to an 80x25 grid, the input size is limited to 21 characters if you run it in a Befunge-93 interpreter. Running it as Befunge-98 should remove this limit.

Edit - Now it works more along the lines of the intended output, at the expense of only three characters of length.

• Note that the letters are separated with a blank column. Letters are relatively in L shape. Oct 30, 2014 at 7:56
• Drats! That complicated things rather significantly. I think I can salvage this, but it'll add to the ending size... oh well. Oct 30, 2014 at 17:30

# Javascript 102 84 85

Edit: Had to fix spacing. Not so small anymore.

function p(s){for(i in s){o='';for(n in s)o+=(n==i?s[n]:n==s.length-1-i?s[n]:' ')+' ';console.log(o)}}

p('Thanks-Dennis')

T                       s
h                   i
a               n
n           n
k       e
s   D
-
s   D
k       e
n           n
a               n
h                   i
T                       s
• This does not have enough horizontal spacing. Oct 30, 2014 at 17:22
• Wow, shorter than mine and not even using ES6. +1 Oct 30, 2014 at 19:30
• Better, but the spacing is still wrong (look at the X). Also, unless the question says otherwise, you should write a program or a function. Snippets with hardcoded variables are generally frowned upon. Oct 31, 2014 at 1:06
• And can't you make it function(s) since you don't recurse? Jun 23, 2017 at 12:54

## CJam, 38363534 32 bytes

l:I,,_f{f{_2$=@2$+I,(=|\I=S?S}N}

Test it here. This reads the input word from STDIN. It also works for an even number of characters. This prints a trailing column of spaces, but I don't see anything in the rules against that.

Explanation

l:I,,_f{f{_2$=@2$+I,(=|\I=S?S}N}
l:I                              "Read input and store it in I.";
,                             "Get string length.";
,_                           "Turn into range and duplicate.";
f{                       } "Map block onto first range, with second range on the stack.";
f{                   }   "Map block onto second range, with first iterator in stack.
y and x on the stack (x on top).";
_2$= "Duplicate x, duplicate y, check for equality."; @2$+               "Pull up y, duplucate x, add,";
I,(=           "Check if that's one less than the string length.";
|          "Bitwise or between conditions.";
\         "Swap condition and x.";
I=       "Take x'th character from the string.";
S?     "Push a space and select character depending on condition.";
S    "Push another space.";
N  "Push a line feed.";

The contents of the stack are printed automatically at the end of the program.

• My dear CJam, you've just got beaten by APL. ^_^
– user18967
Oct 29, 2014 at 11:09
• @vaxquis I was surprised I was able to beat it twice in the first place. This problem seems much more suited to APL and co than to stack-based languages. Oct 29, 2014 at 11:13
• well, at least it's easier to write l:I,,_f{f{_2$=@2$+I,(=|\I=S?S}N} on a napkin than ↑{∊2↑¨⍵↑¨I}¨↓(+∨⌽)∘.=⍨⍳⍴I←⍞ ... chuckle
– user18967
Oct 29, 2014 at 14:40
• Well, someone else managed to beat me using CJam, and I can't make my program any shorter, so CJam did eventually beat me. (But Pyth gets the crown.) (That said, @vaxquis, APL is far easier to read than most of the other languages used on here, certainly once you know the 60 or so characters, and certainly when compared to Pyth or CJam or even J.) Oct 30, 2014 at 9:42

# C, 105

two slightly different ways of doing it.

c,i,j;main(int l,char**v){for(l=strlen(v[1]);j-l;)putchar((c=v[1][i++])?i-1-j&&l-i-j?32:c:(i=0,j++,10));}

i,j;main(int l,char**v){for(l=strlen(v[1]);j-l;i++)putchar((l-i)?i-j&&l-i-j-1?32:v[1][i]:(i=-1,j++,10));}

If you want to add extra spaces, replace putchar( with printf(" %c", for an extra 5 characters.

## J - 36 30 bytes:

Edit: 6 characters shorter, credits go to @algorithmshark .

(1j1#"1' '&,({~](*>.*&|.)=)#\)

eg:

(1j1#"1' '&,({~](*>.*&|.)=)#\) 'Code-Golf'
C               f
o           l
d       o
e   G
-
e   G
d       o
o           l
C               f

Bonus: works with even length strings too:

(1j1#"1' '&,({~](*>.*&|.)=)#\) 'CodeGolf'
C             f
o         l
d     o
e G
e G
d     o
o         l
C             f

# Prolog - 240 bytes

:-initialization m.
+[]. +[H|T]:-(H=' ';!),+T.
+[H|T]+I+X:-0=:=I,X=H;+T+(I-1)+X.
+L+I+C+S:-L=:=I;S=[D|E],+C+I+B,+C+(L-I-1)+B,+B+2*I+D,+L+(I+1)+C+E,+B,writef('%s\n',[B]).
-X:-get_char(C),(C='\n',X=[];X=[C|Y],-Y).
m:- -X,length(X,L),+L+0+_+X.

Invocation:

$echo "Code-Golf" | swipl -qf c.pl C f o l d o e G - e G d o o l C f Readable: :- initialization(main). vars_to_spaces([]). vars_to_spaces([' '|T]) :- vars_to_spaces(T). vars_to_spaces([_|T]) :- vars_to_spaces(T). get_index([Head|_], Index, Result) :- 0 =:= Index, Result = Head. get_index([_|Tail], Index, Result) :- get_index(Tail, Index-1, Result). print_loop(Length, Index, Board, String) :- Length =:= Index; String = [FirstChar|RestString], get_index(Board, Index, Line), get_index(Board, Length-Index-1, Line), get_index(Line, 2*Index, FirstChar), print_loop(Length, Index+1, Board, RestString), vars_to_spaces(Line), writef('%s\n', [Line]). get_line(Line) :- get_char(C), ( C = '\n', Line = []; Line = [C|More], get_line(More)). main :- get_line(String), length(String, Length), print_loop(Length, 0, _, String). # Jelly, 11 bytes LḶḤ⁶ẋ;"µ»ṚY Try it online! # Powershell, 78 bytes +8 bytes thanks @streetster param($s)$s|% t*y|%{$r=,' '*$s.length;$r[+$i++]=$_;$r[-$i]=$s[-$i];$r-join' '} Explaned test script:$f = {

param($s) # declare a parameter$s stores a word
$s|% t*y|%{ # split$s into chars https://codegolf.stackexchange.com/a/168174/80745
$r=,' '*$s.length   # create an array of spaces with length equal length of the word
$r[$i++]=$_ # set first char$r[-$i]=$s[-$i] # set last char$r-join' '          # join the array to string
}

}

&$f PROGRAM '' &$f sword
''
&$f uhm '' &$f o
''
&$f codegolf # It works with word that contains even number of letters # Powershell, 70 bytes (if a word can be represented by an array of chars) +8 bytes thanks @streetster ($s=$args)|%{$r=,' '*$s.count;$r[+$i++]=$_;$r[-$i]=$s[-$i];$r-join' '} Test script:$f = {

($s=$args)|%{$r=,' '*$s.count;$r[+$i++]=$_;$r[-$i]=$s[-$i];$r-join' '}

}

&$f P R O G R A M '' &$f s w o r d
''
&$f u h m '' &$f o
''

89 + 1 for -n.

Run with:

printf \$z\ }
echo)