Rosetta Code/Tasks without examples: Difference between revisions

Content added Content deleted
Line 2: Line 2:


Task: Scrape http://rosettacode.org/wiki/Category:Programming_Tasks for all the tasks. Traverse the links. Extract the text or html between a tag with a class of "infobox" and the beginning of the table with the id of "toc".
Task: Scrape http://rosettacode.org/wiki/Category:Programming_Tasks for all the tasks. Traverse the links. Extract the text or html between a tag with a class of "infobox" and the beginning of the table with the id of "toc".

=={{header|D}}==
<lang d>import std.algorithm;
import std.net.curl;
import std.range;
import std.regex;
import std.stdio;
import std.string;

void process(string base, string task) {
auto re2 = ctRegex!`</?[^>]*>`;

auto page = base ~ task;
auto content = get(page);

string prefix = `using any language you may know.</div>`;
auto beg = content.indexOf(prefix);
auto end = content.indexOf(`<div id="toc"`, beg);
auto snippet = content[beg + prefix.length .. end];
writeln(replaceAll(snippet, re2, ``));

writeln("#####################################################");
}

void main() {
auto pattern = ctRegex!`<li><a href="/wiki/(.*?)"`;

auto content = get("http://rosettacode.org/wiki/Category:Programming_Tasks");

string[] tasks;
foreach (m; matchAll(content, pattern)) {
tasks ~= m[1].idup;
}

auto base = "http://rosettacode.org/wiki/";
tasks.take(3).each!(task => process(base, task));
}</lang>
{{out}}
<pre>There are 100 doors in a row that are all initially closed.
You make 100 passes by the doors.
The first time through, visit every door and &#160;toggle&#160; the door &#160;(if the door is closed, &#160;open it; &#160; if it is open,&#160; close it).
The second time, only visit every 2nd door &#160; (door #2, #4, #6, ...), &#160; and toggle it.
The third time, visit every 3rd door &#160; (door #3, #6, #9, ...), etc, &#160; until you only visit the 100th door.


Task

Answer the question: &#160; what state are the doors in after the last pass? &#160; Which are open, which are closed?

Alternate:
As noted in this page's &#160; discussion page, &#160; the only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an &#160; optimization &#160; that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.



#####################################################



The Problem

100 prisoners are individually numbered 1 to 100
A room having a cupboard of 100 opaque drawers numbered 1 to 100, that cannot be seen from outside.
Cards numbered 1 to 100 are placed randomly, one to a drawer, and the drawers all closed; at the start.
Prisoners start outside the room
They can decide some strategy before any enter the room.
Prisoners enter the room one by one, can open a drawer, inspect the card number in the drawer, then close the drawer.
A prisoner can open no more than 50 drawers.
A prisoner tries to find his own number.
A prisoner finding his own number is then held apart from the others.
If all 100 prisoners find their own numbers then they will all be pardoned. If any don't then all sentences stand.


The task

Simulate several thousand instances of the game where the prisoners randomly open drawers
Simulate several thousand instances of the game where the prisoners use the optimal strategy mentioned in the Wikipedia article, of:
First opening the drawer whose outside number is his prisoner number.
If the card within has his number then he succeeds otherwise he opens the drawer with the same number as that of the revealed card. (until he opens his maximum).

Show and compare the computed probabilities of success for the two strategies, here, on this page.


References

The unbelievable solution to the 100 prisoner puzzle standupmaths (Video).
wp:100 prisoners problem
100 Prisoners Escape Puzzle DataGenetics.
Random permutation statistics#One hundred prisoners on Wikipedia.



#####################################################




Task

Implement the Fifteen Puzzle Game.

The &#160; 15-puzzle &#160; is also known as:

&#160; Fifteen Puzzle
&#160; Gem Puzzle
&#160; Boss Puzzle
&#160; Game of Fifteen
&#160; Mystic Square
&#160; 14-15 Puzzle
&#160; and many others.


Related Tasks

&#160; 15 Puzzle Solver
&#160; 16 Puzzle Game



#####################################################</pre>


=={{header|Go}}==
=={{header|Go}}==