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 03:12

C#

If you're okay with imposing a limit on the array size then:

Array.ForEach(Guid.NewGuid().ToByteArray().OrderBy(c => c).ToArray(), c => Console.WriteLine(c));

Otherwise, a less restrictive (but slightly more verbose) angle could be taken:

var r = new Random();
(new int[100]).Select(i => r.Next()).OrderBy(i => i).ToList().ForEach(Console.WriteLine);

Okay, I think this is the last time I'm coming back to this one...

116 chars:

using System;
class A
{
    static void Main()
    {
        var r=new Random();
        var n=1D;
        for(int i=0;i<100;i++,Console.WriteLine(n+=r.Next()));
    }
}
查看更多
SAY GOODBYE
3楼-- · 2019-03-09 03:13

Java, again

import java.util.*;
class R
{
    public static void main(String[]a)
    {
        List x=new Stack();
        while(x.size()<100)x.add((int)(Math.random()*9e9));
        Collections.sort(x);
        System.out.print(x);
    }
}

i don't think it can be made shorter than this.. i also cut out unnecessary spaces.

LE: oh yes it can :) inspired by ding's post..

import java.util.*;
class R
{
    public static void main(String[]a)
    {
        Set x=new TreeSet();
        while(x.size()<100)x.add((int)(Math.random()*9e9));
        System.out.print(x);
    }
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-09 03:15

Powershell :

35 chars (with PowerShell Community Extensions, which replaces Get-Random):

0..99|%{[int]((random)*10000)}|sort

20 characters (plain PowerShell v2):

0..99|%{random}|sort
查看更多
放我归山
5楼-- · 2019-03-09 03:17

C#

using System;
using System.Linq;
class A {
    static void Main() {
        var r=new Random();
        new A[100].Select(i=>r.Next()).OrderBy(i=>i).ToList().ForEach(Console.WriteLine);
    }
}

EDIT: made complete program. assumes newlines and spaces could be removed, but left in for clarity :)

EDIT: made even shorter.... I dare someone to improve this one... I've tried for an hour.

EDIT: I think that's a bit shorter.

EDIT: I think that's even more shorter. Ugh, make me stop.

EDIT: One more line, one less character. Debatable...


Explanation

A[100] - an array of any old thing - in this case A's (it's a nice short name). The contents are completely ignored, it's the size of the array that counts.

.Select(i=>r.Next()) - generates an enumerable of 100 values of r.Next().

.OrderBy(i=>i) - sorts the previous in order.

.ToList() - convert the sorted enumerable of int to a List, so we can use ForEach.

ForEach(Console.WriteLine) - call Console.WriteLine 100 times, passing in each integer value in the list.

查看更多
唯我独甜
6楼-- · 2019-03-09 03:17

In OCaml:

List.sort compare (let rec r = function 0 -> [] | a -> (Random.int 9999)::(r (a-1)) in r 100);;

Edit: in OCaml typing that in the toplevel will print out the list, but if you want the list printed to stdout:

List.iter (fun x -> Printf.printf "%d\n" x) (List.sort compare (let rec r = function 0 -> [] | a -> (Random.int 9999)::(r (a-1)) in r 100));;
查看更多
唯我独甜
7楼-- · 2019-03-09 03:18

C++ with boost. Too bad that #include's are already half of all the text :)

#include <boost/bind.hpp>
#include <algorithm>
#include <vector>
#include <iterator>
#include <cstdlib>
int main() {
    using namespace std;
    vector<int> a(100);
    transform(a.begin(), a.end(), a.begin(), boost::bind(&rand));
    sort(a.begin(), a.end());
    copy(a.begin(), a.end(), ostream_iterator<int>(cout, "\n"));
}
查看更多
登录 后发表回答