Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Want to write a program in minimum number of code,all programming language accepted Runtime criteria also challanged

Example:

Input : abcdef
Output: a b c d e f
        ab cd ef
        abc def
        abcd ef
        abcde f
        abcdef
        abcde f
        abcd ef
        abc def
        ab cd ef
        a b c d e f
share|improve this question
1  
Why were the tags fastest-code and functional-programming added? The former conflicts with the winning condition given already (which is shortest code) and the latter doesn't apply terribly much... –  FireFly Feb 12 at 22:40

17 Answers 17

Ruby, 84 81 characters

i=gets.chop
puts (a=(1..i.size).map{|n|i.scan(/.{0,#{n}}/)*' '})+a.reverse[1..-1]

The magic is in the regex: i.scan(/.{0,#{n}}/). Since Ruby does regex interpolation too, it will become

i.scan(/.{0,1}/)
i.scan(/.{0,2}/)
...

This means it will grab as many characters as it can, limited to n. n progressively gets bigger, and is used to generate the first half of the output.

Then I just call reverse to generate the second half. I had to use [1..-1] in order to avoid repeating the middle line (what that does is basically chop off the first element of the array).

Sample run:

c:\a\ruby>spacesplitgolf.rb
CodeGolfStackExchange         // (this is the input)
C o d e G o l f S t a c k E x c h a n g e
Co de Go lf St ac kE xc ha ng e
Cod eGo lfS tac kEx cha nge
Code Golf Stac kExc hang e
CodeG olfSt ackEx chang e
CodeGo lfStac kExcha nge
CodeGol fStackE xchange
CodeGolf StackExc hange
CodeGolfS tackExcha nge
CodeGolfSt ackExchang e
CodeGolfSta ckExchange
CodeGolfStac kExchange
CodeGolfStack Exchange
CodeGolfStackE xchange
CodeGolfStackEx change
CodeGolfStackExc hange
CodeGolfStackExch ange
CodeGolfStackExcha nge
CodeGolfStackExchan ge
CodeGolfStackExchang e
CodeGolfStackExchange
CodeGolfStackExchang e
CodeGolfStackExchan ge
CodeGolfStackExcha nge
CodeGolfStackExch ange
CodeGolfStackExc hange
CodeGolfStackEx change
CodeGolfStackE xchange
CodeGolfStack Exchange
CodeGolfStac kExchange
CodeGolfSta ckExchange
CodeGolfSt ackExchang e
CodeGolfS tackExcha nge
CodeGolf StackExc hange
CodeGol fStackE xchange
CodeGo lfStac kExcha nge
CodeG olfSt ackEx chang e
Code Golf Stac kExc hang e
Cod eGo lfS tac kEx cha nge
Co de Go lf St ac kE xc ha ng e
C o d e G o l f S t a c k E x c h a n g e
share|improve this answer
    
4th line contain single and double string Co de G ol f St ac k Ex ch an ge-this is not correct...correct is Co de Go lf St ac kE xc ha ng e –  Boopathi Feb 11 at 13:12
    
@Boopathi That's because the spaces in the string count as letters too... I've edited the example to use an input with no spaces. –  Doorknob 冰 Feb 11 at 13:15

GolfScript, 22 characters

:I,,{)I/' '*n+}%.-1%1>

Try the example here.

> abcdef
a b c d e f
ab cd ef
abc def
abcd ef
abcde f
abcdef
abcde f
abcd ef
abc def
ab cd ef
a b c d e f

Code explained:

:I          # Save input to variable I
,,          # Create array [0..n)
{
  )         # Increment loop variable by 1
  I/        # Split input into chunks with as many characters
  ' '*      # Join with spaces
  n+        # Append newline
}%
.-1%1>      # Create reverse copy (minus first element)
share|improve this answer

Smalltalk (Smalltalk/X) (137 chars)

s := Stdin nextLine.
(1 to:s size),(s size-1 downTo:1) do:[:n |
 ((s asCollectionOfSubCollectionsOfSize:n) asStringWith:' ') printNL] 

output (first line is input):

abcdef
a b c d e f
ab cd ef
abc def
abcd ef
abcde f
abcdef
abcde f
abcd ef
abc def
ab cd ef
a b c d e f
share|improve this answer
1  
That is quite an impressive score when it includes identifiers such as asCollectionOfSubCollectionsOfSize... –  FireFly Feb 12 at 22:42
    
that's right, smalltalk was designed to be more readable than writable - bad for this kind of contest... –  blabla999 Feb 13 at 17:44

Python 94 (bit verbose, but that;s Python)

s=raw_input();print'\n'.join(' '.join(map(''.join,zip(*[iter(s)]*n)))for n in range(1,len(s)))
share|improve this answer

Delphi XE3 - WayToMany characters (WayToMany==321 261 245)

Im sure I can get this shorter so consider this as Work In Progress ^.^

Edits:
Removed use of array making 2 units redundant saving a total of 61 chars
Removed useless begin..end for loop saving 16 chars

{$APPTYPE CONSOLE}uses idglobal;var i,st:int16;s,r:string;p:boolean;begin readln(s);st:=1;p:=true;repeat r:='';for I:=1to Length(s)do r:=r+iif(i mod st=0,s[i]+' ',s[i]);writeln(r);if st=length(s)then p:=false;st:=iif(p,st+1,st-1);until st<1;end.

With indent

{$APPTYPE CONSOLE}
uses idglobal;
var
i,st:int16;
s,r:string;
p:boolean;
begin
readln(s);
st:=1;
p:=true;
repeat
  r:='';
  for I:=1to Length(s)do
    r:=r+iif(i mod st=0,s[i]+' ',s[i]);
  writeln(r);
  if st=length(s)then
    p:=false;
  st:=iif(p,st+1,st-1);
until st<1;
end.

Result: (first line is input) enter image description here

share|improve this answer

Rebol (144 chars)

s: input o:[]repeat i(length? s) - 1[t: copy s forskip t i + 1[insert t" "]append o next t]append o reverse append copy o s forall o[print o/1]


Un-golfed:

s: input
o: []

repeat i (length? s) - 1 [
    t: copy s
    forskip t i + 1 [insert t " "]
    append o next t
]

append o reverse append copy o s

forall o [print o/1]


Usage example:

rebol script.reb <<< "abcdef"
a b c d e f
ab cd ef
abc def
abcd ef
abcde f
abcdef
abcde f
abcd ef
abc def
ab cd ef
a b c d e f
share|improve this answer

R, 95

x=scan(,"");z=1:nchar(x);for(i in c(z,rev(z)[-1]))cat(gsub(paste0("(.{",i,"})"),"\\1 ",x),"\n")

(based on regular expressions)

Example:

> x=scan(,"");z=1:nchar(x);for(i in c(z,rev(z)[-1]))cat(gsub(paste0("(.{",i,"})"),"\\1 ",x),"\n")
1: abcdef
2: 
Read 1 item
a b c d e f  
ab cd ef  
abc def  
abcd ef 
abcde f 
abcdef  
abcde f 
abcd ef 
abc def  
ab cd ef  
a b c d e f  
share|improve this answer

JavaScript - 242

I'm new to CodeGolf (and JS as well) and this is my first try. Any suggestions are welcome :)

function f(s){var i,j,a=s.split(''),t;for(i=1;i<=a.length;i++){t='';for(j=1;j<=a.length;j++){t+=a[j-1];if(j%i==0)t+=' ';}
console.log(t);}
for(i=a.length;i>0;i--){t='';for(j=1;j<=a.length;j++){t+=a[j-1];if(j%i==0)t+=' ';}
console.log(t);}}

Example:

>>> f('CodeGolfIsAwesome');

C o d e G o l f I s A w e s o m e
Co de Go lf Is Aw es om e
Cod eGo lfI sAw eso me
Code Golf IsAw esom e
CodeG olfIs Aweso me
CodeGo lfIsAw esome
CodeGol fIsAwes ome
CodeGolf IsAwesom e
CodeGolfI sAwesome
CodeGolfIs Awesome
CodeGolfIsA wesome
CodeGolfIsAw esome
CodeGolfIsAwe some
CodeGolfIsAwes ome
CodeGolfIsAweso me
CodeGolfIsAwesom e
CodeGolfIsAwesome
CodeGolfIsAwesom e
CodeGolfIsAweso me
CodeGolfIsAwes ome
CodeGolfIsAwe some
CodeGolfIsAw esome
CodeGolfIsA wesome
CodeGolfIs Awesome
CodeGolfI sAwesome
CodeGolf IsAwesom e
CodeGol fIsAwes ome
CodeGo lfIsAw esome
CodeG olfIs Aweso me
Code Golf IsAw esom e
Cod eGo lfI sAw eso me
Co de Go lf Is Aw es om e
C o d e G o l f I s A w e s o m e 
share|improve this answer

Perl, 77 71

Better late, than never (and since so far no Perl solution):

chomp($s=<>);say$s=~s/.{$_}/$& /gr for@a=1..length$s,reverse@a[-@a..-2]

Requires 5.014 because of r modifier. Uses 'say' feature. And

echo CodeGolfStackExchange| perl -M5.010 spaces.pl
C o d e G o l f S t a c k E x c h a n g e
Co de Go lf St ac kE xc ha ng e
Cod eGo lfS tac kEx cha nge
Code Golf Stac kExc hang e
CodeG olfSt ackEx chang e
CodeGo lfStac kExcha nge
CodeGol fStackE xchange
CodeGolf StackExc hange
CodeGolfS tackExcha nge
CodeGolfSt ackExchang e
CodeGolfSta ckExchange
CodeGolfStac kExchange
CodeGolfStack Exchange
CodeGolfStackE xchange
CodeGolfStackEx change
CodeGolfStackExc hange
CodeGolfStackExch ange
CodeGolfStackExcha nge
CodeGolfStackExchan ge
CodeGolfStackExchang e
CodeGolfStackExchange
CodeGolfStackExchang e
CodeGolfStackExchan ge
CodeGolfStackExcha nge
CodeGolfStackExch ange
CodeGolfStackExc hange
CodeGolfStackEx change
CodeGolfStackE xchange
CodeGolfStack Exchange
CodeGolfStac kExchange
CodeGolfSta ckExchange
CodeGolfSt ackExchang e
CodeGolfS tackExcha nge
CodeGolf StackExc hange
CodeGol fStackE xchange
CodeGo lfStac kExcha nge
CodeG olfSt ackEx chang e
Code Golf Stac kExc hang e
Cod eGo lfS tac kEx cha nge
Co de Go lf St ac kE xc ha ng e
C o d e G o l f S t a c k E x c h a n g e
share|improve this answer

D - 178 176

Golfed:

import std.stdio,std.range;void main(string[]a){int l=a[1].length;for(int i=1;i<=l;++i)a[1].chunks(i).join(" ").writeln;for(int i=l-1;i>0;--i)a[1].chunks(i).join(" ").writeln;}

Un-golfed:

import std.stdio, std.range;

void main( string[] a )
{   
    int l = a[1].length;

    for( int i = 1; i <= l; ++i )
        a[1].chunks( i ).join( " " ).writeln;

    for( int i = l - 1; i > 0; --i )
        a[1].chunks( i ).join( " " ).writeln;
}

Example 1:

F:\Code\D\Other>rdmd spaces.d abcdef
a b c d e f
ab cd ef
abc def
abcd ef
abcde f
abcdef
abcde f
abcd ef
abc def
ab cd ef
a b c d e f

Example 2:

F:\Code\D\Other>rdmd spaces.d CodeGolfStackExchange
C o d e G o l f S t a c k E x c h a n g e
Co de Go lf St ac kE xc ha ng e
Cod eGo lfS tac kEx cha nge
Code Golf Stac kExc hang e
CodeG olfSt ackEx chang e
CodeGo lfStac kExcha nge
CodeGol fStackE xchange
CodeGolf StackExc hange
CodeGolfS tackExcha nge
CodeGolfSt ackExchang e
CodeGolfSta ckExchange
CodeGolfStac kExchange
CodeGolfStack Exchange
CodeGolfStackE xchange
CodeGolfStackEx change
CodeGolfStackExc hange
CodeGolfStackExch ange
CodeGolfStackExcha nge
CodeGolfStackExchan ge
CodeGolfStackExchang e
CodeGolfStackExchange
CodeGolfStackExchang e
CodeGolfStackExchan ge
CodeGolfStackExcha nge
CodeGolfStackExch ange
CodeGolfStackExc hange
CodeGolfStackEx change
CodeGolfStackE xchange
CodeGolfStack Exchange
CodeGolfStac kExchange
CodeGolfSta ckExchange
CodeGolfSt ackExchang e
CodeGolfS tackExcha nge
CodeGolf StackExc hange
CodeGol fStackE xchange
CodeGo lfStac kExcha nge
CodeG olfSt ackEx chang e
Code Golf Stac kExc hang e
Cod eGo lfS tac kEx cha nge
Co de Go lf St ac kE xc ha ng e
C o d e G o l f S t a c k E x c h a n g e
share|improve this answer

k [50 chars]

{x c,1_|c:{1_,/-1,/:(y*!-_-(#x)%y)_x}[!c]'1+!c:#x}

Example

{x c,1_|c:{1_,/-1,/:(y*!-_-(#x)%y)_x}[!c]'1+!c:#x}"abcdef"

"a b c d e f"
"ab cd ef"
"abc def"
"abcd ef"
"abcde f"
"abcdef"
"abcde f"
"abcd ef"
"abc def"
"ab cd ef"
"a b c d e f"

Example 2

{x c,1_|c:{1_,/-1,/:(y*!-_-(#x)%y)_x}[!c]'1+!c:#x}"CodeGolfStackExchange"

"C o d e G o l f S t a c k E x c h a n g e"
"Co de Go lf St ac kE xc ha ng e"
"Cod eGo lfS tac kEx cha nge"
"Code Golf Stac kExc hang e"
"CodeG olfSt ackEx chang e"
"CodeGo lfStac kExcha nge"
"CodeGol fStackE xchange"
"CodeGolf StackExc hange"
"CodeGolfS tackExcha nge"
"CodeGolfSt ackExchang e"
"CodeGolfSta ckExchange"
"CodeGolfStac kExchange"
"CodeGolfStack Exchange"
"CodeGolfStackE xchange"
"CodeGolfStackEx change"
"CodeGolfStackExc hange"
"CodeGolfStackExch ange"
"CodeGolfStackExcha nge"
"CodeGolfStackExchan ge"
"CodeGolfStackExchang e"
"CodeGolfStackExchange"
"CodeGolfStackExchang e"
"CodeGolfStackExchan ge"
"CodeGolfStackExcha nge"
"CodeGolfStackExch ange"
"CodeGolfStackExc hange"
"CodeGolfStackEx change"
"CodeGolfStackE xchange"
"CodeGolfStack Exchange"
"CodeGolfStac kExchange"
"CodeGolfSta ckExchange"
"CodeGolfSt ackExchang e"
"CodeGolfS tackExcha nge"
"CodeGolf StackExc hange"
"CodeGol fStackE xchange"
"CodeGo lfStac kExcha nge"
"CodeG olfSt ackEx chang e"
"Code Golf Stac kExc hang e"
"Cod eGo lfS tac kEx cha nge"
"Co de Go lf St ac kE xc ha ng e"
"C o d e G o l f S t a c k E x c h a n g e"
share|improve this answer

C# - 182 176

using C=System.Console;class P{static void Main(string[]p){int l=p[0].Length,i=-l,j;for(;++i<l;){for(j=0;++j<=l;)C.Write(p[0][j-1]+(j%(i>0?l-i:l+i)>0?"":" "));C.WriteLine();}}}

Ungolfed:

using C=System.Console;

class P
{
    static void Main(string[] p)
    {
        int l=p[0].Length,i=-l,j;
        for (; ++i < l;)
        {
            for(j=0;++j<=l;)C.Write(p[0][j-1]+(j%(i>0?l-i:l+i)>0?"":" "));
            C.WriteLine();
        }
    }
}

Output:

D:\>program CodeGolfStackExchange
C o d e G o l f S t a c k E x c h a n g e
Co de Go lf St ac kE xc ha ng e
Cod eGo lfS tac kEx cha nge
Code Golf Stac kExc hang e
CodeG olfSt ackEx chang e
CodeGo lfStac kExcha nge
CodeGol fStackE xchange
CodeGolf StackExc hange
CodeGolfS tackExcha nge
CodeGolfSt ackExchang e
CodeGolfSta ckExchange
CodeGolfStac kExchange
CodeGolfStack Exchange
CodeGolfStackE xchange
CodeGolfStackEx change
CodeGolfStackExc hange
CodeGolfStackExch ange
CodeGolfStackExcha nge
CodeGolfStackExchan ge
CodeGolfStackExchang e
CodeGolfStackExchange
CodeGolfStackExchang e
CodeGolfStackExchan ge
CodeGolfStackExcha nge
CodeGolfStackExch ange
CodeGolfStackExc hange
CodeGolfStackEx change
CodeGolfStackE xchange
CodeGolfStack Exchange
CodeGolfStac kExchange
CodeGolfSta ckExchange
CodeGolfSt ackExchang e
CodeGolfS tackExcha nge
CodeGolf StackExc hange
CodeGol fStackE xchange
CodeGo lfStac kExcha nge
CodeG olfSt ackEx chang e
Code Golf Stac kExc hang e
Cod eGo lfS tac kEx cha nge
Co de Go lf St ac kE xc ha ng e
C o d e G o l f S t a c k E x c h a n g e

Edit: now using parameters instead of ReadLine().

share|improve this answer

JavaScript 127

function f(s) {
o=s+'\n'
for(i=s.length;--i;){
l='',j=0
while((p=s.substr(i*j++,i)))l+=p+' '
l+='\n',o=l+o+l
}
console.log(o)
}
share|improve this answer

Python - 153

a=raw_input()
k=0
b=len(a)
for i in range(b+1)+range(b-1):
 k+=1
 p=[i,b-2-i][k>b]
 s=a
 if kb+1:
    for j in a[p::p+1]:s=s.replace(j, j+" ",1)
    print s 
share|improve this answer

J - 32 char

(,&' '\(,@)"0 _~(1}.-,]-#)@i.@#)

Explained:

  • i.@# - Make a list of the integers from 0 to one less than the length of the string.
  • (-,]-#) - Take the negatives of these integers, and then append to that each of those integers minus the length again. So e.g. if the length was 6, we've now made 0 _1 _2 _3 _4 _5 and _6 _5 _4 _3 _2 _1, and appended them together.
  • 1}. - Drop that first element because it's a 0 and of no use to anyone.
  • u\(,@)"0 _~ - For each negative integer, apply the verb u on non-overlapping infixes of the string, with length the negative of the integer, and then run it into a single string. So e.g. for _2 it will split the string up into pairs of letters, apply u, then make a string back of all the new infixes.
  • ,&' ' - Append a space.

Usage:

   (,&' '\(,@)"0 _~(1}.-,]-#)@i.@#) 'abcdef'
a b c d e f
ab cd ef
abc def
abcd ef
abcde f
abcdef
abcde f
abcd ef
abc def
ab cd ef
a b c d e f
share|improve this answer

Q, 34

{a,1_(|)a:" "sv'(1+(!)(#)x)cut\:x}
share|improve this answer

C - 134 132

Maximum 8 characters of input, ignoring buffer overflow

#define L n){for(i=0;b[i];){if(i%n==0)p(32);p(b[i++]);}p(10);}
main(i){char b[9],n=1,(*p)()=putchar;gets(b);for(;b[n];++L for(;n;--L}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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