Rosetta Code/Tasks without examples: Difference between revisions

→‎{{header|Wren}}: Various changes including some to accommodate changes to the HTML.
m (→‎{{header|Phix}}: syntax coloured, use common code)
(→‎{{header|Wren}}: Various changes including some to accommodate changes to the HTML.)
 
(4 intermediate revisions by 3 users not shown)
Line 13:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.algorithm;
import std.net.curl;
import std.range;
Line 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 134:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 177:
time.Sleep(5 * time.Second) // wait 5 seconds before processing next task
}
}</langsyntaxhighlight>
 
{{out}}
Line 270:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Line 336:
tasks.stream().limit(limit).forEach(task -> process(client, base, task));
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">using HTTP
 
function gettaskdescriptions(numtoprint = 3)
Line 358:
 
gettaskdescriptions()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
Line 414:
}
}
}</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import htmlparser, httpclient, os, re, strutils, xmltree
 
let re1 = re("""<li><a href="/wiki/(.*?)"""")
Line 437:
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>(.*?)<div id="toc"mw:tocplace>""")
for i, task in tasks:
var matches: array[2, string]
Line 448:
echo xmlnode.innerText() # Echo the tree as text.
if i == Limit - 1: break
os.sleep(5000) # Wait 5 seconds before processing next task.</langsyntaxhighlight>
 
{{out}}
Line 537:
=={{header|Perl}}==
Slice and dice the HTML. Output is as for the other examples.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 553:
print "$title\n$task_at_hand\n\n";
sleep 10; # so you have time to read each task...
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 559:
Output similar to zkl, I assume the first four constants are self-explanatory.
{{libheader|Phix/libcurl}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Tasks_without_examples.exw</span>
<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>
Line 658:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Raku}}==
Line 664:
{{works with|Rakudo|2017.10}}
 
<syntaxhighlight lang="raku" perl6line>use HTTP::UserAgent;
use Gumbo;
 
Line 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 694:
 
# Get the raw page
my $html = $ua.get: "httphttps://rosettacode.org/wiki/{$title}";
 
# Filter out the actual task description
Line 715:
sub cleanup ( $string ) {
$string.subst( /^.+ '</div>'/, '' )
}</langsyntaxhighlight>
{{out|Abridged sample output}}
<div style="border-style: groove; margin: 50px; padding: 25px;">
Line 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 876:
 
End Function
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
Line 882:
{{libheader|Wren-pattern}}
An embedded program so we can use the libcurl library.
<syntaxhighlight lang="wren">/* Rosetta_Code_Tasks_without_examples.wren */
<lang ecmascript>/* rc_tasks_without_examples.wren */
 
import "./pattern" for Pattern
Line 921:
var unescs = [
["_", " "],
["\%2B252B", "+"],
["\%27", "'"],
["\%C3\%A9", "é"],
Line 937:
}
 
var url = "httphttps://rosettacode.org/wiki/Category:Programming_Tasks"
var content = getContent.call(url)
var p1 = Pattern.new("<li><a href/=\"//wiki//[+1^\"]\"")
Line 944:
var tasks = matches.map { |m| m.capsText[0] }.toList
for (task in tasks.take(3)) { // just show the first 3 say
var taskUrl = "httphttps://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("<divmeta idproperty=\"mw:PageProp/toc\" />")
html = html[start + text.count...end]
text = p2.replaceAll(html, "").replace("&#160;", "").trim()
Line 954:
System.print("\n****** %(title) ******\n")
System.print(text)
}</langsyntaxhighlight>
<br>
which we embed in the following C program, build and run.
<langsyntaxhighlight lang="c">/* gcc rc_tasks_without_examplesRosetta_Code_Tasks_without_examples.c -o rc_tasks_without_examplesRosetta_Code_Tasks_without_examples -lcurl -lwren -lm */
 
#include <stdio.h>
Line 1,131:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "rc_tasks_without_examplesRosetta_Code_Tasks_without_examples.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,147:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,154:
****** 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.
 
Line 1,165:
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.
 
Line 1,174:
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.
Line 1,198:
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 ******
Line 1,209:
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>
 
Line 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 1,276:
}
 
newTasks();</langsyntaxhighlight>
{{out|Concise Output}}
<pre>
9,476

edits