Create, sort, and print a list of 100 random ints

2019-03-09 02:12发布

What is the least amount of code you can write to create, sort (ascending), and print a list of 100 random positive integers? By least amount of code I mean characters contained in the entire source file, so get to minifying.

I'm interested in seeing the answers using any and all programming languages. Let's try to keep one answer per language, edit the previous to correct or simplify. If you can't edit, comment?

30条回答
家丑人穷心不美
2楼-- · 2019-03-09 02:56

Javascript: (via JSDB or Mozilla's Rhino used in shell mode)

x=[];for(i=0;i<100;i++){x.push((Math.random()+"").slice(-8));};x.sort();

Here's a full test run:

c:\>java org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 1 2008 03 06
js> x=[];for(i=0;i<100;i++){x.push((Math.random()+"").slice(-8));};x.sort();
01499626,02403545,02800791,03320788,05748566,07789074,08998522,09040705,09115996,09379424,10940262,11743066,13806434,14113139,14336231,14382956,15581655,16573104,20043435,21234726,21473566,22078813,22378284,22884394,24241003,25108788,25257883,26286262,28212011,29596596,32566749,33329346,33655759,34344559,34666071,35159796,35310143,37233867,37490513,37685305,37845078,38525696,38589046,40538689,41813718,43116428,43658007,43790468,43791145,43809742,44984312,45115129,47283875,47415222,47434661,54777726,55394134,55798732,55969764,56654976,58329996,59079425,59841404,60161896,60185483,60747905,63075065,69348186,69376617,69680882,70145733,70347987,72551703,73122949,73507129,73609605,73979604,75183751,82218859,83285119,85332552,85570024,85968046,86236137,86700519,86974075,87232105,87839338,88577428,90559652,90587374,90916279,90934951,94311632,94422663,94788023,96394742,97573323,98403455,99465016

edit: looks like I can shorten it a few chars by direct assignment rather than "push", and I don't need the {}s:

x=[];for(i=0;i<100;i++)x[i]=(Math.random()+"").slice(-8);x.sort();
查看更多
何必那么认真
3楼-- · 2019-03-09 02:56

groovy:

r=new Random()
List l=[]
100.times{ l << r.nextInt(1000) }
l.sort().each { println it }
查看更多
冷血范
4楼-- · 2019-03-09 02:56

plain old c-code in 167 chars:

main(){int i=100,x[i],n=i;while(i)x[--i]=rand();for(i=0;i<n;i++){int b=x[i],m=i,j=0;for(;j<n;j++)if(x[j]<x[m])m=j;x[i]=x[m];x[m]=b;}i=n;while(i)printf("%d ",x[--i]);}
查看更多
时光不老,我们不散
5楼-- · 2019-03-09 02:57

10 characters in J:

/:~100?9e9

explanation:

/:~ sorts an array (technically, applies a lists sorted permutation vector to itself)

x ? limit returns x random numbers less than limit

9e9 (9000000000) is a reasonable upper limit expressible in 3 characters. !9 (9 factorial) is smaller, but requires one less character.

查看更多
\"骚年 ilove
6楼-- · 2019-03-09 02:57

Common Lisp, int between 0 and 10000 (there is no upper bound for that, but you have to choose one).

(sort (loop repeat 100 collect (random 10000)) #'<)
查看更多
孤傲高冷的网名
7楼-- · 2019-03-09 02:57
mzscheme -e "(sort (build-list 100 (λ x (random 9))) <)"

He said the least chars, not the least bytes. =)

查看更多
登录 后发表回答