用最少的代码字符创建、排序和打印 100 个随机整数的列表

发布于 2024-07-09 19:05:39 字数 157 浏览 11 评论 0原文

您可以编写最少的代码来创建、排序(升序)和打印 100 个随机正整数的列表? 我所说的最少代码量是指整个源文件中包含的字符,因此开始缩小。

我有兴趣使用任何和所有编程语言查看答案。 让我们尝试为每种语言保留一个答案,编辑前一个以更正或简化。 如果无法编辑,可以评论吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

你列表最软的妹 2024-07-16 19:05:39

J: 中的 10 个字符

/:~100?9e9

解释:

/:~ 对数组进行排序(从技术上讲,将列表排序的排列向量应用于自身)

x ? limit 返回 x 个小于 limit

9e9 (9000000000) 的随机数,这是一个可以用 3 个字符表示的合理上限。 !9(9 阶乘)较小,但需要少一个字符。

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.

挽袖吟 2024-07-16 19:05:39

PHP 中的 xkcd 样式

for($i=0;$i<100;$i++) echo "4\n";

xkcd style in PHP:

for($i=0;$i<100;$i++) echo "4\n";
谁许谁一生繁华 2024-07-16 19:05:39

Linux,命令行:

% od -dAn -N40 /dev/random | tr ' ' '\n' | sort -nu
4959
6754
8133
10985
11121
14413
17335
20754
21317
30008
30381
33494
34935
41210
41417
43054
48254
51279
54055
55306

Linux, command line:

% od -dAn -N40 /dev/random | tr ' ' '\n' | sort -nu
4959
6754
8133
10985
11121
14413
17335
20754
21317
30008
30381
33494
34935
41210
41417
43054
48254
51279
54055
55306
夜唯美灬不弃 2024-07-16 19:05:39

我的条目:

echo enter a bunch of ints, hit control-D when done
cat - | sort -n

或者,根据亚当在评论中的说法:

echo enter a bunch of ints, hit control-D when done
sort -n

My entry:

echo enter a bunch of ints, hit control-D when done
cat - | sort -n

or, per Adam in the comments:

echo enter a bunch of ints, hit control-D when done
sort -n
谁的新欢旧爱 2024-07-16 19:05:39

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);
    }
}

编辑:制作完整的程序。 假设可以删除换行符和空格,但为了清晰起见保留了:)

编辑:变得更短......我敢于有人改进这个......我已经尝试了一个小时。

编辑:我认为这有点短。

编辑:我认为这更短。 呃,让我停下来。

编辑:多一行,少一个字符。 有争议的...


Explanation

A[100] - 任何旧事物的数组 - 在本例中为 A(这是一个很好的短名称)。 内容完全被忽略,重要的是数组的大小。

.Select(i=>r.Next()) - 生成 r.Next() 的 100 个可枚举值。

.OrderBy(i=>i) - 按顺序对前一个进行排序。

.ToList() - 将排序后的 int 枚举转换为 List,这样我们就可以使用 ForEach。

ForEach(Console.WriteLine) - 调用 Console.WriteLine 100 次,传入列表中的每个整数值。

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.

并安 2024-07-16 19:05:39

Mathematica,28 个字符

Sort@RandomInteger[2^32, 100]

给出 {0,...,2^32} 中的 100 个(已排序)随机整数。

Mathematica, 28 chars

Sort@RandomInteger[2^32, 100]

That gives 100 (sorted) random integers in {0,...,2^32}.

幻想少年梦 2024-07-16 19:05:39

Common Lisp,0 到 10000 之间的 int(没有上限,但你必须选择一个)。

(sort (loop repeat 100 collect (random 10000)) #'<)

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)) #'<)
掩饰不了的爱 2024-07-16 19:05:39

APL

13 个字符:

a[⍋a←100?9e8]

APL

13 chars:

a[⍋a←100?9e8]
不乱于心 2024-07-16 19:05:39

F#

let r = new System.Random();;

[ for i in 0..100 -> r.Next()] |> List.sort (fun x y -> x-y);;

F#

let r = new System.Random();;

[ for i in 0..100 -> r.Next()] |> List.sort (fun x y -> x-y);;
自演自醉 2024-07-16 19:05:39

ruby 中的尝试:(

p [].tap{|a|100.times{a<<rand(9e9)}}.sort

少了 8 个字符,但需要 Ruby 1.9 的 tap kestrel)

- 对于 ruby​​ 1.8:

p (0..?d).map{rand 1<<32}.sort

30 个字符。 (可以通过改回 9e9 来削减 2,但有问题的评论说范围应该是 MaxInt32。

An attempt in ruby:

p [].tap{|a|100.times{a<<rand(9e9)}}.sort

(With eight fewer characters, but requiring the tap kestrel of Ruby 1.9)

-for ruby 1.8:

p (0..?d).map{rand 1<<32}.sort

30 characters. (could trim by 2 by changing back to 9e9, but comment in question says range should be MaxInt32.

浅唱ヾ落雨殇 2024-07-16 19:05:39

哈斯克尔:

import Random
import List
main=newStdGen>>=print.sort.(take 100).randomRs(0,2^32)

Haskell:

import Random
import List
main=newStdGen>>=print.sort.(take 100).randomRs(0,2^32)
极致的悲 2024-07-16 19:05:39

在 BASH 中:

for i in `seq 100`; do echo $RANDOM; done | sort -n

In BASH:

for i in `seq 100`; do echo $RANDOM; done | sort -n
┼── 2024-07-16 19:05:39

Javascript:(通过 JSDB 或 Mozilla 的 Rhino 在 shell 模式下使用)

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

这是一个完整的测试运行:

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

编辑:看起来我可以通过直接赋值而不是“推送”来缩短它几个字符,而且我不这样做不需要 {}:

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

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();
与酒说心事 2024-07-16 19:05:39

Python打印100个随机的、排序的整数

import random,sys
print sorted(random.randint(1,sys.maxint)for x in range(100))

@Adam 已经打败了我,但我认为使用 randint() 和 sys.maxint 有足够的不同来发布。

Python to print 100 random, sorted integers

import random,sys
print sorted(random.randint(1,sys.maxint)for x in range(100))

@Adam already beat me to it, but I thought using randint() and sys.maxint was sufficiently different to post anyway.

ヅ她的身影、若隐若现 2024-07-16 19:05:39

APL(交互式):

如果您想要数字 0-99(或 1-100,具体取决于无论您将工作区中的索引原点设置为 0 还是 1),要唯一,它需要 8 个字符,如下所示:

↑100?100

如果您不关心唯一性,请执行以下操作(9 个字符):

↑?100ρ100

想要更大的数字吗? 只需用您的上限 N 替换每行的第二个 100,您的随机数将在 0 - N-1 范围内(如果索引原点设置为 1,则为 1-N)。

如果您想保证数字范围为 0-99(如果您想要更大的上限,则为 0 - N-1),无论索引原点设置如何,只需将上述任一行括在括号中并添加

< code>-⎕IO

到末尾(其中 ⎕ 是 APL 的四字符)。 这是额外的 6 个字符。

APL (interactive):

If you want the numbers 0-99 (or 1-100, depending on whether you have the index origin in your workspace set to 0 or 1) to be unique, it takes 8 characters, like so:

↑100?100

If you don't care about uniqueness, do this (9 characters):

↑?100ρ100

Want larger numbers? Just substitute your upper limit, N, for the second 100 on each line, and your random numbers will be in the range 0 - N-1 (or 1-N if your index origin is set to 1).

If you want to guarantee that your numbers range from 0-99 (or 0 - N-1 if you're going for a larger upper limit) regardless of the index origin setting, just enclose either of the above lines in parentheses and add

-⎕IO

to the end (where ⎕ is APL's quad character). That's an additional 6 characters.

不寐倦长更 2024-07-16 19:05:39

Powershell :

35 个字符(使用 PowerShell 社区扩展,取代 Get-Random):

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

20 个字符(纯 PowerShell v2):

0..99|%{random}|sort

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
痴骨ら 2024-07-16 19:05:39

Perl,比 nrich 的版本短了整整 8 个字节,并且在“use warnings”下运行; :)

perl -wle "$,=' ';print sort map {int rand 100} 1..100"

Perl, a full 8 bytes shorter than nrich's version, and runs under "use warnings;" :)

perl -wle "$,=' ';print sort map {int rand 100} 1..100"
请恋爱 2024-07-16 19:05:39

爪哇:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

class Rnd {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>(100);
        for (int i = 0; i < 100; i++) list.add(new Random().nextInt());
        Collections.sort(list);
        System.out.println(list);
    }
}

Java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

class Rnd {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>(100);
        for (int i = 0; i < 100; i++) list.add(new Random().nextInt());
        Collections.sort(list);
        System.out.println(list);
    }
}
傲性难收 2024-07-16 19:05:39

绝妙:

r=new Random()
List l=[]
100.times{ l << r.nextInt(1000) }
l.sort().each { println it }

groovy:

r=new Random()
List l=[]
100.times{ l << r.nextInt(1000) }
l.sort().each { println it }
凡间太子 2024-07-16 19:05:39

克洛尤尔

(defn gen-rands []
(sort (take 100 (repeatedly #(rand-int Integer/MAX_VALUE)))))

Clojure

(defn gen-rands []
(sort (take 100 (repeatedly #(rand-int Integer/MAX_VALUE)))))
淡看悲欢离合 2024-07-16 19:05:39

在 OCaml 中:

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

编辑:在 OCaml 中,在顶层键入将打印出列表,但如果您希望将列表打印到标准输出:

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));;

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));;
森林散布 2024-07-16 19:05:39

Windows BATCH:160。这会在数字中添加一个前导零,但否则排序会有点混乱(因为排序按字符排序 - 它对数字一无所知)。

@echo off
set n=%random%.tmp
call :a >%n%
type %n%|sort
del /Q %n%
exit /B 0
:a
for /L %%i in (1,1,100) do call :b
exit /B 0
:b
set i=00000%random%
echo %i:~-5%

作为一句简短​​的话(72):

cmd/v/c"for /l %x in (0,1,99)do @(set x=0000!RANDOM!&echo !x:~-5!)"|sort

Windows BATCH: 160. This adds a leading zero's to the numbers, but otherwise the sorting is a little messed up (because sort sorts by characters - it doesn't know anything about numbers).

@echo off
set n=%random%.tmp
call :a >%n%
type %n%|sort
del /Q %n%
exit /B 0
:a
for /L %%i in (1,1,100) do call :b
exit /B 0
:b
set i=00000%random%
echo %i:~-5%

As a one-liner and way shorter (72):

cmd/v/c"for /l %x in (0,1,99)do @(set x=0000!RANDOM!&echo !x:~-5!)"|sort
方圜几里 2024-07-16 19:05:39

C++ 不是适合这项工作的工具,但这里是:

#include <algorithm>
#include <stdio.h>

#define each(x) n=0; while(n<100) x

int main()
{
     int v[100], n;
     srand(time(0));
     each(v[n++]=rand());
     std::sort(v, v+100);
     each(printf("%d\n",v[n++]));
}

C++ is not the right tool for this job, but here goes:

#include <algorithm>
#include <stdio.h>

#define each(x) n=0; while(n<100) x

int main()
{
     int v[100], n;
     srand(time(0));
     each(v[n++]=rand());
     std::sort(v, v+100);
     each(printf("%d\n",v[n++]));
}
不如归去 2024-07-16 19:05:39

mackenir:改进了 7 个字符:

namespace 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 );
        }
    }
}

mackenir: an improvement by 7 characters:

namespace 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 );
        }
    }
}
⒈起吃苦の倖褔 2024-07-16 19:05:39

带升压的 C++。 太糟糕了,#include 已经占了所有文本的一半:)

#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"));
}

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"));
}
江南烟雨〆相思醉 2024-07-16 19:05:39

C#

如果您同意对数组大小施加限制,那么:

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

否则,可以采取限制较少(但稍微冗长)的角度:

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

好的,我想这是我最后一次回到这个话题。 ..

116 个字符

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()));
    }
}

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()));
    }
}
终陌 2024-07-16 19:05:39

167 个字符的普通旧 C 代码:

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]);}

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]);}
维持三分热 2024-07-16 19:05:39

Java,再次

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);
    }
}

我认为它不能比这更短..
我还删掉了不必要的空间。

LE:哦,是的,可以:)受到丁的帖子的启发..

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);
    }
}

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);
    }
}
春庭雪 2024-07-16 19:05:39
mzscheme -e "(sort (build-list 100 (λ x (random 9))) <)"

他说的是最少的字符,而不是最少的字节。 =)

mzscheme -e "(sort (build-list 100 (λ x (random 9))) <)"

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

沫雨熙 2024-07-16 19:05:39

Tcl已经死了。

tcl万岁。

创建一个 RANDOM (0-99) 长度列表并将 RANDOM (0-99) 整数放入其中。

还打印到屏幕上,并且可以完全按照 tcl 文件或 tcl shell 中所示的方式运行。

set l {}
proc r {} {expr { int(floor(rand()*99)) }}
for {set i 0} {$i<[r]} {incr i} {lappend l [r]}
puts [lsort -integer $l]

PHP 也不错。

完全确认可以锻炼


<?
for($i=100;$i--;$l[]=rand());
sort($l);
print_r($l);

Tcl is dead.

Long live tcl.

Creates a RANDOM (0-99) length list and puts RANDOM (0-99) integers in it.

Also prints to the screen and can be run exactly as shown in a tcl file, or the tcl shell.

set l {}
proc r {} {expr { int(floor(rand()*99)) }}
for {set i 0} {$i<[r]} {incr i} {lappend l [r]}
puts [lsort -integer $l]

PHP is nice too.

confirms completely to exercise


<?
for($i=100;$i--;$l[]=rand());
sort($l);
print_r($l);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文