add tasks starting with a number

This commit is contained in:
Ingy döt Net 2013-04-10 14:23:37 -07:00
parent 9246b988c7
commit 2dd7375f96
204 changed files with 3726 additions and 0 deletions

BIN
Conf/.task.yaml.swp Normal file

Binary file not shown.

View File

@ -1,3 +1,5 @@
100 doors:
24 game:
99 Bottles of Beer:
Ackermann function:
Anagrams:

1
Lang/4DOS-Batch/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/4DOS-Batch

View File

@ -0,0 +1 @@
../../Task/100-doors/6502-Assembly

1
Lang/ABAP/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/ABAP

1
Lang/ACL2/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/ACL2

1
Lang/ALGOL-68/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/ALGOL-68

1
Lang/APL/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/APL

1
Lang/AWK/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/AWK

1
Lang/ActionScript/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/ActionScript

1
Lang/Ada/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Ada

1
Lang/Ada/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Ada

1
Lang/Aikido/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Aikido

1
Lang/AmigaE/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/AmigaE

1
Lang/AppleScript/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/AppleScript

1
Lang/Arbre/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Arbre

1
Lang/Argile/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Argile

1
Lang/Argile/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Argile

1
Lang/AutoHotkey/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/AutoHotkey

1
Lang/AutoHotkey/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/AutoHotkey

1
Lang/AutoIt/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/AutoIt

1
Lang/AutoIt/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/AutoIt

1
Lang/Axiom/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Axiom

1
Lang/BASIC/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/BASIC

1
Lang/Befunge/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Befunge

1
Lang/C/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/C

1
Lang/C/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/C

1
Lang/Clojure/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Clojure

1
Lang/Clojure/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Clojure

1
Lang/CoffeeScript/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/CoffeeScript

1
Lang/CoffeeScript/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/CoffeeScript

1
Lang/Dylan/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Dylan

1
Lang/Eiffel/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Eiffel

1
Lang/Erlang/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Erlang

1
Lang/Erlang/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Erlang

1
Lang/Forth/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Forth

1
Lang/Fortran/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Fortran

1
Lang/Fortran/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Fortran

1
Lang/Go/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Go

1
Lang/Go/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Go

1
Lang/Haskell/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Haskell

1
Lang/Haskell/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Haskell

1
Lang/Java/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Java

1
Lang/JavaScript/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/JavaScript

1
Lang/JavaScript/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/JavaScript

1
Lang/Lua/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Lua

1
Lang/Lua/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Lua

1
Lang/PHP/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/PHP

1
Lang/PHP/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/PHP

1
Lang/Perl/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Perl

1
Lang/Perl/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Perl

1
Lang/PicoLisp/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/PicoLisp

1
Lang/PicoLisp/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/PicoLisp

1
Lang/Prolog/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Prolog

1
Lang/Python/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Python

1
Lang/Python/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Python

1
Lang/R/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/R

1
Lang/R/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/R

1
Lang/REXX/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/REXX

1
Lang/REXX/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/REXX

1
Lang/Racket/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Racket

1
Lang/Racket/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Racket

1
Lang/Ruby/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Ruby

1
Lang/Ruby/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Ruby

1
Lang/Sather/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Sather

1
Lang/Scala/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Scala

1
Lang/Scala/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Scala

1
Lang/Scheme/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Scheme

1
Lang/Scheme/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Scheme

1
Lang/Smalltalk/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Smalltalk

1
Lang/Tcl/100-doors Symbolic link
View File

@ -0,0 +1 @@
../../Task/100-doors/Tcl

1
Lang/Tcl/24-game Symbolic link
View File

@ -0,0 +1 @@
../../Task/24-game/Tcl

View File

@ -1,4 +1,12 @@
---
100 doors:
name: 100 doors
path: 100-doors
url: 100_doors
24 game:
name: 24 game
path: 24-game
url: 24_game
99 Bottles of Beer:
name: 99 Bottles of Beer
path: 99-Bottles-of-Beer

View File

@ -0,0 +1,5 @@
Problem: You have 100 doors in a row that are all initially closed. You make 100 [[task feature::Rosetta Code:multiple passes|passes]] by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
Question: What state are the doors in after the last pass? Which are open, which are closed? [http://www.techinterview.org/Puzzles/fog0000000079.html]
'''[[task feature::Rosetta Code:extra credit|Alternate]]:''' As noted in this page's [[Talk:100 doors|discussion page]], the only doors that remain open are whose numbers are perfect squares of integers. Opening only those doors is an [[task feature::Rosetta Code:optimization|optimization]] that may also be expressed.

View File

@ -0,0 +1,7 @@
@echo off
set doors=%@repeat[C,100]
do step = 1 to 100
do door = %step to 100 by %step
set doors=%@left[%@eval[%door-1],%doors]%@if[%@instr[%@eval[%door-1],1,%doors]==C,O,C]%@right[%@eval[100-%door],%doors]
enddo
enddo

View File

@ -0,0 +1,3 @@
%@left[n,string] ^: Return n leftmost chars in string
%@right[n,string] ^: Return n rightmost chars in string
%@if[condition,true-val,false-val] ^: Evaluate condition; return true-val if true, false-val if false

View File

@ -0,0 +1,10 @@
;assume all memory is initially set to 0
inc $1 ;start out with a delta of 1
openloop: inc $200,X ;open a door at X
inc $1 ;add 2 to delta
inc $1
txa ;add delta to X
adc $1
tax
cpx #$65 ;check to see if we're at the 100th door
bmi openloop ;jump back to openloop if less than 100

View File

@ -0,0 +1,30 @@
form open_doors_unopt.
data: lv_door type i,
lv_count type i value 1.
data: lt_doors type standard table of c initial size 100.
field-symbols: <wa_door> type c.
do 100 times.
append initial line to lt_doors assigning <wa_door>.
<wa_door> = 'X'.
enddo.
while lv_count < 100.
lv_count = lv_count + 1.
lv_door = lv_count.
while lv_door < 100.
read table lt_doors index lv_door assigning <wa_door>.
if <wa_door> = ' '.
<wa_door> = 'X'.
else.
<wa_door> = ' '.
endif.
add lv_count to lv_door.
endwhile.
endwhile.
loop at lt_doors assigning <wa_door>.
if <wa_door> = 'X'.
write : / 'Door', (4) sy-tabix right-justified, 'is open' no-gap.
endif.
endloop.
endform.

View File

@ -0,0 +1,14 @@
form open_doors_opt.
data: lv_square type i value 1,
lv_inc type i value 3.
data: lt_doors type standard table of c initial size 100.
field-symbols: <wa_door> type c.
do 100 times.
append initial line to lt_doors assigning <wa_door>.
if sy-index = lv_square.
<wa_door> = 'X'.
add: lv_inc to lv_square, 2 to lv_inc.
write : / 'Door', (4) sy-index right-justified, 'is open' no-gap.
endif.
enddo.
endform.

View File

@ -0,0 +1,21 @@
(defun rep (n x)
(if (zp n)
nil
(cons x
(rep (- n 1) x))))
(defun toggle-every-r (n i bs)
(if (endp bs)
nil
(cons (if (zp i)
(not (first bs))
(first bs))
(toggle-every-r n (mod (1- i) n) (rest bs)))))
(defun toggle-every (n bs)
(toggle-every-r n (1- n) bs))
(defun 100-doors (i doors)
(if (zp i)
doors
(100-doors (1- i) (toggle-every i doors))))

View File

@ -0,0 +1,25 @@
# declare some constants #
INT limit = 100;
PROC doors = VOID:
(
MODE DOORSTATE = BOOL;
BOOL closed = FALSE;
BOOL open = NOT closed;
MODE DOORLIST = [limit]DOORSTATE;
DOORLIST the doors;
FOR i FROM LWB the doors TO UPB the doors DO the doors[i]:=closed OD;
FOR i FROM LWB the doors TO UPB the doors DO
FOR j FROM LWB the doors TO UPB the doors DO
IF j MOD i = 0 THEN
the doors[j] := NOT the doors[j]
FI
OD
OD;
FOR i FROM LWB the doors TO UPB the doors DO
printf(($g" is "gl$,i,(the doors[i]|"opened"|"closed")))
OD
);
doors;

View File

@ -0,0 +1,7 @@
PROC doors optimised = ( INT limit )VOID:
FOR i TO limit DO
REAL num := sqrt(i);
printf(($g" is "gl$,i,(ENTIER num = num |"opened"|"closed") ))
OD
;
doors optimised(limit)

View File

@ -0,0 +1,11 @@
outdoors num
⍝ Simulates the 100 doors problem for any number of doors
⍝ Returns a boolean vector with 1 being open
outnum ⍝ num steps
out¨out ⍝ Count out the spacing for each step
out1=out ⍝ Make that into a boolean vector
out¨out ⍝ Flip each vector around
out(num)¨out ⍝ Copy each out to the right size
out/out ⍝ XOR each vector, toggling each marked door
outout ⍝ Disclose the results to get a vector

View File

@ -0,0 +1,7 @@
outdoorsOptimized num;marks
⍝ Returns a boolean vector of the doors that would be left open
marksnum*0.5 ⍝ Take the square root of the size, floored
marks(marks)*2 ⍝ Get each door to be opened
outnum0 ⍝ Make a vector of 0s
out[marks]1 ⍝ Set the marked doors to 1

View File

@ -0,0 +1,17 @@
BEGIN {
for(i=1; i <= 100; i++)
{
doors[i] = 0 # close the doors
}
for(i=1; i <= 100; i++)
{
for(j=i; j <= 100; j += i)
{
doors[j] = (doors[j]+1) % 2
}
}
for(i=1; i <= 100; i++)
{
print i, doors[i] ? "open" : "close"
}
}

View File

@ -0,0 +1,14 @@
BEGIN {
for(i=1; i <= 100; i++) {
doors[i] = 0 # close the doors
}
for(i=1; i <= 100; i++) {
if ( int(sqrt(i)) == sqrt(i) ) {
doors[i] = 1
}
}
for(i=1; i <= 100; i++)
{
print i, doors[i] ? "open" : "close"
}
}

View File

@ -0,0 +1,21 @@
package {
import flash.display.Sprite;
public class Doors extends Sprite {
public function Doors() {
// Initialize the array
var doors:Array = new Array(100);
for (var i:Number = 0; i < 100; i++) {
doors[i] = false;
// Do the work
for (var pass:Number = 0; pass < 100; pass++) {
for (var j:Number = pass; j < 100; j += (pass+1)) {
doors[j] = !doors[j];
}
}
trace(doors);
}
}
}

View File

@ -0,0 +1,22 @@
with Ada.Text_Io; use Ada.Text_Io;
procedure Doors is
type Door_State is (Closed, Open);
type Door_List is array(Positive range 1..100) of Door_State;
The_Doors : Door_List := (others => Closed);
begin
for I in 1..100 loop
for J in The_Doors'range loop
if J mod I = 0 then
if The_Doors(J) = Closed then
The_Doors(J) := Open;
else
The_Doors(J) := Closed;
end if;
end if;
end loop;
end loop;
for I in The_Doors'range loop
Put_Line(Integer'Image(I) & " is " & Door_State'Image(The_Doors(I)));
end loop;
end Doors;

View File

@ -0,0 +1,16 @@
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
procedure Doors_Optimized is
Num : Float;
begin
for I in 1..100 loop
Num := Sqrt(Float(I));
Put(Integer'Image(I) & " is ");
if Float'Floor(Num) = Num then
Put_Line("Opened");
else
Put_Line("Closed");
end if;
end loop;
end Doors_Optimized;

View File

@ -0,0 +1,13 @@
var doors = new int [100]
foreach pass 100 {
for (var door = pass ; door < 100 ; door += pass+1) {
doors[door] = !doors[door]
}
}
var d = 1
foreach door doors {
println ("door " + d++ + " is " + (door ? "open" : "closed"))
}

View File

@ -0,0 +1,14 @@
PROC main()
DEF t[100]: ARRAY,
pass, door
FOR door := 0 TO 99 DO t[door] := FALSE
FOR pass := 0 TO 99
door := pass
WHILE door <= 99
t[door] := Not(t[door])
door := door + pass + 1
ENDWHILE
ENDFOR
FOR door := 0 TO 99 DO WriteF('\d is \s\n', door+1,
IF t[door] THEN 'open' ELSE 'closed')
ENDPROC

View File

@ -0,0 +1,17 @@
set is_open to {}
repeat 100 times
set end of is_open to false
end
repeat with pass from 1 to 100
repeat with door from pass to 100 by pass
set item door of is_open to not item door of is_open
end
end
set open_doors to {}
repeat with door from 1 to 100
if item door of is_open then
set end of open_doors to door
end
end
set text item delimiters to ", "
display dialog "Open doors: " & open_doors

View File

@ -0,0 +1,12 @@
openshut(n):
for x in [1..n]
x%n==0
pass(n):
if n==100
openshut(n)
else
openshut(n) xor pass(n+1)
100doors():
pass(1) -> io

View File

@ -0,0 +1,16 @@
use std, array
close all doors
for each pass from 1 to 100
for (door = pass) (door <= 100) (door += pass)
toggle door
let int pass, door.
.: close all doors :. {memset doors 0 size of doors}
.:toggle <int door>:. { !!(doors[door - 1]) }
let doors be an array of 100 bool
for each door from 1 to 100
printf "#%.3d %s\n" door (doors[door - 1]) ? "[ ]", "[X]"

View File

@ -0,0 +1,27 @@
Loop, 100
Door%A_Index% := "closed"
Loop, 100 {
x := A_Index, y := A_Index
While (x <= 100)
{
CurrentDoor := Door%x%
If CurrentDoor contains closed
{
Door%x% := "open"
x += y
}
else if CurrentDoor contains open
{
Door%x% := "closed"
x += y
}
}
}
Loop, 100 {
CurrentDoor := Door%A_Index%
If CurrentDoor contains open
Res .= "Door " A_Index " is open`n"
}
MsgBox % Res

View File

@ -0,0 +1,6 @@
increment := 3, square := 1
Loop, 100
If (A_Index = square)
outstring .= "`nDoor " A_Index " is open"
,square += increment, increment += 2
MsgBox,, Succesfull, % SubStr(outstring, 2)

View File

@ -0,0 +1,3 @@
While (Door := A_Index ** 2) <= 100
Result .= "Door " Door " is open`n"
MsgBox, %Result%

View File

@ -0,0 +1,17 @@
#include <array.au3>
$doors = 100
;door array, 0 = closed, 1 = open
Local $door[$doors +1]
For $ii = 1 To $doors
For $i = $ii To $doors Step $ii
$door[$i] = Not $door[$i]
next
Next
;display to screen
For $i = 1 To $doors
ConsoleWrite (Number($door[$i])& " ")
If Mod($i,10) = 0 Then ConsoleWrite(@CRLF)
Next

View File

@ -0,0 +1,6 @@
(open,closed,change,open?) := (true,false,not,test);
doors := bits(100,closed);
for i in 1..#doors repeat
for j in i..#doors by i repeat
doors.j := change doors.j
[i for i in 1..#doors | open? doors.i]

View File

@ -0,0 +1,2 @@
[i for i in 1..100 | perfectSquare? i] -- or
[i^2 for i in 1..sqrt(100)::Integer]

Some files were not shown because too many files have changed in this diff Show More