Code Golfing Challenge

Code Golf is the art of achieving a particular challenge with as less code as possible. I'd like to see what kinds of tricks Sup Forums can do with their favorite language. The challenge is to print an ASCII "diamond" on the terminal, like so:
*
***
*****
*******
*****
***
*

My attempt in Ruby:
s=4;7.times{|r|r

Other urls found in this thread:

codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default
pastebin.com/gbMZWCxQ
twitter.com/NSFWRedditImage

s-someone?

printf " *\n ***\n *****\n*******\n *****\n ***\n *"

t-that's not fair!

C
#include
int main(void){int i,j,s=7;for(i=0;i

print " *\n***\n *"

kek

Should add width as a parameter to that challenge, or statically banging out the string really is the most straight forward solution.

Agreed, also wouldn't be a bad idea to include the 'standard loopholes' from the codegolf stackexchange. codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default

Yes, I should've done that.

Now with variable size faggot.

i did one of those when i started learning programming. im trying to post it but Sup Forums blocks it
pastebin.com/gbMZWCxQ

DELET

Basically you win every code golfing challenge with Ruby or Perl..

Come at me!
:)

(1..7).step(2){|i| a=('*'*i).center(8); puts a;Thread.new{sleep(8-i);puts(a)}if i!=7}

very nice!

T-thanks..

>leaving out the best one of these

...

Ruby-chan is cute!

I feel like there's probably a good meta joke around Elixir's macro system, but I'll settle for a cuter Erlang.

it's also fucking wrong

LMAO js

r=1;for(i=1;i>0;){console.log(" ".repeat(4-i)+"*".repeat(2*i-1));i+=!r?-1:1;if(i==4)r=null}
91 in js, kill me.

change the end to "r=0", save 3 characters

kek
Also reduced to 86.
for(r=i=1;i>0;){console.log(" ".repeat(4-i)+"*".repeat(2*i-1));i+=!r?-1:1;if(i>3)r=!1}

someone post lewds of C#-waifu

Thank you. Saves me another one from

Actually, check it out:

for(r=i=1;i>0;){console.log(" ".repeat(4-i)+"*".repeat(2*i-1));i+=r;if(i>3)r=-1}


Down to 80!

Nice

unfortunately I only have these

...

print (lambda x: "\n".join(("*"*i).center(x*2) for i in range(1,x*2,2)+range(x*2-x+1,0,-2)))(4)

small mistake, now it works with values other than 4
print (lambda x: "\n".join(("*"*i).center(x*2) for i in range(1,x*2,2)+range(x*2-3,0,-2)))(20)

why is this board full of brainlets
s=int(input())

for i in range(1,2*s):
for j in range(1, 2*s):
if(abs(i-s)+abs(j-s)>=s):
print(' ',end='')
else:
print('*',end='')
print()

A Ruby/Erlang hybrid more specifically.

91 again but takes actual values now:
f=(s)=>{for(r=i=1;i>0;){console.log(" ".repeat(s-i)+"*".repeat(2*i-1));i+=r;if(i>s-1)r=-1}}

no java? :( i wanna see what people would do for java

I love how Rust has two shields.

swank

#include
#include
int main(){int s,i,j;scanf("%d",&s);for(i=1;i

f=s=>{for(r=i=1;i>0;){console.log(" ".repeat(s-i)+"*".repeat(2*i-1));i+=r;if(i>s-1)r=-1}}


you can drop the parenthesis in an arrow function if there's exactly one argument

You mean APL, J or one of those specialized golfing languages.

>java
heresy! bring the pitchforks, this worm dared to proliferate unwholly words inside the Saint IGNUcius altar

Lua version. Could shave off some characters by not making it a function.
function d(w)o="*";r=o:rep(w);s="";for i=2,w,2 do s=s.." ";t=s..o:rep(w-i);r=t.."\n"..r.."\n"..t;end;print(r)end
d(7)
d(3)
d(1)
*
***
*****
*******
*****
***
*
*
***
*
*

java
System.out.println(" *\r\n" +
" ***\r\n" +
" *****\r\n" +
"*******\r\n" +
" *****\r\n" +
" ***");

Good luck, I hope you have one of those special keyboards with moon runes, eh?

J looks cool, though.

python, only because I had my IDE open
h=10
def a(s,l):
x=s if l!=0 else ''
for i in range(l-1):
x+=s
return x
for i in range(h*2-1):
x=h-i-1 if i

100 characters
lambda n:print("\n".join(' '*(n-j)+'*'*(j*2-1) for i in (range(1,n+1),range(n-1,0,-1)) for j in i))

Fiddled with it a bit more, saved four characters.
function d(w)o="*"r=o:rep(w)for i=2,w,2 do t=(" "):rep(i/2)..o:rep(w-i)r=t.."\n"..r.."\n"..t;end;print(r)end

test
test

Sup Forums does not like my 199 char java8 solution, have a screenshot instead..

68 characters
$n=;print map{$x=abs($n-$_);" "x$x."*"x(($n-$x)*2+1)."\n"}0..$n*2

updated
h=9
for i in range(h*2):
x=h-i if i

J:
h=:' * *** ***** ***'
7 7$h,'*',|.h

42 characters in total

whoa.. so this is true power

oof, missed an optimization:
h=:' * *** ***** ****'
7 7$h,}.|.h

41

Wait, is this parameterized by the diamond size?

if you mean what i think you mean, no, it doesn't generalize

...

>printf " *\n ***\n *****\n*******\n *****\n ***\n *"
>52 chars
Get fucked:

Lame.

Converted to Ruby - 61 chars
x=9;for i in 1..x*2do;y=i

Use (x-i).abs.

whoa
x=9;for i in 1..x*2do;y=(x-i).abs;puts' '*y+'*'*(x-y)*2end


56 chars

++++[>++++++++++++>++++++++++++............

I couldn't be assed to learn how Java strings manipulation work.

int currentPosition = 1;
boolean limitReached = false;

for (int i = 0; i < 7; i++)
{
for (int j = 0; j < currentPosition; j++)
{
System.out.printf("*");

if (currentPosition >= 7)
{
limitReached = true;
}
}

if (limitReached == true)
{
currentPosition = currentPosition - 2;
}

else
{
currentPosition = currentPosition + 2;
}

System.out.println("");

Behold! The least comprehensible code of all time!
(|.|:(' ','*',^:(1+i.2)''),|.' ','*',^:(1+i.3)''),|:('*',^:(1+i.3)''),|.'*',^:(1+i.4)''

87 characters

it also generalizes, at the cost of a few more characters:
d =: 3 : 0
f =.

the fuck is this language?

Can we also see who can do this trying to use as many characters as they can?
I need a good laugh

Now we just need one for pony.

Meant to also quote since Pony can have even more processes than Erlang/Elixir.

It will automatically garbage collect processes that are waiting for a message but will never recieve one, I guess that could be a source of inspiration.

Sup Forumsod tier

That image makes me want to learn Erlang

I want to say awk

import diamond
print(diamond(n))

python3
for y,x in zip([3,2,1,0,1,2,3],[1,3,5,7,5,3,1]):print(' '*y+'*'*x)

66
:^(

its not werf it
tantalizes you with possibility but ultimately the ecosystem is slumping towards disrepair slowly but surely

>Come with me if you want to research
But I use Ruby and Python for that...

Three. You forgot the one on its back.

Shit presentation, but R:

s=4;z=matrix(" ",2*s-1,2*s-1);z[abs(row(z)-s)+abs(col(z)-s)

Not that great, but meh, I didn't want to spend all that long on it
f x=sequence$putStrLn((reverse$g$x-2)++g x)
where
g (-1)=[]
g y=(replicate(quot(x-y)2)' '++replicate y '*'):g(y-2)

Wait, fuck, there should be an _ after sequence

++++++++++++++++++++++++++++++++...++++++++++.----------...----------------------.----------
++++++++++++++++++++++++++++++++..++++++++++...----------..----------------------.----------
++++++++++++++++++++++++++++++++.++++++++++.....----------.----------------------.----------
++++++++++++++++++++++++++++++++++++++++++.......--------------------------------.----------
++++++++++++++++++++++++++++++++.++++++++++.....----------.----------------------.----------
++++++++++++++++++++++++++++++++..++++++++++...----------..----------------------.----------
++++++++++++++++++++++++++++++++...++++++++++.----------...----------------------.----------

J

main(i)
{
i = (i-1)?i:0x1ffff03;
char* s = &i;
for(;*s!=s[1];(*s)+=s[2])
printf("%*s\n",7-*s,*s+(short*)"*******");
s[3] && main(0x10401);
}


works on my machine

Mix up Erlang, C++ and Rust.

The ecosystem is growing. The library quality is as good as ever.

I'd recommend taking a look at Elixir instead. Get all the benefits of BEAM and OTP, plus a bitchin' macro system (macros are themselves written in Elixir and work on the parsed source code, not the plain-text representation) that cuts out 95% of the obscene amount of boilerplate code that Erlang requires.

Neither Erlang nor Elixir have much of a job market at the moment, but I've been studying Elixir in my spare time the past couple of months and I really think it's poised to blow up in the next few years as people realize that it's much more than just "Erlang that looks like Ruby".

You'll still need to learn Erlang to use Elixir seriously because you'll need to read the docs for Erlang/OTP itself and for its libraries.

what the fuck

Fair enough. I've been slowly working through "Elixir in Action" and while the author strongly recommends studying Erlang, I am definitely not at the "using Elixir seriously" level so it hasn't been much of an issue for me yet.

you should check out the IOCCC

It's not perfect, but here's my brainfuck thing:
+++++[>+++++ +++<
+++++ ++[>>+++++ +................<

.>.........

Read Programming Erlang next. It's a really good programming book.

thanks for the recommendation!

thread status: REKT

#define N 11
char a[N] = {[0 ... N-1] = '*'};
__attribute__((destructor)) main(i,c)
{
char* s = ((i = c?0xffff04:0x10501),&i);
for(s[!c]=N/2+!c;*s!=s[1];*s+=s[2])
printf("%*s\n",N-*s,*s+(short*)a);
}

supports different sized diamonds

Recursion isn't even idiomatic CL.