Rosetta Code/Tasks without examples: Difference between revisions

→‎{{header|Wren}}: Various changes including some to accommodate changes to the HTML.
(→‎{{header|Wren}}: Various changes including some to accommodate changes to the HTML.)
 
(14 intermediate revisions by 7 users not shown)
Line 1:
{{draft task}}
{{draft task}}A RosettaCode contributor is going on a long journey. To ease the boredom of a long flight he takes his favourite programming language manual and a list of RosettaCode tasks, but there's a catch: the tasks have to be description only and not include all the solutions already supplied by other programmers.
 
A RosettaCode contributor is going on a long journey.
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".
 
To ease the boredom of a long flight he takes his favourite programming language manual and a list of RosettaCode tasks,   but there's a catch:   the tasks have to be description only and not include all the solutions already supplied by other programmers.
 
 
;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".
<br><br>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.algorithm;
import std.net.curl;
import std.range;
Line 38 ⟶ 47:
auto base = "http://rosettacode.org/wiki/";
tasks.take(3).each!(task => process(base, task));
}</langsyntaxhighlight>
{{out}}
<pre>There are 100 doors in a row that are all initially closed.
Line 125 ⟶ 134:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 168 ⟶ 177:
time.Sleep(5 * time.Second) // wait 5 seconds before processing next task
}
}</langsyntaxhighlight>
 
{{out}}
Line 261 ⟶ 270:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Line 327 ⟶ 336:
tasks.stream().limit(limit).forEach(task -> process(client, base, task));
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Go}}
<syntaxhighlight lang="julia">using HTTP
 
function gettaskdescriptions(numtoprint = 3)
page = "http://rosettacode.org/wiki/Category:Programming_Tasks"
body = String(HTTP.get(page).body)
tasks = [m.captures[1] for m in eachmatch(r"<li><a href=\"/wiki/([^\"]+)\"", body)]
base = "http://rosettacode.org/wiki/"
for (i, task) in enumerate(tasks)
page = base * task
body = String(HTTP.get(page).body)
m = match(r"using any language you may know.</div>(.+)<div id=\"toc\""s, body)
m != nothing && println(replace(m.captures[1], r"<[^>]*>"s => ""), "\n", "="^60, "\n")
i >= numtoprint && break
sleep(rand(3:7)) # wait 5 +/- 2 seconds before processing next task
end
end
 
gettaskdescriptions()
</syntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
Line 383 ⟶ 414:
}
}
}</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Go}}
<syntaxhighlight lang="nim">import htmlparser, httpclient, os, re, strutils, xmltree
 
let re1 = re("""<li><a href="/wiki/(.*?)"""")
const Page = "http://rosettacode.org/wiki/Category:Programming_Tasks"
var client = newHttpClient()
 
# Find tasks.
var body = client.getContent(Page)
var tasks: seq[string]
var start = 0
while true:
var matches: array[1, string]
start = body.find(re1, matches, start) + 1
if start == 0: break
if not matches[0].startsWith("Category:"):
tasks.add matches[0]
 
const Base = "http://rosettacode.org/wiki/"
const Limit = 3 # number of tasks to print out.
let re2 = re("""(?s)using any language you may know.</div>(.*?)<mw:tocplace>""")
for i, task in tasks:
var matches: array[2, string]
let page = Base & task
body = client.getContent(page)
if body.find(re2, matches) < 0:
raise newException(ValueError, "unable to find pattern in page.")
let xmlnode = matches[0].parseHtml() # Build an XML tree from the HTML.
echo task.replace('_', ' ')
echo xmlnode.innerText() # Echo the tree as text.
if i == Limit - 1: break
os.sleep(5000) # Wait 5 seconds before processing next task.</syntaxhighlight>
 
{{out}}
<pre>100 doors
 
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 toggle the door (if the door is closed, open it; if it is open, close it).
The second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it.
The third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
 
 
Task
 
Answer the question: what state are the doors in after the last pass? Which are open, which are closed?
 
Alternate:
As noted in this page's discussion page, the only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an optimization that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.
 
 
 
100 prisoners
 
 
 
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.
 
 
 
15 puzzle game
 
 
 
Task
 
Implement the Fifteen Puzzle Game.
 
The 15-puzzle is also known as:
 
Fifteen Puzzle
Gem Puzzle
Boss Puzzle
Game of Fifteen
Mystic Square
14-15 Puzzle
and some others.
 
 
Related Tasks
 
15 Puzzle Solver
16 Puzzle Game
 
</pre>
 
=={{header|Perl}}==
Slice and dice the HTML. Output is as for the other examples.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 403 ⟶ 553:
print "$title\n$task_at_hand\n\n";
sleep 10; # so you have time to read each task...
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Since downloading all the pages can be very slow, this uses a cache. Limiting by "Phix" fairly obviously speeds it up tenfold :-)<br>
Output similar to zkl, I assume the first four constants are self-explanatory.
{{libheader|Phix/libcurl}}
<lang Phix>-- demo\rosetta\Tasks_without_examples.exw
<!--<syntaxhighlight lang="phix">(notonline)-->
constant output_html = true,
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Tasks_without_examples.exw</span>
include_drafts = true,
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (libcurl, file i/o, peek, progress..)</span>
summary = false,
<span style="color: #008080;">constant</span> <span style="color: #000000;">output_html</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
notlang = "Phix" -- "" for all
<span style="color: #000000;">include_drafts</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">summary</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">,</span>
include builtins\timedate.e
<span style="color: #000000;">notlang</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Phix"</span> <span style="color: #000080;font-style:italic;">-- or "" (ie a zero length string) for all</span>
integer refresh_cache = timedelta(days:=30) -- 0 for always
 
<span style="color: #008080;">include</span> <span style="color: #000000;">rosettacode_cache</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- see [[Rosetta_Code/Count_examples#Phix]]</span>
include builtins\libcurl.e
atom curl = NULL
<span style="color: #008080;">function</span> <span style="color: #000000;">extract_tasks</span><span style="color: #0000FF;">()</span>
atom pErrorBuffer
<span style="color: #008080;">if</span> <span style="color: #7060A8;">get_file_type</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"rc_cache"</span><span style="color: #0000FF;">)!=</span><span style="color: #004600;">FILETYPE_DIRECTORY</span> <span style="color: #008080;">then</span>
 
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">create_directory</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"rc_cache"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
function write_callback(atom pData, integer size, integer nmemb, integer fn)
<span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"cannot create rc_cache directory"</span><span style="color: #0000FF;">)</span>
integer bytes_written = size * nmemb
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
puts(fn,peek({pData,bytes_written}))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return bytes_written
<span style="color: #000080;font-style:italic;">-- extract tasks from eg `&lt;li&gt;&lt;a href="/wiki/100_doors"`</span>
end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tasks</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dewiki</span><span style="color: #0000FF;">(</span><span style="color: #000000;">open_category</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Programming_Tasks"</span><span style="color: #0000FF;">))</span>
constant write_cb = call_back({'+', routine_id("write_callback")})
<span style="color: #008080;">if</span> <span style="color: #000000;">include_drafts</span> <span style="color: #008080;">then</span>
 
<span style="color: #000000;">tasks</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">dewiki</span><span style="color: #0000FF;">(</span><span style="color: #000000;">open_category</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Draft_Programming_Tasks"</span><span style="color: #0000FF;">))</span>
function open_download(string filename, url)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
bool refetch = true
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">notlang</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if get_file_type("rc_cache")!=FILETYPE_DIRECTORY then
<span style="color: #000080;font-style:italic;">-- filter already done in specified language</span>
if not create_directory("rc_cache") then
<span style="color: #004080;">string</span> <span style="color: #000000;">langurl</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"http://rosettacode.org/wiki/Category:"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">notlang</span>
crash("cannot create rc_cache directory")
<span style="color: #004080;">sequence</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dewiki</span><span style="color: #0000FF;">(</span><span style="color: #000000;">open_download</span><span style="color: #0000FF;">(</span><span style="color: #000000;">notlang</span><span style="color: #0000FF;">&</span><span style="color: #008000;">".htm"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">langurl</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end if
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tasks</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
filename = join_path({"rc_cache",filename})
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tasks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">done</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if file_exists(filename) then
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
-- use existing file if <= refresh_cache (30+ days) old
<span style="color: #000000;">tasks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tasks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
sequence last_mod = get_file_date(filename) -- (0.8.1+)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
atom delta = timedate_diff(last_mod,date())
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
refetch = (delta>refresh_cache)
<span style="color: #000000;">tasks</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tasks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
else
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
string directory = get_file_path(filename)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if get_file_type(directory)!=FILETYPE_DIRECTORY then
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">summary</span> <span style="color: #008080;">then</span>
if not create_directory(directory,make_parent:=true) then
<span style="color: #000080;font-style:italic;">-- replace with contents</span>
crash("cannot create %s directory",{directory})
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tasks</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end if
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tasks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
end if
<span style="color: #000000;">url</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"http://rosettacode.org/wiki/%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">}),</span>
end if
<span style="color: #000000;">contents</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">open_download</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">&</span><span style="color: #008000;">".htm"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">url</span><span style="color: #0000FF;">)</span>
if refetch then
<span style="color: #004080;">integer</span> <span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`&lt;/div&gt;`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`&lt;div class="infobox"`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">))+</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`&lt;/div&gt;`</span><span style="color: #0000FF;">)</span>
printf(1,"Downloading %s...\n",{filename})
<span style="color: #004080;">integer</span> <span style="color: #000000;">finish</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`&lt;div id="toc"`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span>
if curl=NULL then
<span style="color: #000080;font-style:italic;">-- ... but draft tasks with too few languages have no toc:</span>
curl_global_init()
<span style="color: #008080;">if</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">finish</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`&lt;h2&gt;`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
curl = curl_easy_init()
<span style="color: #000080;font-style:italic;">-- ... and if no languages at all, use the footer:</span>
pErrorBuffer = allocate(CURL_ERROR_SIZE)
<span style="color: #008080;">if</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">finish</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`&lt;/div&gt;&lt;div class="printfooter"&gt;`</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">,</span><span style="color: #000000;">start</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, pErrorBuffer)
<span style="color: #008080;">if</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb)
<span style="color: #000000;">contents</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">contents</span><span style="color: #0000FF;">[</span><span style="color: #000000;">start</span><span style="color: #0000FF;">..</span><span style="color: #000000;">finish</span><span style="color: #0000FF;">]</span>
end if
<span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"_"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)</span>
url = substitute(url,"%3A",":")
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;b&gt;"</span><span style="color: #0000FF;">&</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"&lt;/b&gt;"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
url = substitute(url,"%2A","*")
<span style="color: #000080;font-style:italic;">-- (ps: I refuse to panic over the occasional replicated header...)</span>
curl_easy_setopt(curl, CURLOPT_URL, url)
<span style="color: #000000;">contents</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;h3&gt;%s&lt;/h3&gt;%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">contents</span><span style="color: #0000FF;">})</span>
integer fn = open(filename,"wb")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if fn=-1 then ?9/0 end if
<span style="color: #000000;">tasks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">contents</span>
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fn)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">get_key</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">#1B</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
CURLcode res = curl_easy_perform(curl)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if res!=CURLE_OK then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
string error = sprintf("%d",res)
<span style="color: #000000;">curl_cleanup</span><span style="color: #0000FF;">()</span>
if res=CURLE_COULDNT_RESOLVE_HOST then
<span style="color: #008080;">return</span> <span style="color: #000000;">tasks</span>
error &= " [CURLE_COULDNT_RESOLVE_HOST]"
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end if
printf(1, "Error %s downloading file\n", error)
<span style="color: #008080;">constant</span> <span style="color: #000000;">html_header</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
{} = wait_key()
&lt;!DOCTYPE html&gt;
abort(0)
&lt;html lang="en"&gt;
end if
&lt;head&gt;
close(fn)
&lt;meta charset="utf-8" /&gt;
refresh_cache += timedelta(days:=1) -- did I mention it is slow?
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
end if
&lt;title&gt;Rosettacode Tasks without examples&lt;/title&gt;
return get_text(filename)
&lt;/head&gt;
end function
&lt;body&gt;
 
&lt;h2&gt;Rosettacode Tasks without examples&lt;/h2&gt;
function open_category(string filename)
Generated %s, %d entries&lt;br&gt;
return open_download(filename&".htm","http://rosettacode.org/wiki/Category:"&filename)
&lt;br&gt;
end function
"""</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">html_footer</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
function dewiki(string s)
&lt;/body&gt;
sequence tasks = {}
&lt;/html&gt;
integer start = 1, finish = match(`<div class="printfooter">`,s)
"""</span>
s = s[1..finish-1]
while true do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">extract_tasks</span><span style="color: #0000FF;">()</span>
start = match("<li><a href=\"/wiki/",s,start)
<span style="color: #008080;">if</span> <span style="color: #000000;">output_html</span> <span style="color: #008080;">then</span>
if start=0 then exit end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Tasks_Without_Examples.html"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
start += length("<li><a href=\"/wiki/")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_header</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">date</span><span style="color: #0000FF;">()),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)})</span>
finish = find('"',s,start)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string task = s[start..finish-1]
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s&lt;br&gt;\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_clean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
task = substitute(task,"*","%2A")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
task = substitute(task,":","%3A")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_footer</span><span style="color: #0000FF;">)</span>
tasks = append(tasks,task)
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
start = finish+1
<span style="color: #008080;">else</span>
end while
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
return tasks
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end function
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_clean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
function extract_tasks()
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
-- extract tasks from eg `<li><a href="/wiki/100_doors"`
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d tasks\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">))</span>
sequence tasks = dewiki(open_category("Programming_Tasks"))
if include_drafts then
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
tasks &= dewiki(open_category("Draft_Programming_Tasks"))
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
end if
<!--</syntaxhighlight>-->
if length(notlang) then
-- filter already done in specified language
string langurl = "http://rosettacode.org/wiki/Category:"&notlang
sequence done = dewiki(open_download(notlang&".htm",langurl))
integer k = 0
for i=1 to length(tasks) do
if not find(tasks[i],done) then
k += 1
tasks[k] = tasks[i]
end if
end for
tasks = tasks[1..k]
done = {}
end if
if not summary then
-- replace with contents
for i=1 to length(tasks) do
string ti = tasks[i],
url = sprintf("http://rosettacode.org/wiki/%s",{ti}),
contents = open_download(ti&".htm",url)
integer start = match(`</div>`,contents,match(`<div class="infobox"`,contents))+length(`</div>`)
integer finish = match(`<div id="toc"`,contents,start)-1
-- ... but draft tasks with too few languages have no toc:
if finish=-1 then finish = match(`<h2>`,contents,start)-1 end if
-- ... and if no languages at all, use the footer:
if finish=-1 then finish = match(`</div><div class="printfooter">`,contents,start)-1 end if
if finish=-1 then ?9/0 end if
contents = contents[start..finish]
ti = substitute(ti,"_"," ")
if not match("<b>"&ti&"</b>",contents) then
-- (ps: I refuse to panic over the occasional replicated header...)
contents = sprintf("<h3>%s</h3>%s",{ti,contents})
end if
tasks[i] = contents
if get_key()=#1B then exit end if
end for
end if
if curl!=NULL then
curl_easy_cleanup(curl)
free(pErrorBuffer)
curl = NULL
pErrorBuffer = NULL
end if
return tasks
end function
 
function html_clean(string ri)
ri = substitute(ri,"%3A",":")
ri = substitute(ri,"%E2%80%93","-")
ri = substitute(ri,"%E2%80%99","'")
ri = substitute(ri,"%27","'")
ri = substitute(ri,"%2B","+")
ri = substitute(ri,"%C3%A8","e")
ri = substitute(ri,"%C3%A9","e")
ri = substitute(ri,"%22","\"")
ri = substitute(ri,"%2A","*")
return ri
end function
 
constant html_header = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Rosettacode Tasks without examples</title>
</head>
<body>
<h2>Rosettacode Tasks without examples</h2>
Generated %s, %d entries<br><br>
""",
html_footer = """
</body>
</html>
"""
 
sequence results = extract_tasks()
if output_html then
integer fn = open("Tasks_Without_Examples.html","w")
printf(fn,html_header,{format_timedate(date()),length(results)})
for i=1 to length(results) do
printf(fn,"%s<br>",html_clean(results[i]))
end for
puts(fn,html_footer)
close(fn)
else
for i=1 to length(results) do
printf(1,"%s\n",html_clean(results[i]))
end for
end if
 
?"done"
{} = wait_key()</lang>
 
=={{header|Raku}}==
Line 606 ⟶ 664:
{{works with|Rakudo|2017.10}}
 
<syntaxhighlight lang="raku" perl6line>use HTTP::UserAgent;
use Gumbo;
 
Line 614 ⟶ 672:
# Get list of Tasks
say "Updating Programming_Tasks list...";
my $page = "httphttps://rosettacode.org/wiki/Category:Programming_Tasks";
my $html = $ua.get($page).content;
my $xmldoc = parse-html($html, :TAG<div>, :id<mw-pages>);
Line 636 ⟶ 694:
 
# Get the raw page
my $html = $ua.get: "httphttps://rosettacode.org/wiki/{$title}";
 
# Filter out the actual task description
Line 657 ⟶ 715:
sub cleanup ( $string ) {
$string.subst( /^.+ '</div>'/, '' )
}</langsyntaxhighlight>
{{out|Abridged sample output}}
<div style="border-style: groove; margin: 50px; padding: 25px;">
Line 743 ⟶ 801:
 
Could output text by retrieving innerText instead of innerHTML in Slurp().
<langsyntaxhighlight lang="vbscript">Option Explicit
 
Dim oIE : Set oIE = CreateObject("InternetExplorer.Application")
Line 818 ⟶ 876:
 
End Function
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{libheader|libcurl}}
{{libheader|Wren-pattern}}
An embedded program so we can use the libcurl library.
<syntaxhighlight lang="wren">/* Rosetta_Code_Tasks_without_examples.wren */
 
import "./pattern" for Pattern
 
var CURLOPT_URL = 10002
var CURLOPT_FOLLOWLOCATION = 52
var CURLOPT_WRITEFUNCTION = 20011
var CURLOPT_WRITEDATA = 10001
 
foreign class Buffer {
construct new() {} // C will allocate buffer of a suitable size
 
foreign value // returns buffer contents as a string
}
 
foreign class Curl {
construct easyInit() {}
 
foreign easySetOpt(opt, param)
 
foreign easyPerform()
 
foreign easyCleanup()
}
 
var curl = Curl.easyInit()
 
var getContent = Fn.new { |url|
var buffer = Buffer.new()
curl.easySetOpt(CURLOPT_URL, url)
curl.easySetOpt(CURLOPT_FOLLOWLOCATION, 1)
curl.easySetOpt(CURLOPT_WRITEFUNCTION, 0) // write function to be supplied by C
curl.easySetOpt(CURLOPT_WRITEDATA, buffer)
curl.easyPerform()
return buffer.value
}
 
var unescs = [
["_", " "],
["\%252B", "+"],
["\%27", "'"],
["\%C3\%A9", "é"],
["\%E2\%80\%93", "–"],
["\%22", "\""],
["\%C3\%B6", "ö"],
["\%E2\%80\%99", "’"],
["\%C3\%A8", "è"],
["\%C5\%91", "ő"],
]
 
var unescape = Fn.new { |text|
for (u in unescs) text = text.replace(u[0], u[1])
return text
}
 
var url = "https://rosettacode.org/wiki/Category:Programming_Tasks"
var content = getContent.call(url)
var p1 = Pattern.new("<li><a href/=\"//wiki//[+1^\"]\"")
var p2 = Pattern.new("<~//+0^>>")
var matches = p1.findAll(content)
var tasks = matches.map { |m| m.capsText[0] }.toList
for (task in tasks.take(3)) { // just show the first 3 say
var taskUrl = "https://rosettacode.org/wiki/" + task
var html = getContent.call(taskUrl)
var text = "using any language you may know.</div>"
var start = html.indexOf(text)
var end = html.indexOf("<meta property=\"mw:PageProp/toc\" />")
html = html[start + text.count...end]
text = p2.replaceAll(html, "").replace("&#160;", "").trim()
var title = unescape.call(task)
System.print("\n****** %(title) ******\n")
System.print(text)
}</syntaxhighlight>
<br>
which we embed in the following C program, build and run.
<syntaxhighlight lang="c">/* gcc Rosetta_Code_Tasks_without_examples.c -o Rosetta_Code_Tasks_without_examples -lcurl -lwren -lm */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "wren.h"
 
struct MemoryStruct {
char *memory;
size_t size;
};
 
/* C <=> Wren interface functions */
 
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
 
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
 
void C_bufferAllocate(WrenVM* vm) {
struct MemoryStruct *ms = (struct MemoryStruct *)wrenSetSlotNewForeign(vm, 0, 0, sizeof(struct MemoryStruct));
ms->memory = malloc(1);
ms->size = 0;
}
 
void C_bufferFinalize(void* data) {
struct MemoryStruct *ms = (struct MemoryStruct *)data;
free(ms->memory);
}
 
void C_curlAllocate(WrenVM* vm) {
CURL** pcurl = (CURL**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(CURL*));
*pcurl = curl_easy_init();
}
 
void C_value(WrenVM* vm) {
struct MemoryStruct *ms = (struct MemoryStruct *)wrenGetSlotForeign(vm, 0);
wrenSetSlotString(vm, 0, ms->memory);
}
 
void C_easyPerform(WrenVM* vm) {
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0);
curl_easy_perform(curl);
}
 
void C_easyCleanup(WrenVM* vm) {
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0);
curl_easy_cleanup(curl);
}
 
void C_easySetOpt(WrenVM* vm) {
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0);
CURLoption opt = (CURLoption)wrenGetSlotDouble(vm, 1);
if (opt < 10000) {
long lparam = (long)wrenGetSlotDouble(vm, 2);
curl_easy_setopt(curl, opt, lparam);
} else if (opt < 20000) {
if (opt == CURLOPT_WRITEDATA) {
struct MemoryStruct *ms = (struct MemoryStruct *)wrenGetSlotForeign(vm, 2);
curl_easy_setopt(curl, opt, (void *)ms);
} else if (opt == CURLOPT_URL) {
const char *url = wrenGetSlotString(vm, 2);
curl_easy_setopt(curl, opt, url);
}
} else if (opt < 30000) {
if (opt == CURLOPT_WRITEFUNCTION) {
curl_easy_setopt(curl, opt, &WriteMemoryCallback);
}
}
}
 
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.allocate = NULL;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Buffer") == 0) {
methods.allocate = C_bufferAllocate;
methods.finalize = C_bufferFinalize;
} else if (strcmp(className, "Curl") == 0) {
methods.allocate = C_curlAllocate;
}
}
return methods;
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Buffer") == 0) {
if (!isStatic && strcmp(signature, "value") == 0) return C_value;
} else if (strcmp(className, "Curl") == 0) {
if (!isStatic && strcmp(signature, "easySetOpt(_,_)") == 0) return C_easySetOpt;
if (!isStatic && strcmp(signature, "easyPerform()") == 0) return C_easyPerform;
if (!isStatic && strcmp(signature, "easyCleanup()") == 0) return C_easyCleanup;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) {
if( result.source) free((void*)result.source);
}
 
WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) {
WrenLoadModuleResult result = {0};
if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) {
result.onComplete = loadModuleComplete;
char fullName[strlen(name) + 6];
strcpy(fullName, name);
strcat(fullName, ".wren");
result.source = readFile(fullName);
}
return result;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
config.loadModuleFn = &loadModule;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Rosetta_Code_Tasks_without_examples.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
 
{{out}}
Just showing the first 3 tasks.
<pre>
****** 100 doors ******
 
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 toggle the door (if the door is closed, open it; if it is open, close it).
The second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it.
The third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
 
 
Task
 
Answer the question: what state are the doors in after the last pass? Which are open, which are closed?
 
Alternate:
As noted in this page's discussion page, the only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an optimization that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.
 
****** 100 prisoners ******
 
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.
 
****** 15 puzzle game ******
 
Task
 
Implement the Fifteen Puzzle Game.
 
The 15-puzzle is also known as:
 
Fifteen Puzzle
Gem Puzzle
Boss Puzzle
Game of Fifteen
Mystic Square
14-15 Puzzle
and some others.
 
 
Related Tasks
 
15 Puzzle Solver
16 Puzzle Game
</pre>
 
=={{header|zkl}}==
Line 825 ⟶ 1,231:
 
Uses libraries cURL and YAJL (yet another json library).
<langsyntaxhighlight lang="zkl">var [const] CURL=Import("zklCurl"), YAJL=Import("zklYAJL")[0];
 
fcn getTasks(language){
Line 870 ⟶ 1,276:
}
 
newTasks();</langsyntaxhighlight>
{{out|Concise Output}}
<pre>
9,476

edits