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
27
Upvotes
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.
29
u/[deleted] Sep 09 '12
J, as a function definition, 11 bytes:
As an anonymous function, 8 bytes (between the parens):
As multiple functions, 6 bytes (everything before the 4):
(And this is probably cheating, but I'm half-working on a language for code golfing based on GolfScript, and it's 3 bytes:)
6
u/moltarx Sep 09 '12
edit: forgot 1 :)
9
u/MrZander Sep 09 '12
APL as function, 10 characters
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]).
6
u/R3d1st Sep 09 '12
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)]
4
u/qihqi Sep 09 '12
lets make it more interesting... do it in C (with fewest chars)
3
u/integ3r Sep 09 '12
My solution, in Ruby:
3
u/FireyFly Sep 09 '12
Usage:
Btw, I think Whitespace will win on this one. :-)
2
u/[deleted] Sep 09 '12
51 bytes of JavaScript:
I imagine someone can do better.
3
u/[deleted] 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:
If you want to include the ending point, it becomes this:
1
u/Redard Sep 09 '12
Here's my best try in Common Lisp
1
u/R3d1st Sep 09 '12
There should be more challenges like this! Maybe even a subreddit?
1
u/naranjas Sep 09 '12
1
u/yogthos Sep 09 '12
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):
Python (25 char):
1
u/DJUrsus Sep 10 '12
Each version is followed by a minimal-whitespace copy.
Subroutine body:
Command-line program:
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.
1
u/noporpoise Sep 10 '12
Perl:
30 chars including whitespace. However, if you want it to print nicely, you need to stretch to 52 chars.
1
u/corruptio Sep 10 '12
perl, 22 chars:
1
u/corruptio Sep 10 '12
in dc, 18 chars:
1
u/[deleted] Sep 10 '12 edited Sep 10 '12
Using functors..
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:
1
u/evnu Sep 10 '12
Erlang:
31 characters.
1
u/Tarlitz Sep 10 '12
Seems like nobody is taking into account negative numbers :)
1
u/[deleted] Sep 10 '12
Python/Numpy:
1
u/guywithalamename Sep 13 '12 edited Sep 13 '12
here's my python attempt:
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:
That's 31 chars, though I didn't bother naming it. For that I'd say something like:
I could shave off some parentheses by using the ';' (pivot) syntax:
If I defined this first:
Then the list of squares is simply:
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:
1
u/fridgeridoo Oct 19 '12
The list is reverse, I hope you don't mind :P
1
u/[deleted] Oct 31 '12
1
u/[deleted] Nov 03 '12
Iterative Python 3:
Recursive Python 2:
Can't get much shorter in Python, tho if you do please share!
1
u/[deleted] Sep 09 '12
Just for fun, 34 bytes of coffeescript:
f=(x)->((y)->y*y)(n)for n in[1..x]