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

43 Answers 43

1
2
1
\$\begingroup\$

C# 208

static void Main()
{
string s=Console.ReadLine(),t="";
int n=s.Length;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)t+=s[i];
else if(i==n-j-1)t+=s[n-i-1];
t+=" ";
}
t+="\n";
}
Console.WriteLine(t);
}
\$\endgroup\$
2
  • \$\begingroup\$ Save 11 precious characters with t+=i==j?s[i]:i==n-j-1?s[n-i-1]:"";. \$\endgroup\$ Oct 29, 2014 at 11:22
  • 2
    \$\begingroup\$ Can you remove those newlines? \$\endgroup\$
    – Adalynn
    Jun 23, 2017 at 12:53
1
\$\begingroup\$

GolfScript 46 (DEMO)

:w,:l,{l,{.[.l(\-]2$?)!w@[=32]=}%\;''+' '*}%n*
\$\endgroup\$
1
\$\begingroup\$

No matter how long, there's always gotta be an answer in...

Java - 289 234 bytes

public static void main(String[]a){int l=a.length-1,i=0,j;for(;i<=l;i++){for(j=0;j<=l;j++){if(j==i)System.out.print(a[i]);else if(i==l-j)System.out.print(a[j]);else System.out.print(" ");System.out.print(" ");}System.out.println();}}}

Ungolfed:

    class A {

    public static void main(String[] a) {
        int l = a.length - 1, i = 0, j;
        for (; i <= l; i++) {
            for (j=0; j <= l;j++) {
                if (j == i)
                    System.out.print(a[i]);
                else if (i == l-j)
                    System.out.print(a[j]);
                else
                    System.out.print(" ");
                System.out.print(" ");
                }            
            System.out.println();
        }
    }
}

Output, lousily done, is:

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

Added the import java.util.Scanner inside the code because I never remember if the imports count towards the byte count... Damn, I really suck at this.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Imports do count. This is partly because of Python's import-and-alias syntax: from math import floor as f which is a bit cheaty \$\endgroup\$
    – user16402
    Oct 29, 2014 at 15:54
  • \$\begingroup\$ You should be able to save a bunch of characters by combining all the System.out.print calls into one, using a couple of ternary operators. \$\endgroup\$
    – DLosc
    Oct 30, 2014 at 16:29
  • \$\begingroup\$ @DLosc Could you give me a few examples? \$\endgroup\$ Oct 30, 2014 at 18:28
  • 1
    \$\begingroup\$ Yes, actually Geobits' answer is a perfect example--see the contents of the System.out.print call at the end. \$\endgroup\$
    – DLosc
    Oct 31, 2014 at 0:13
  • 1
    \$\begingroup\$ @RodolfoDias Don't feel that way. My first few golfs in Java were terrible, and I can still normally shave a decent chunk off my "first revisions" if I look hard enough ;) \$\endgroup\$
    – Geobits
    Oct 31, 2014 at 13:28
1
\$\begingroup\$

C# (192 / 170)

using System;class P{static void Main(){var s=Console.ReadLine();int x,y,l=s.Length;for(x=0;x<l;x++){for(y=0;y<l;y++)Console.Write(x==y||l-x==y+1?s.Substring(x,1):" ");Console.Write("\n");};}}

Or, as "Main() only":

static void Main(){var s=Console.ReadLine();int x,y,l=s.Length;for(x=0;x<l;x++){for(y=0;y<l;y++)Console.Write(x==y||l-x==y+1?s.Substring(x,1):" ");Console.Write("\n");};}
\$\endgroup\$
1
\$\begingroup\$

QBasic, 91 bytes

INPUT w$
l=LEN(w$)
FOR i=1TO l
c$=MID$(w$,i,1)
LOCATE i+1,i*2
?c$
LOCATE l-i+2,i*2
?c$
NEXT

Loops through the input word and uses the LOCATE statement to place each letter (twice) on the screen.

\$\endgroup\$
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\$

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

TI-Basic, 75 bytes

Input Str1
length(Str1→L
For(I,1,L
" 
For(J,1,L
If max(J={I,1-I+L:Then
Ans+sub(Str1,J,1)+" 
Else
Ans+"  
End
End
Disp Ans
End

There is 1 space at the end of lines 4 and 7, and there are 2 spaces at the end of line 9. Outputs a leading space at the beginning of each line.


Alternatively, a larger but faster method (100 bytes):

Input Str1
length(Str1→L
" 
For(I,1,L
Ans+"  →Str2
End
For(I,1,L
min(I,1-I+L→K
2(max(L,2)-Ans
Disp sub(Str2+sub(Str1,K,1)+Str2,Ans+4,Ans)+sub(Str1,L-K+1,1
End

There is 1 space at the end of line 3. Outputs two leading spaces if the input has a length of 1.

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

Jelly, 10 bytes

ṪṭaɗƤ⁶»Ṛ$G

Try it online!

Erik's existing answer ties with minor modifications to also use G: LḶ⁶ẋ;"µ»ṚG.

    Ƥ         For each prefix of the input,
Ṫ             remove the last element
 ṭ            and append it back onto
  aɗ ⁶        the remaining elements replaced with spaces.
      »       Take the maximum of each element of that and
       Ṛ$     its reverse.
         G    Grid format: join each row on spaces then join the rows on newlines.
\$\endgroup\$
-2
\$\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
  • 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\$ Apr 7, 2016 at 9:04
  • \$\begingroup\$ And anyways, per the rules, using C++ is prohibited. \$\endgroup\$
    – Adalynn
    Jun 23, 2017 at 12:58
1
2

Your Answer

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

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