r/tinycode • u/davelol6 • Sep 09 '12
Tinycode challenge!
Here is a challenge. Write a function that returns a list of the squares of all the numbers between one and a given number. See if you can beat my 48-char (exc. whitespace) 3-line beast:
def f(n): l = [] while len(l) <= n: l.append(x**2) print l
29
Sep 09 '12
J, as a function definition, 11 bytes:
f=.*:@>:@i.
f 4
1 4 9 16As an anonymous function, 8 bytes (between the parens):
(*:@>:@i.) 4
1 4 9 16As multiple functions, 6 bytes (everything before the 4):
*:>:i.4
1 4 9 16(And this is probably cheating, but I'm half-working on a language for code golfing based on GolfScript, and it's 3 bytes:)
75 5F FE6
u/moltarx Sep 09 '12
f n = 1 : [ i * i | i <- [2..n] ]edit: forgot 1 :)
2
Sep 09 '12
19 characters. booyah.
f n=[i*i|i<-[1..n]]7
Sep 09 '12
17 characters, same type.
f n=map(^2)[1..n]2
u/R3d1st Sep 09 '12
crap, just came up with the same solution, bevor I saw your post... It's even 16, if you just count the chars, and exclude the whitespaces.
3
1
0
Sep 09 '12
[deleted]
4
Sep 09 '12 edited Sep 09 '12
Enjoy your list of even numbers.
-edit- The type of
i ** 2is too restrictive.i ^ 2andi * iwill do just fine.2
1
9
u/MrZander Sep 09 '12
APL as function, 10 characters
f←{(⍳⍵)*2}Use: f 5
Result: 1 4 9 16 25
You will probably need the APL font to see this properly, the 2 squares are iota and omega
7
u/MrQuimico Sep 09 '12
Using ISO Prolog:
p(N,L) :- (N=1,L=[1]);(N>1,N1 is N-1,N2 is N*N,p(N1,Ls),L = [N2|Ls]).
4
3
1
u/sleepingsquirrel Sep 12 '12
Try:
f(N,L) :- bagof(Y, X^(between(1,N,X), Y is X**2), L).or:
f(N,L) :- bagof(X**2,between(1,N,X),Xs),maplist(is,L,Xs).
6
u/R3d1st Sep 09 '12
f n = map (^2) [1..n]2
Sep 10 '12
This pleases me. Very aesthetic. Is this Haskell?
1
u/R3d1st Sep 10 '12
yes, but after I committed this entry, Tinctorious apparently already had the same solution :-)
4
u/sordid_salmon Sep 09 '12
As long as we're cheating anyway: def f(n): return [x**2 for x in range(1,n)]
1
0
Sep 09 '12
[deleted]
1
u/sordid_salmon Sep 09 '12
def f(n): return [x*x for x in range(n)]*oops, too fast on the trigger there3
u/ilogik Sep 09 '12
f=lambda n:[x*x for x in range(1,n)]
-2
4
u/qihqi Sep 09 '12
lets make it more interesting... do it in C (with fewest chars)
8
u/qihqi Sep 09 '12
void f(int * A,int n){while(n--&&A[n]=n*n);}
7
4
-1
u/14113 Sep 09 '12
with for loop:
void s(int i,int * A){for(;i>0;i--,A[j]=i*i);}
not sure if it's longer, or even works...
1
u/Noctune Sep 10 '12
Tried to make a recursive one. It's not as short as the iterative ones though.
f(int*a,int n){!(*a=n*n)||f(++a,--n);}1
u/qihqi Sep 11 '12
very nice!
f(int*a,int n){(a=nn)&&f(++a,--n);} is the same tho saving one more char
3
u/integ3r Sep 09 '12
My solution, in Ruby:
->(x){Array.new(x){|y|(y+1)**2}}2
2
u/_jeffJohnsonsBookEmp Sep 10 '12 edited Sep 10 '12
shaving off 1 char at a time
->(x){x.times.map{|i|(i+1)**2}}EDIT: 28 chars
->(x){1.upto(x).map{|i|i*i}}1
3
u/FireyFly Sep 09 '12
f=: *: @ >: @ i.Usage:
f 8
1 4 9 16 25 36 49 64Btw, I think Whitespace will win on this one. :-)
2
Sep 09 '12
51 bytes of JavaScript:
function f(x){z=[];while(x^1)z[--x-1]=x*x;return z}I imagine someone can do better.
1
Sep 10 '12
47:
function f(x){for(z=[];z[~-x]=x*x--;);return z}And 43 that returns a function instead of an array so it's probably not allowed (use it like f(5)[1] to run it on the number 5 and get the 2nd square):
function f(x){for(;f[~-x]=x*x--;);return f}
3
Sep 09 '12 edited Sep 09 '12
Unless I'm mistaken, your function doesn't return the list of squares, it prints it. Therefore I win by disqualification.
Edit: Great idea, by the way. These are fun.
1
u/JeffAMcGee Sep 09 '12
I was going to give a python answer that looked like ilogik's. Instead, I'll try to remember perl:
sub f{map $_**2,1..$_[0]}If you want to include the ending point, it becomes this:
sub f{map $_**2,1..$_[0]+1}1
u/Redard Sep 09 '12
Here's my best try in Common Lisp
(defun f (N)
(mapcar #'(lambda (x) (expt x 2)
(loop for i from 1 to N collect i)))2
u/philh Sep 10 '12
(defun f (n) (loop for i from 1 to n collect (expt i 2)))(You can save another three chars by removing spaces before open parens.)
1
u/Redard Sep 10 '12
Nice job shortening it. Never thought of applying a function to collect's argument.
2
1
u/R3d1st Sep 09 '12
There should be more challenges like this! Maybe even a subreddit?
1
1
1
1
1
u/gatesphere Sep 10 '12
In Io: x := method(n,1 to(n) map(x,x*x)) 29 non-whitespace characters.
Io> x(20) ==> list(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400)
1
u/gcr Sep 10 '12
Racket (32 char):
(for/list ([i 10]) (sqr (+ 1 i)))Python (25 char):
[x*x for x in range(1,10)]1
u/yogthos Sep 10 '12
I believe the challenge calls for a callable function which accepts the range as an input, so Scheme would be a tad longer:
(define (f n) (for/list ([i n]) (sqr (+ 1 i))))1
u/gcr Sep 11 '12
Actually, it just said to write a function, not necessarily to bind it to a variable. You could do a little shorter:
(λ(n)(for/list([i n])(sqr(+ 1 i))))1
u/recursive Sep 10 '12
I don't know racket, but your python example does not result in a named callable function.
1
1
u/DJUrsus Sep 10 '12
Each version is followed by a minimal-whitespace copy.
Subroutine body:
print $_ * $_, "\n" for 1 .. $_[0];
print$_*$_,"\n"for 1..$_[0];Command-line program:
print $_ * $_, "\n" for 1 .. $ARGV[0];
print$_*$_,"\n"for 1..$ARGV[0];1
u/recursive Sep 10 '12
If I'm not mistaken, that can not possibly work because x is not defined. In any case, here's 36 bytes (inlcluding whitespace) of python that actually works.
f=lambda n:[~d*~d for d in range(n)]1
u/noporpoise Sep 10 '12
Perl:
sub f{ map {$_**2} 1..$_[0]; }30 chars including whitespace. However, if you want it to print nicely, you need to stretch to 52 chars.
sub f{ print join(" ", map {$_**2} 1..$_[0])."\n"; }1
1
1
Sep 10 '12 edited Sep 10 '12
Using functors..
g n = fmap (^2) [1..n]edit: looks like someone has already submitted this fairly trivial case before me.. :)
1
u/nohtyp Sep 10 '12 edited Oct 15 '12
In Factor:
: range-square ( limit -- squares )
1 - 1 swap 1 <range> [ dup * ] map1
1
1
u/Tarlitz Sep 10 '12
Seems like nobody is taking into account negative numbers :)
def f(n):
return [x*x for x in range(2 if n > 0 else 0,abs(n))]1
1
u/guywithalamename Sep 13 '12 edited Sep 13 '12
here's my python attempt:
def f(n):
print [x*x for x in range(1,n+1)]core "functionality" has 27 chars, including spaces :)
edit: didn't notice this post is already some days old. my attempt has been posted multiple times already x)
1
u/fexl Sep 23 '12 edited Sep 23 '12
In Fexl (http://fexl.com) the function is:
(\n map (\x * x x) (range 1 n))That's 31 chars, though I didn't bother naming it. For that I'd say something like:
\f = (\n map (\x * x x) (range 1 n))I could shave off some parentheses by using the ';' (pivot) syntax:
(\n map (\x * x x); range 1 n)If I defined this first:
\square = (\x * x x)Then the list of squares is simply:
(\n map square; range 1 n)In contests such as this, I'd much rather see a "token" count rather than a character count. For example, I could delete the single space after the ';' there, saving a whole whopping byte, but to me it's token complexity that really counts.
Oh and if you actually want to see the squares from 1 to 20, you could say:
do (f 20) \x print x;nl;1
u/fridgeridoo Oct 19 '12
def f(n) a=[]; n.times { a<<n**2;n-=1 }; a endThe list is reverse, I hope you don't mind :P
f(5)
-> [25, 16, 9, 4, 1]1
1
Nov 03 '12
Iterative Python 3:
def f(x):[print(x*x)for x in range(1,x+1)]Recursive Python 2:
def f(x):print((x>1)and f(x-1)or 0)+x*x,Can't get much shorter in Python, tho if you do please share!
1
Sep 09 '12
Just for fun, 34 bytes of coffeescript:
f=(x)->((y)->y*y)(n)for n in[1..x]
1
0
Sep 09 '12
[deleted]
2
1
7
u/davelol6 Sep 09 '12
Never dreamed that this would be that popular. My dad managed it in VB in 28 chars, minus function definitions and all that crap.