September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -3,4 +3,4 @@ In general, sleep sort works by starting a separate task for each item to be sor
|
|||
|
||||
Task: Write a program that implements sleep sort. Have it accept non-negative integers on the command line and print the integers in sorted order. If this is not idomatic in your language or environment, input and output may be done differently. Enhancements for optimization, generalization, practicality, robustness, and so on are not required.
|
||||
|
||||
Sleep sort was [http://dis.4chan.org/read/prog/1295544154 presented] anonymously on 4chan and has been [http://news.ycombinator.com/item?id=2657277 discussed] on Hacker News.
|
||||
Sleep sort was [https://archive.fo/xhGo presented] anonymously on 4chan and has been [http://news.ycombinator.com/item?id=2657277 discussed] on Hacker News.
|
||||
|
|
|
|||
|
|
@ -1,18 +1,6 @@
|
|||
import 'dart:async';
|
||||
void main() async {
|
||||
Future<void> sleepsort(Iterable<int> input) => Future.wait(input
|
||||
.map((i) => Future.delayed(Duration(milliseconds: i), () => print(i))));
|
||||
|
||||
Future<List<int>> sleepsort(List<int> input) {
|
||||
List<Future<int>> tasks = [];
|
||||
List<int> result = [];
|
||||
for (int i in input) {
|
||||
tasks.add(new Future.delayed(new Duration(seconds: i), () {
|
||||
result.add(i);
|
||||
}));
|
||||
}
|
||||
return Future.wait(tasks).then((_) {
|
||||
return result;
|
||||
});
|
||||
await sleepsort([3, 10, 2, 120, 122, 121, 54]);
|
||||
}
|
||||
|
||||
sleepsort.sleepsort([3, 1, 2]).then((List<int> sorted) {
|
||||
print(sorted);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,27 +1,32 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
import extensions'threading.
|
||||
import extensions;
|
||||
import system'routines;
|
||||
import extensions'threading;
|
||||
import system'threading;
|
||||
|
||||
extension $op
|
||||
static sync = new object();
|
||||
|
||||
extension op
|
||||
{
|
||||
sleepSort
|
||||
[
|
||||
self forEach(:n)
|
||||
[
|
||||
threadControl start:
|
||||
[
|
||||
var a := 1000 * n.
|
||||
sleepSort()
|
||||
{
|
||||
self.forEach:(n)
|
||||
{
|
||||
threadControl.start(()
|
||||
{
|
||||
threadControl.sleep(1000 * n);
|
||||
|
||||
threadControl sleep(1000 * n).
|
||||
console printLine(n).
|
||||
].
|
||||
]
|
||||
]
|
||||
lock(sync)
|
||||
{
|
||||
console.printLine(n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
'program'arguments skipping:1; selectBy(%"convertorOp.toInt"); toArray; sleepSort.
|
||||
public program()
|
||||
{
|
||||
program_arguments.skipping:1.selectBy(mssgconst toInt<convertorOp>[0]).toArray().sleepSort();
|
||||
|
||||
console readChar.
|
||||
].
|
||||
console.readChar()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
[1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0].timeoutSort(function(n) { document.write(n + 'br'); })
|
||||
[1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0].timeoutSort(function(n) { document.write(n + '<br>'); })
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/env python3
|
||||
from asyncio import run, sleep, wait
|
||||
from sys import argv
|
||||
|
||||
async def f(n):
|
||||
await sleep(n)
|
||||
print(n)
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(wait(list(map(f, map(int, argv[1:])))))
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#! /usr/bin/env tclsh
|
||||
|
||||
package require Tcl 8.6
|
||||
|
||||
# By aspect (https://wiki.tcl-lang.org/page/aspect). Modified slightly.
|
||||
# 1. Schedule N delayed calls to our own coroutine.
|
||||
# 2. Yield N times to grab the scheduled values. Print each.
|
||||
# 3. Store the sorted list in $varName.
|
||||
proc sleep-sort {ls varName} {
|
||||
foreach x $ls {
|
||||
after $x [info coroutine] $x
|
||||
}
|
||||
|
||||
set $varName [lmap x $ls {
|
||||
set newX [yield]
|
||||
puts $newX
|
||||
lindex $newX
|
||||
}]
|
||||
}
|
||||
|
||||
# Ensure the list is suitable for use with [sleep-sort].
|
||||
proc validate ls {
|
||||
if {[llength $ls] == 0} {
|
||||
error {list is empty}
|
||||
}
|
||||
|
||||
foreach x $ls {
|
||||
if {![string is integer -strict $x] || $x < 0} {
|
||||
error [list invalid value: $x]
|
||||
}
|
||||
}
|
||||
|
||||
return $ls
|
||||
}
|
||||
|
||||
coroutine c sleep-sort [validate $argv] ::sorted
|
||||
vwait sorted
|
||||
Loading…
Add table
Add a link
Reference in a new issue