以最少的代码字符创建,排序和打印100个随机整数的列表 [英] Create, sort, and print a list of 100 random ints in the fewest chars of code

查看:70
本文介绍了以最少的代码字符创建,排序和打印100个随机整数的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建,排序(升序)和打印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?

推荐答案

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 :变得更短了....我敢于有人来改善这一点...我已经尝试了一个小时.

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.

编辑:我认为这要短得多. gh,让我停下来.

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

编辑:多一行,少一个字符.值得商......

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


说明

A[100]-任何旧东西的数组-在这种情况下为A(这是一个很好的简称).内容被完全忽略,重要的是数组的大小.

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())-生成100个r.Next()值的枚举.

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

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

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

.ToList()-将排序的int枚举值转换为List,以便我们可以使用ForEach.

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

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

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

这篇关于以最少的代码字符创建,排序和打印100个随机整数的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆