Onion Programming
Python 2 - N = 17, 53 characters
Oh I love source-layout challenges with Python...
i=4 ;
ii=3 ;
iii=2 ;
iiii=1 ;
iiiii=0;R=raw_input ;
iiiii;w=R().split() ;
iiiii;n=map(int,w) ;
iiiii;S=set(n);M=max ;
iiiii;s=sorted(n) ;
iiiii;J="\n".join ;
iiiii;j=" ".join ;
iiiii;k=M(map(abs,n)) ;
iiiii;A=J(["CORE"]*k) ;
iiiii;B=sum(n)/len(n) ;
iiiii;C=j(w[::-1]) ;
iiiii;D=j(map(str,s)) ;
iiiii;E=j(map(str,S)) ;
iiiii;P=A,B,C,D,E ;
iiiii;print P[i] ;
iiiii;" /__----__\ " ;
iiiii;"|/ (')(') \| " ;
iiii;" \ __ / " ;
iii;" ,'--__--'. " ;
ii;" / :| \ " ;
i;" (_) :| (_) ";
There's still some unused whitespace, though.
I could still improve the unique character count, but I'll stick with better readability - if there is any at all.
Edit: Oh, it's Stan again!
CJam, N = 5, 27 (26) unique characters
It's 26 characters if I don't count the spaces. The program could actually be converted to one that doesn't use spaces, by just filling up all empty spaces with no-ops (e.g. _;
which duplicates the top stack element and then discards, or by sorting the array again and again), but it would just distract from the actual code.
l~]_|S*
{l~]$S*
{l~]W%S*
{l~]_,\
{l~]{z
}%$W=
"CORE
"*
}
;:+d\/ }
; }
; }
;
Test it here.
The core is
l~]{z
}%$W=
"CORE
"*
(Plus an empty line.)
I'm fairly sure that N = 4
can't be done in CJam (and I'm sure Dennis will convince me otherwise :D). The above has 17 characters, and while it might be possible to get it down to 16 (e.g. if CJam didn't have a bug to choke on :z
, which requires {z}%
, or by using ARGV), I don't think you can fit it in the layout without introducing a line break within CORE
.
All of the implementations are very straightforward solutions to the given tasks. All of them start with l~]
which reads STDIN, evaluates it, and puts it in an array.
The previous layer is always surrounded in {...}
, which makes it a block that isn't automatically executed. And instead of executing it, I just discard it from the stack with ;
, so no layer depends on code in the previous layer. In the Layer 1, the code didn't fit into the first line, so I continued it after discarding the core block.
Now for the actual programs:
Core:
{z}%$W="CORE "*
Map
abs
onto the list, sort it, take the last element, repeatCORE
(and a line break) that many times.Layer 1:
_,\:+d\/
Duplicate the list, take the length, swap the stack elements, get the sum, cast to
double
, swap the stack elements, divide. I think this can be shorter, but there's no incentive to do so.Layer 2:
W%S*
Reverse the array, riffle with spaces.
Layer 3:
$S*
Sort the array, riffle with spaces.
Layer 4:
Duplicate, take set union, riffle with spaces.
Some other optimisations are also possible, like reusing the ;
and *S
of Layer 2, but again, but it doesn't affect the score.
Python 3: N=11, 40 distinct characters
if 1:
if 1:
if 1:
if 1:
p=print;R=0
a=input()
b=a.split()
m=map;a=abs
E=max;l=len
n=m(int,b);
C=['CORE']
"R=E(m(a,n))"
OO=C*R;s=sum
"x='\n'.join"
"p(x(O)) "
"p(s(n)/l(b)) "
"p(*b[::-1]) "
"p(*sorted(n)) "
p(*set(n))
Thanks to @Falko for being my muse. This works, because Python does not create a new scope for each if statement, so the variables persist in the outer print
statements. One annoying thing was that a map
object (in our case n
) can be used only once. So it was necessary to string out the R=E(...)
line, but then R
was not defined. Therefore I was lucky that there were four spaces left in the first line!
The output can be solved by providing multiple elements *b[::-1]
instead of the list. The alternative ' '.join(...)
would have been too long.