创建一个应用程序来确定数字序列是否已排序
每周的代码保龄球游戏的另一部分与前一个版本一样已经有一周多了,到目前为止已经得到了很好的探索。回顾一下:
Code-Bowling 是一项挑战 写出最晦涩、未经优化的内容, 可怕且混蛋的代码 可能的。基本上,准确的 与代码高尔夫相反。
挑战:
创建一个程序,接受一系列数字,并确定它们是否按升序排列。
示例:
$ ./myprogram 1 2 7 10 14
true
$ ./myprogram 7 2 0 1
false
规则:
确实没有。它可以是控制台应用程序,可以是网页,可以是任何东西。它只需要是一个接受数字并返回数字的独立程序。格式和方法100%由您决定。
所以玩得开心,让我们看看你能想出的混蛋解决方案!
Yet another installment of the weekly code-bowling game as the previous incarnation is over a week old and fairly well explored by now. As a refresher:
Code-Bowling is a challenge for
writing the most obscure, unoptimized,
horrific and bastardized code
possible. Basically, the exact
opposite of Code-Golf.
The Challenge:
Create a program that takes a sequence of numbers, and determines if they are in an ascending order.
Example:
$ ./myprogram 1 2 7 10 14
true
$ ./myprogram 7 2 0 1
false
Rules:
There really are none. It can be a console application, it can be a webpage, it can be whatever. It just needs to be a stand-alone program that accepts numbers and returns numbers. The format and methods are 100% up to you.
So have fun, and let's see the bastardized solutions you can come up with!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这使用了我称之为“父排序”的东西。对于大于 1 的列表,您必须向妈妈或爸爸询问每对数字。这很有趣,因为妈妈有可能让你去问爸爸,而爸爸让你去问妈妈的可能性更大。假设无限的堆栈功能可以永远运行。
This uses something I call "Parent Sort". For list greater than size 1, you have to ask Mom or Dad about each pair of numbers. It's interesting because there's a chance that Mom might have you go ask Dad, and there's a bigger chance that Dad will have you go ask Mom. Could run forever assuming infinite stack capabilities.
该解决方案的最坏情况性能为 O(n!),其工作原理是生成列表的所有可能排列,然后计算一个数字(请参阅函数“值”),该数字具有顺序列表(升序或降序)的最小值。
This solution has worst-case performance O(n!) and works by generating all possible permutations of the list, and then calculating a number (see the function 'value') that has it's minimum for sequential lists (ascending or descending).
这是一个快速的。有趣的是,它应该仍然非常有效,因为它只迭代这些术语一次。它只能处理 0 到 255 之间的数字...
它的工作原理是反转字符串(
1 2 5 4
变为1 2 0 4 3
,换句话说,序列中的数字成为结果中的键,序列中的位置成为值然后我们需要检查的是1
是否位于位置1
中。沿着同样的思路(相同的理论,只是集合论运算):
Here's a quick one. Interestingly, it should still be pretty efficient, since it only iterates over the terms once. It can only work on numbers between 0 and 255...
It works by inverting the string (
1 2 5 4
becomes1 2 0 4 3
, in other words, the number in the sequence becomes the key in the result, and the position in the sequence becomes the value. Then all we need to check is that1
is in position1
.And along the same lines (same theory, just set-theory operations):
这个解决方案并不是未经优化的,但它是晦涩的、可怕的和混蛋的......
这个(ab)使用C编译器作为它的运行时环境,或者可以使用以下shell脚本调用以获得更漂亮的输出(依赖于上面的内容)在sortedcpp.c中):
This solution isn't unoptimised, but it is obscure, horrific, and bastardised...
This (ab)uses the C compiler as it's runtime environment, or can be invoked with the following shell script for prettier output (relies on the above being in sortedcpp.c):
我第一次使用动态规划让事情变得更糟
它的时间和空间复杂度为 O(n²)
First time I used dynamic programing to make things worse
It has time and space complexity of O(n²)
耶,Python!
Yay, Python!