all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

6
Task/Sleep/0DESCRIPTION Normal file
View file

@ -0,0 +1,6 @@
Write a program that does the following in this order:
* Input an amount of time to sleep in whatever units are most natural for your language (milliseconds, seconds, ticks, etc.). This unit should be noted in comments or in a description.
* [[Hello world/Text|Print]] "Sleeping..."
* Sleep the main [[thread]] for the given amount of time.
* Print "Awake!"
* End.

2
Task/Sleep/1META.yaml Normal file
View file

@ -0,0 +1,2 @@
---
note: Basic language learning

12
Task/Sleep/AWK/sleep.awk Normal file
View file

@ -0,0 +1,12 @@
# syntax: GAWK -f SLEEP.AWK [seconds]
BEGIN {
print("Sleeping...")
loop(ARGV[1])
print("Awake!")
exit(0)
}
function loop(seconds, t) {
# awk lacks a sleep mechanism, so simulate one by looping
t = systime()
while (systime() < t + seconds) {}
}

11
Task/Sleep/Ada/sleep.ada Normal file
View file

@ -0,0 +1,11 @@
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
procedure Sleep is
In_Val : Float;
begin
Get(In_Val);
Put_Line("Sleeping...");
delay Duration(In_Val);
Put_Line("Awake!");
end Sleep;

View file

@ -0,0 +1,4 @@
TrayTip, sleeping, sleeping
sleep, 2000 ; 2 seconds
TrayTip, awake, awake
Msgbox, awake

View file

@ -0,0 +1,6 @@
#AutoIt Version: 3.2.10.0
$sleep_me=InputBox("Sleep", "Number of seconds to sleep", "10", "", -1, -1, 0, 0)
Dim $sleep_millisec=$sleep_me*1000
MsgBox(0,"Sleep","Sleeping for "&$sleep_me&" sec")
sleep ($sleep_millisec)
MsgBox(0,"Awake","... Awaking")

View file

@ -0,0 +1,4 @@
INPUT sec 'the SLEEP command takes seconds
PRINT "Sleeping..."
SLEEP sec
PRINT "Awake!"

View file

@ -0,0 +1,5 @@
10 REM s is the number of seconds
20 LET s = 5
30 PRINT "Sleeping"
40 PAUSE s * 50
50 PRINT "Awake"

View file

@ -0,0 +1,4 @@
INPUT "Enter the time to sleep in centiseconds: " sleep%
PRINT "Sleeping..."
WAIT sleep%
PRINT "Awake!"

View file

@ -0,0 +1,6 @@
@echo off
set /p Seconds=Enter the number of seconds to sleep:
set /a Seconds+=1
echo Sleeping ...
ping -n %Seconds% localhost >nul 2>&1
echo Awake!

View file

@ -0,0 +1,5 @@
@echo off
set /p MilliSeconds=Enter the number of milliseconds to sleep:
echo Sleeping ...
ping -n 1 -w %MilliSeconds% 1.2.3.4 >nul 2>&1
echo Awake!

View file

@ -0,0 +1,5 @@
@echo off
set /p Seconds=Enter the number of seconds to sleep:
echo Sleeping ...
timeout /t %Seconds% /nobreak >nul
echo Awake!

View file

@ -0,0 +1,11 @@
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
unsigned long microseconds;
std::cin >> microseconds;
std::cout << "Sleeping..." << std::endl;
std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
std::cout << "Awake!\n";
}

View file

@ -0,0 +1,14 @@
#include <unistd.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
useconds_t microseconds;
cin >> microseconds;
cout << "Sleeping..." << endl;
usleep(microseconds);
cout << "Awake!" << endl;
return 0;
}

12
Task/Sleep/C/sleep.c Normal file
View file

@ -0,0 +1,12 @@
#include <stdio.h>
#include <unistd.h>
int main()
{
unsigned int seconds;
scanf("%u", &seconds);
printf("Sleeping...\n");
sleep(seconds);
printf("Awake!\n");
return 0;
}

View file

@ -0,0 +1,6 @@
(defn sleep [ms] ; time in milliseconds
(println "Sleeping...")
(Thread/sleep ms)
(println "Awake!"))
; call it
(sleep 1000)

View file

@ -0,0 +1,7 @@
(defun test-sleep ()
(let ((seconds (read)))
(format t "Sleeping...~%")
(sleep seconds)
(format t "Awake!~%")))
(test-sleep)

12
Task/Sleep/D/sleep.d Normal file
View file

@ -0,0 +1,12 @@
import std.stdio, core.thread;
void main() {
write("Enter a time to sleep (in seconds): ");
long secs;
readf(" %d", &secs);
writeln("Sleeping...");
Thread.sleep(dur!"seconds"(secs));
writeln("Awake!");
}

View file

@ -0,0 +1,17 @@
program SleepOneSecond;
{$APPTYPE CONSOLE}
uses SysUtils;
var
lTimeToSleep: Integer;
begin
if ParamCount = 0 then
lTimeToSleep := 1000
else
lTimeToSleep := StrToInt(ParamStr(1));
WriteLn('Sleeping...');
Sleep(lTimeToSleep); // milliseconds
WriteLn('Awake!');
end.

7
Task/Sleep/E/sleep.e Normal file
View file

@ -0,0 +1,7 @@
def sleep(milliseconds :int, nextThing) {
stdout.println("Sleeping...")
timer.whenPast(timer.now() + milliseconds, fn {
stdout.println("Awake!")
nextThing()
})
}

11
Task/Sleep/EGL/sleep.egl Normal file
View file

@ -0,0 +1,11 @@
program Sleep type BasicProgram{}
// Syntax: sysLib.wait(time BIN(9,2) in)
function main()
SysLib.writeStdout("Sleeping!");
sysLib.wait(15); // waits for 15 seconds
SysLib.writeStdout("Awake!");
end
end

17
Task/Sleep/Eiffel/sleep.e Normal file
View file

@ -0,0 +1,17 @@
class
APPLICATION
inherit
EXECUTION_ENVIRONMENT
create
make
feature -- Initialization
make
-- Sleep for a given number of nanoseconds.
do
print ("Enter a number of nanoseconds: ")
io.read_integer_64
print ("Sleeping...%N")
sleep (io.last_integer_64)
print ("Awake!%N")
end
end

View file

@ -0,0 +1,4 @@
main() ->
io:format("Sleeping...~n"),
timer:sleep(1000), %% in milliseconds
io:format("Awake!~n").

View file

@ -0,0 +1,6 @@
main() ->
io:format("Sleeping...~n"),
receive
after 1000 -> ok %% in milliseconds
end,
io:format("Awake!~n").

View file

@ -0,0 +1,7 @@
USING: calendar io math.parser threads ;
: read-sleep ( -- )
readln string>number seconds
"Sleeping..." print
sleep
"Awake!" print ;

View file

@ -0,0 +1,21 @@
using concurrent
class Main
{
public static Void main ()
{
echo ("Enter a time to sleep: ")
input := Env.cur.in.readLine
try
{
time := Duration.fromStr (input)
echo ("sleeping ...")
Actor.sleep (time)
echo ("awake!")
}
catch
{
echo ("Invalid time entered")
}
}
}

View file

@ -0,0 +1,4 @@
: sleep ( ms -- )
." Sleeping..."
ms
." awake." cr ;

View file

@ -0,0 +1,18 @@
program test_sleep
implicit none
integer :: iostat
integer :: seconds
character (32) :: argument
if (iargc () == 1) then
call getarg (1, argument)
read (argument, *, iostat = iostat) seconds
if (iostat == 0) then
write (*, '(a)') 'Sleeping...'
call sleep (seconds)
write (*, '(a)') 'Awake!'
end if
end if
end program test_sleep

View file

@ -0,0 +1,7 @@
do
t = eval[input["Enter amount of time to sleep: ", "1 second"]]
while ! (t conforms time)
println["Sleeping..."]
sleep[t]
println["Awake!"]

13
Task/Sleep/Go/sleep.go Normal file
View file

@ -0,0 +1,13 @@
package main
import "time"
import "fmt"
func main() {
fmt.Print("Enter number of seconds to sleep: ")
var sec float64
fmt.Scanf("%f", &sec)
fmt.Print("Sleeping…")
time.Sleep(time.Duration(sec * float64(time.Second)))
fmt.Println("\nAwake!")
}

View file

@ -0,0 +1,5 @@
def sleepTest = {
println("Sleeping...")
sleep(it)
println("Awake!")
}

View file

@ -0,0 +1,12 @@
sleepTest(1000)
print '''
Hmmm. That was... less than satisfying.
How about this instead?
'''
Thread.start {
(0..5).each {
println it
sleep(1000)
}
}
sleepTest(5000)

View file

@ -0,0 +1,6 @@
import Control.Concurrent
main = do seconds <- readLn
putStrLn "Sleeping..."
threadDelay $ round $ seconds * 1000000
putStrLn "Awake!"

View file

@ -0,0 +1,4 @@
DLG(NameEdit = milliseconds, Button = "Go to sleep")
WRITE(StatusBar) "Sleeping ... "
SYSTEM(WAIT = milliseconds)
WRITE(Messagebox) "Awake!"

4
Task/Sleep/IDL/sleep.idl Normal file
View file

@ -0,0 +1,4 @@
read,i,prompt='Input sleep time in seconds: '
print,'Sleeping...'
wait,i ; in seconds, but accepts floats(/fractional) as input
print,'Awake!'

View file

@ -0,0 +1,12 @@
procedure main()
repeat {
writes("Enter number of seconds to sleep :")
s := reads()
if s = ( 0 < integer(s)) then break
}
write("\nSleeping for ",s," seconds.")
delay(1000 * s)
write("Awake!")
end

7
Task/Sleep/J/sleep-1.j Normal file
View file

@ -0,0 +1,7 @@
sleep =: 6!:3
sleeping=: monad define
smoutput 'Sleeping...'
sleep y
smoutput 'Awake!'
)

3
Task/Sleep/J/sleep-2.j Normal file
View file

@ -0,0 +1,3 @@
sleeping 0.500 NB. Sleep 500 milliseconds
Sleeping...
Awake!

View file

@ -0,0 +1,15 @@
import java.util.InputMismatchException;
import java.util.Scanner;
public class Sleep {
public static void main(final String[] args) throws InterruptedException {
try {
int ms = new Scanner(System.in).nextInt(); //Java's sleep method accepts milliseconds
System.out.println("Sleeping...");
Thread.sleep(ms);
System.out.println("Awake!");
} catch (InputMismatchException inputMismatchException) {
System.err.println("Exception: " + inputMismatchException);
}
}
}

View file

@ -0,0 +1,9 @@
<script>
setTimeout(function () {
document.write('Awake!')
}, prompt("Number of milliseconds to sleep"));
document.write('Sleeping... ');
</script>

View file

@ -0,0 +1,4 @@
Input "Please input the number of milliseconds you would like to sleep. "; sleeptime
Print "Sleeping..."
CallDLL #kernel32, "Sleep", sleeptime As long, ret As void
Print "Awake!"

View file

@ -0,0 +1,5 @@
to sleep :n
print [Sleeping...]
wait :n ; units: 1/60th of a second
print [Awake.]
end

View file

@ -0,0 +1,9 @@
function sleep()
time = input('How many seconds would you like me to sleep for? ');
assert(time > .01);
disp('Sleeping...');
pause(time);
disp('Awake!');
end

View file

@ -0,0 +1 @@
Sleep[seconds_] := (Print["Sleeping..."]; Pause[seconds]; Print["Awake!"];)

View file

@ -0,0 +1,37 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method sleep(secs) public static binary
ms = (secs * 1000).format(null, 0) -- milliseconds, rounded to nearest integer
say 'Sleeping...'
do
Thread.sleep(ms)
catch ix = InterruptedException
say 'Sleep interrupted!'
ix.printStackTrace()
end
say 'Awake!'
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
secs = -1
loop until \secs.datatype('N')
if secs > 0 then do
say 'Napping for' secs's'
say
sleep(secs)
end
say
say 'How many seconds do you want me to sleep? (enter something non-numeric to terminate)\-'
parse ask secs .
say
end
say
say 'Goodbye...'
say
return

View file

@ -0,0 +1,3 @@
(println "Sleeping..." )
(sleep 2000) ; Wait for 2 seconds
(println "Awake!")

View file

@ -0,0 +1,7 @@
import os, strutils
echo("Enter how long I should sleep (in milliseconds):")
var timed = parseInt(readLine(stdin).string)
echo("Sleeping...")
sleep(timed)
echo("Awake!")

View file

@ -0,0 +1,5 @@
#load "unix.cma";;
let seconds = read_int ();;
print_endline "Sleeping...";;
Unix.sleep seconds;; (* number is integer in seconds *)
print_endline "Awake!";;

View file

@ -0,0 +1,7 @@
#load "unix.cma";;
#directory "+threads";;
#load "threads.cma";;
let seconds = read_float ();;
print_endline "Sleeping...";;
Thread.delay seconds;; (* number is in seconds ... but accepts fractions *)
print_endline "Awake!";;

View file

@ -0,0 +1,11 @@
bundle Default {
class Test {
function : Main(args : System.String[]) ~ Nil {
if(args->Size() = 1) {
"Sleeping..."->PrintLine();
Thread->Sleep(args[0]->ToInt());
"Awake!"->PrintLine();
};
}
}
}

View file

@ -0,0 +1,17 @@
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTimeInterval sleeptime;
printf("wait time in seconds: ");
scanf("%f", &sleeptime);
NSLog(@"sleeping...");
[NSThread sleepForTimeInterval: sleeptime];
NSLog(@"awakening...");
[pool release];
return 0;
}

8
Task/Sleep/Oz/sleep.oz Normal file
View file

@ -0,0 +1,8 @@
declare
class TextFile from Open.file Open.text end
StdIn = {New TextFile init(name:stdin)}
WaitTime = {String.toInt {StdIn getS($)}}
in
{System.showInfo "Sleeping..."}
{Delay WaitTime} %% in milliseconds
{System.showInfo "Awake!"}

View file

@ -0,0 +1,7 @@
sleep(ms)={
print("Sleeping...");
while((ms-=gettime()) > 0,);
print("Awake!")
};
sleep(input())

View file

@ -0,0 +1,8 @@
sleep(s)={
print("Sleeping...");
alarm(s);
trap(alarmer,,while(1,));
print("Awake!")
};
sleep(input())

View file

@ -0,0 +1,4 @@
$seconds = 42;
echo "Sleeping...\n";
sleep($seconds); # number is integer in seconds
echo "Awake!\n";

View file

@ -0,0 +1,4 @@
$microseconds = 42000000;
echo "Sleeping...\n";
usleep($microseconds); # number is integer in microseconds
echo "Awake!\n";

View file

@ -0,0 +1,4 @@
$nanoseconds = 42000000000;
echo "Sleeping...\n";
time_nanosleep($seconds, $nanoseconds); # first arg in seconds plus second arg in nanoseconds
echo "Awake!\n";

View file

@ -0,0 +1,3 @@
put ('sleeping');
delay (2000); /* wait for 2 seconds (=2000 milliseconds). */
put ('awake');

View file

@ -0,0 +1,4 @@
my $sec = prompt("Sleep for how many microfortnights? ") * 1.2096;
say "Sleeping...";
sleep $sec;
say "Awake!";

View file

@ -0,0 +1,4 @@
$seconds = <>;
print "Sleeping...\n";
sleep $seconds; # number is in seconds
print "Awake!\n";

View file

@ -0,0 +1,11 @@
use Time::HiRes qw( usleep nanosleep );
$microseconds = <>;
print "Sleeping...\n";
usleep $microseconds;
print "Awake!\n";
$nanoseconds = <>;
print "Sleeping...\n";
nanosleep $nanoseconds;
print "Awake!\n";

View file

@ -0,0 +1,3 @@
(prinl "Sleeping..." )
(wait 2000) # Wait for 2 seconds
(prinl "Awake!")

View file

@ -0,0 +1,3 @@
(prinl "Sleeping..." )
(call 'sleep 2) # Wait for 2 seconds
(prinl "Awake!")

View file

@ -0,0 +1,4 @@
$d = [int] (Read-Host Duration in seconds)
Write-Host Sleeping ...
Start-Sleep $d
Write-Host Awake!

View file

@ -0,0 +1,4 @@
rosetta_sleep(Time) :-
writeln('Sleeping...'),
sleep(Time),
writeln('Awake!').

View file

@ -0,0 +1,4 @@
<@ SAYLIT>Number of seconds: </@><@ GETVAR>secs</@>
<@ SAYLIT>Sleeping</@>
<@ ACTPAUVAR>secs</@>
<@ SAYLIT>Awake</@>

View file

@ -0,0 +1,4 @@
<# MontrezLittéralement>Number of seconds: </#><# PrenezUneValeurVariable>secs</#>
<# MontrezLittéralement>Sleeping</#>
<# AgissezFaireUnePauseVariable>secs</#>
<# MontrezLittéralement>Awake</#>

View file

@ -0,0 +1,4 @@
<@ 显示_字串_>Number of seconds: </@><@ 获取_变量_>secs</@>
<@ 显示_字串_>Sleeping</@>
<@ 运行_暂停动变量_>secs</@>
<@ 显示_字串_>Awake</@>

View file

@ -0,0 +1,11 @@
If OpenConsole()
Print("Enter a time(milliseconds) to sleep: ")
x.i = Val(Input())
PrintN("Sleeping...")
Delay(x) ;in milliseconds
PrintN("Awake!")
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,6 @@
import time
seconds = float(raw_input())
print "Sleeping..."
time.sleep(seconds) # number is in seconds ... but accepts fractions
print "Awake!"

9
Task/Sleep/R/sleep.r Normal file
View file

@ -0,0 +1,9 @@
sleep <- function(time=1)
{
message("Sleeping...")
flush.console()
Sys.sleep(time)
message("Awake!")
}
sleep()

View file

@ -0,0 +1,11 @@
REBOL [
Title: "Sleep Main Thread"
Date: 2009-12-15
Author: oofoe
URL: http://rosettacode.org/wiki/Sleep_the_Main_Thread
]
naptime: to-integer ask "Please enter sleep time in seconds: "
print "Sleeping..."
wait naptime
print "Awake!"

View file

@ -0,0 +1,6 @@
/*REXX program to sleep x seconds (base in the argument). */
parse arg secs . /*get a (possible) argument. */
secs=word(secs 0,1) /*if not present, assume 0 (zero)*/
say 'Sleeping' secs "seconds." /*tell 'em what's happening. */
call delay(secs) /*snooze. Hopefully, a short nap*/
say 'Awake!' /*and tell 'em we're running. */

View file

@ -0,0 +1,7 @@
#lang racket
(displayln "Enter a time (in seconds): ")
(define time (read))
(when (number? time)
(displayln "Sleeping...")
(sleep time)
(displayln "Awake!"))

View file

@ -0,0 +1,6 @@
: sleep ( n- )
[ time [ time over - 1 > ] until drop ] times ;
: test
"\nTime to sleep (in seconds): " puts getToken toNumber
"\nSleeping..." sleep
"\nAwake!\n" ;

5
Task/Sleep/Ruby/sleep.rb Normal file
View file

@ -0,0 +1,5 @@
seconds = gets.to_f
puts "Sleeping..."
sleep(seconds) # number is in seconds ... but accepts fractions
# Minimum resolution is system dependent.
puts "Awake!"

View file

@ -0,0 +1,9 @@
import java.util.Scanner
object Sleeper extends Application {
val input = new Scanner(System.in)
val ms = input.nextInt
System.out.println("Sleeping...")
Thread.sleep(ms)
System.out.println("Awake!")
}

View file

@ -0,0 +1,11 @@
(use format)
(use srfi-18)
(format #t "Enter a time (in seconds): ")
(let ((time (read))) ; converts input to a number, if possible
(if (number? time)
(begin
(format #t "Sleeping...~&")
(thread-sleep! time)
(format #t "Awake!~&"))
(format #t "You must enter a number~&")))

View file

@ -0,0 +1,13 @@
$ include "seed7_05.s7i";
include "duration.s7i";
const proc: main is func
local
var integer: secondsToSleep is 0;
begin
write("Enter number of seconds to sleep: ");
readln(secondsToSleep);
writeln("Sleeping...");
wait(secondsToSleep . SECONDS);
writeln("Awake!");
end func;

View file

@ -0,0 +1,4 @@
t := (FillInTheBlankMorph request: 'Enter time in seconds') asNumber.
Transcript show: 'Sleeping...'.
(Delay forSeconds: t) wait.
Transcript show: 'Awake!'.

View file

@ -0,0 +1,4 @@
t := (Dialog request: 'Enter time in seconds') asNumber.
Transcript show: 'Sleeping...'.
(Delay forSeconds: t) wait.
Transcript show: 'Awake!'.

View file

@ -0,0 +1,8 @@
(TextIO.print "input a number of seconds please: ";
let val seconds = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn))) in
TextIO.print "Sleeping...\n";
OS.Process.sleep (Time.fromReal seconds); (* it takes a Time.time data structure as arg,
but in my implementation it seems to round down to the nearest second.
I dunno why; it doesn't say anything about this in the documentation *)
TextIO.print "Awake!\n"
end)

View file

@ -0,0 +1,6 @@
function (time)
{
Print("Sleeping...")
Sleep(time) // time is in milliseconds
Print("Awake!")
}

View file

@ -0,0 +1,5 @@
$$ MODE TUSCRIPT
secondsrange=2
PRINT "Sleeping ",secondsrange," seconds "
WAIT #secondsrange
PRINT "Awake after Naping ",secondsrange, " seconds"

View file

@ -0,0 +1,6 @@
puts -nonewline "Enter a number of milliseconds to sleep: "
flush stdout
set millis [gets stdin]
puts Sleeping...
after $millis
puts Awake!

View file

@ -0,0 +1,8 @@
puts -nonewline "Enter a number of milliseconds to sleep: "
flush stdout
set millis [gets stdin]
set ::wakupflag 0
puts Sleeping...
after $millis set ::wakeupflag 1
vwait ::wakeupflag
puts Awake!

View file

@ -0,0 +1,5 @@
printf "Enter a time in seconds to sleep: "
read seconds
echo "Sleeping..."
sleep "$seconds"
echo "Awake!"

View file

@ -0,0 +1,4 @@
iSeconds=InputBox("Enter a time in seconds to sleep: ","Sleep Example for RosettaCode.org")
WScript.Echo "Sleeping..."
WScript.Sleep iSeconds*1000 'Sleep is done in Milli-Seconds
WScript.Echo "Awake!"

View file

@ -0,0 +1,4 @@
#1 = Get_Num("Sleep time in 1/10 seconds: ")
Message("Sleeping...\n")
Sleep(#1)
Message("Awake!\n")