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
add comment

11 Answers

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 yesterday
 
@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 yesterday
add comment

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
add comment

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
add comment

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
add comment

Haskell 70 (48)

s(x:y)=map(x:)(s y)++map([x,' ']++)(s y)
s a=[a]
main=interact$concat.s

s recursively generates the list of all possible combinations of spaces and main just reads lines from stdin and feeds it to s. It's 48 if you allow running the code at the REPL as acceptable.

share|improve this answer
 
This doesn't print the required output. It just adds spaces at every possible position. –  svick 17 mins ago
add comment

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
add comment

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
add comment

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
add comment

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
add comment

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
add comment

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
add comment

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.