31
\$\begingroup\$

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.
\$\endgroup\$
  • 2
    \$\begingroup\$ This is in the related posts on stack overflow: stackoverflow.com/q/5508110 \$\endgroup\$ – Level River St Oct 29 '14 at 11:26
  • 2
    \$\begingroup\$ @flawr I think it means odd, as in not an even number. \$\endgroup\$ – NinjaBearMonkey Oct 29 '14 at 14:28
  • 32
    \$\begingroup\$ God I am stupid, I thought it was kind of an obscure computer scientist abbreviation=) \$\endgroup\$ – flawr Oct 29 '14 at 14:34
  • 2
    \$\begingroup\$ @jpjacobs: Bytes, unless the question explicitly says otherwise. \$\endgroup\$ – Dennis Oct 29 '14 at 17:20
  • 2
    \$\begingroup\$ Whole program or just a function? (real smart of me to answer first and make this question later...) \$\endgroup\$ – Rodolfo Dias Oct 29 '14 at 20:23

40 Answers 40

1
2
1
\$\begingroup\$

K (oK), 31 bytes

Solution:

`c$a||a:(2*#x)$(-1-2*!#x)$,:'x:

Try it online!

Explanation:

Generate the diagonal, and or with the reverse of the diagonal

`c$a||a:(2*#x)$(-1-2*!#x)$,:'x: / the solution
                             x: / save input as x
                          ,:'   / enlist (,:) each-both (')
                         $      / pad (negative is right-pad)
               (        )       / do this together
                      #x        / count (#) length of x
                     !          / range (!) 0..this count
                   2*           / double it
                -1-             / subtract from -1
              $                 / pad
        (    )                  / do this together
           #x                   / count (#) length of x
         2*                     / multiply by 2
      a:                        / save as a
     |                          / reverse (|) it
   a|                           / or (|) with a
`c$                             / cast to characters                           
\$\endgroup\$
1
\$\begingroup\$

K4, 26 bytes

Solution:

a||a:(2*#x)$(-1-2*!#x)$$x:

Explanation:

Similar to my oK answer but a little shorter:

a||a:(2*#x)$(-1-2*!#x)$$x: / the solution
                        x: / save input as x
                       $   / string ($) breaks into char lists
                      $    / cast ($)
            (        )     / do this together
                   #x      / count (#) length of x
                  !        / range (!) 0..this count
                2*         / double it
             -1-           / subtract from -1
           $               / pad
     (    )                / do this together
        #x                 / count (#) length of x
      2*                   / multiply by 2
   a:                      / save as a
  |                        / reverse (|) it
a|                         / or (|) with a
\$\endgroup\$
1
\$\begingroup\$

Stax, 13 bytes

â┤▼iS╙£≥»u5V╓

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

{       begin block for mapping characters of input
  ]i^)  left-pad each character to length (i+1) where i is the index
m       map using block
r       reverse the array; this produces the southwest-northeast diagonal
m       map using the rest of the program, implicitly output result
  i     iteration index
  xi@   get the i'th character of the original input, where i is the iteration
  &     assign element to array at index
  0\    join with spaces

Run this one

\$\endgroup\$
1
\$\begingroup\$

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
''
&$f c o d e g o l f  # It works with word that contains even number of letters

Output for both cases:

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

s       d
  w   r
    o
  w   r
s       d

u   m
  h
u   m

o

c             f
  o         l
    d     o
      e g
      e g
    d     o
  o         l
c             f
\$\endgroup\$
  • \$\begingroup\$ Since you can take input as a char-array (Meta post), that can save some bytes. \$\endgroup\$ – AdmBorkBork Aug 20 '18 at 13:21
  • \$\begingroup\$ I'm not sure that the word in the rule print the following pattern for any given word that contains odd number of letters means an array of chars in a Powershell :) But I've updated post \$\endgroup\$ – mazzy Aug 20 '18 at 15:52
  • 1
    \$\begingroup\$ Aren't you crossing the wrong way? \$\endgroup\$ – streetster Aug 21 '18 at 7:16
  • \$\begingroup\$ @streetster, you are best! I've updated the post. \$\endgroup\$ – mazzy Aug 21 '18 at 8:27
  • 1
    \$\begingroup\$ Oof, +8 bytes - sorry! \$\endgroup\$ – streetster Aug 21 '18 at 14:36
1
\$\begingroup\$

Charcoal, 9 7 17 10 bytes

↘S⟲OOLθUE¹

+10 bytes because I missed the requirement of space-columns.
-7 bytes thanks to @ASCII-only.

Try it online (verbose) or try it only (pure).

Challenges like this is why Charcoal was made.

Explanation:

Print the input-string in a down-right direction:

Print(:DownRight, InputString());
↘S

Rotate it 90 degree counterclockwise, and shift it up the length of the input-string amount of times with overlap:

RotateOverlapOverlap(Length(q));
⟲OOLθ

Put a space gap of size 1 between every column:

Extend(1);
UE¹
\$\endgroup\$
  • 1
    \$\begingroup\$ It's not exactly diagonal. There should be an empty column between characters. \$\endgroup\$ – sampathsris Aug 17 '18 at 7:50
  • 1
    \$\begingroup\$ @Krumia Should be fixed now. \$\endgroup\$ – Kevin Cruijssen Aug 17 '18 at 8:09
  • 2
    \$\begingroup\$ @KevinCruijssen 10 \$\endgroup\$ – ASCII-only Nov 19 '18 at 23:43
  • \$\begingroup\$ @ASCII-only Thanks! Didn't knew about Extend, but it's just what was necessary here. \$\endgroup\$ – Kevin Cruijssen Nov 20 '18 at 7:36
  • \$\begingroup\$ XD > try it only \$\endgroup\$ – ASCII-only Dec 5 '18 at 11:47
1
\$\begingroup\$

R, 88 bytes

n=ncol(m<-diag(u<-utf8ToInt(scan(,""))-32));diag(m[,n:1])=u;write(intToUtf8(m+32,T),1,n)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice! Like me, you can save 2 bytes by using pmax (originally Giuseppe's suggestion). \$\endgroup\$ – Robert Hacken Nov 24 '18 at 23:10
  • 1
    \$\begingroup\$ This won't work with length 1 input. \$\endgroup\$ – Giuseppe Nov 24 '18 at 23:28
1
\$\begingroup\$

Canvas, 6 bytes

\:↶n *

Try it here!

Explanation

\       Create a diagonal with the input
 :      Duplicate the diagonal
  ↶     Rotate left
    n   Simple overlap 
      * Insert spaces on every line
\$\endgroup\$
  • \$\begingroup\$ There is already a 5-byte Canvas answer, but nice approach regardless! :) \$\endgroup\$ – Kevin Cruijssen Apr 9 '20 at 8:12
  • \$\begingroup\$ I see, the built-in is ... \$\endgroup\$ – user92069 Apr 9 '20 at 8:38
0
\$\begingroup\$

PHP, 81 bytes

for($k=strlen($s=$argn);$k;$t[2*$i]=$s[$i++],$t[2*--$k]=$s[$k],print"$t
")$t=" ";

Run as pipe with -nR or try it online.

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

Python 3, 176 bytes

w=input();l=len(w);d=l//2;n=' ';x=range(d)
for i in x:print(n*i*2+w[i],n*(l+2-i*4),w[-1*i-1])
print(n*(l-1)+w[d])
for i in x:print(n*(l-i*2-3)+w[-1*(d+i+2)],n*(i*4+1),w[d+i+1])

Try it online!

This is super stupid, any improvements that you notice are welcome

\$\endgroup\$
-1
\$\begingroup\$

C++:

#include <iostream>
#include <string>

int main()
{
    char a[] = {'P','R','O','G','R','A','M'};
    int length = sizeof(a)/sizeof(a[0]);
    int l = length - 1, i, j;
    for (i=0; i <= l; i++) {
        for (j=0; j <= l;j++) {
            if (j == i)
                std::cout<<a[i];
            else if (i == l-j)
                std::cout<<a[j];
            else
                std::cout<<"s";
        }            
        std::cout<<"\n";
    }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to PPCG! This is code-golf which means that the goal is to solve the problem in as few bytes as code as possible. As per our help centre every answer should make a reasonable stab at the given winning criterion (within the limits of their language), which would mean at least removing unnecessary whitespace and using single-character variable names. You can always include a readable/"ungolfed" version in addition to the competitive one. Once you've done this, please also include the byte count of your code in the header. \$\endgroup\$ – Martin Ender Apr 7 '16 at 9:04
  • \$\begingroup\$ And anyways, per the rules, using C++ is prohibited. \$\endgroup\$ – Zacharý Jun 23 '17 at 12:58
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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