Rosetta Code/Tasks without examples: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
|||
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|Perl 6}}== |
|||
{{works with|Rakudo|2017.10}} |
|||
<lang perl6>use HTTP::UserAgent; |
|||
use Gumbo; |
|||
my $ua = HTTP::UserAgent.new; |
|||
my $taskfile = './RC_tasks.html'; |
|||
# Get list of Tasks |
|||
say "Updating Programming_Tasks list..."; |
|||
my $page = "http://rosettacode.org/wiki/Category:Programming_Tasks"; |
|||
my $html = $ua.get($page).content; |
|||
my $xmldoc = parse-html($html, :TAG<div>, :id<mw-pages>); |
|||
my @tasks = parse-html($xmldoc[0].Str, :TAG<li>).Str.comb( /'/wiki/' <-["]>+ / )».substr(6); #" |
|||
my $f = open("./RC_Programming_Tasks.txt", :w) or die "$!\n"; |
|||
note "Writing Programming_Tasks file..."; |
|||
$f.print( @tasks.join("\n") ); |
|||
$f.close; |
|||
sleep .5; |
|||
for 'Programming_Tasks' -> $category |
|||
{ # Scrape info from each page. |
|||
note "Loading $category file..."; |
|||
note "Retreiving tasks..."; |
|||
my @entries = "./RC_{$category}.txt".IO.slurp.lines; |
|||
for @entries -> $title { |
|||
note $title; |
|||
# Get the raw page |
|||
my $html = $ua.get: "http://rosettacode.org/wiki/{$title}"; |
|||
# Filter out the actual task description |
|||
$html.content ~~ m|'<div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div' |
|||
.+? 'using any language you may know.</div>' (.+?) '<div id="toc"'|; |
|||
my $task = cleanup $0.Str; |
|||
# save to a file |
|||
my $fh = $taskfile.IO.open :a; |
|||
$fh.put: "<hr>\n $title\n<hr>\n$task"; |
|||
$fh.close; |
|||
sleep 3; # Don't pound the server |
|||
} |
|||
} |
|||
sub cleanup ( $string ) { |
|||
$string.subst( /^.+ '</div>'/, '' ) |
|||
}</lang> |
|||
{{out|Abridged sample output}} |
|||
<div style="border-style: groove; margin: 50px; padding: 25px;"> |
|||
<hr> |
|||
100_doors |
|||
<hr> |
|||
<p>There are 100 doors in a row that are all initially closed. |
|||
</p><p>You make 100 <a href="/wiki/Rosetta_Code:Multiple_passes" title="Rosetta Code:Multiple passes">passes</a> by the doors. |
|||
</p><p>The first time through, visit every door and  <i>toggle</i>  the door  (if the door is closed,  open it;   if it is open,  close it). |
|||
</p><p>The second time, only visit every 2<sup>nd</sup> door   (door #2, #4, #6, ...),   and toggle it. |
|||
</p><p>The third time, visit every 3<sup>rd</sup> door   (door #3, #6, #9, ...), etc,   until you only visit the 100<sup>th</sup> door. |
|||
</p><p><br /> |
|||
</p> |
|||
<dl><dt>Task</dt> |
|||
<dd></dd></dl> |
|||
<p>Answer the question:   what state are the doors in after the last pass?   Which are open, which are closed? |
|||
</p><p><br /> |
|||
<b><a href="/wiki/Rosetta_Code:Extra_credit" title="Rosetta Code:Extra credit">Alternate</a>:</b> |
|||
As noted in this page's   <a href="/wiki/Talk:100_doors" title="Talk:100 doors">discussion page</a>,   the only doors that remain open are those whose numbers are perfect squares. |
|||
</p><p>Opening only those doors is an   <a href="/wiki/Rosetta_Code:Optimization" title="Rosetta Code:Optimization">optimization</a>   that may also be expressed; |
|||
however, as should be obvious, this defeats the intent of comparing implementations across programming languages. |
|||
<br /><br /> |
|||
</p> |
|||
<hr> |
|||
15_Puzzle_Game |
|||
<hr> |
|||
<dl><dt>Task</dt> |
|||
<dd></dd></dl> |
|||
<p>Implement the <a href="http://en.wikipedia.org/wiki/15_puzzle" class="extiw" title="wp:15 puzzle">Fifteen Puzzle Game</a>. |
|||
<br /><br /> |
|||
</p> |
|||
<dl><dt>Related Task</dt> |
|||
<dd></dd></dl> |
|||
<ul><li> <a href="/wiki/15_puzzle_solver" title="15 puzzle solver">15 Puzzle Solver</a></li></ul> |
|||
<p><br /><br /> |
|||
</p> |
|||
<hr> |
|||
15_puzzle_solver |
|||
<hr> |
|||
<p>Your task is to write a program that finds a solution in the fewest single moves (no multimoves) possible to a random <a href="http://en.wikipedia.org/wiki/15_puzzle" class="extiw" title="wp:15 puzzle">Fifteen Puzzle Game</a>.<br /> |
|||
For this task you will be using the following puzzle:<br /> |
|||
</p> |
|||
<pre>15 14 1 6 |
|||
9 11 4 12 |
|||
0 10 7 3 |
|||
13 8 5 2</pre> |
|||
<p><br /> |
|||
</p> |
|||
Solution:<pre> 1 2 3 4 |
|||
5 6 7 8 |
|||
9 10 11 12 |
|||
13 14 15 0 |
|||
</pre> |
|||
<p>The output must show the moves' directions, like so: left, left, left, down, right... and so on.<br /> |
|||
There are 2 solutions with 52 moves:<br /> |
|||
rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd<br /> |
|||
rrruldluuldrurdddluulurrrdlddruldluurddlulurruldrrdd<br /> |
|||
finding either one, or both is an acceptable result.<br /> |
|||
see: <a rel="nofollow" class="external text" href="http://www.rosettacode.org/wiki/15_puzzle_solver/Optimal_solution">Pretty Print of Optimal Solution</a> |
|||
</p> |
|||
<dl><dt>Extra credit.</dt></dl> |
|||
<p>Solve the following problem: |
|||
</p> |
|||
<pre> 0 12 9 13 |
|||
15 11 10 14 |
|||
3 7 2 5 |
|||
4 8 6 1 |
|||
</pre> |
|||
<p><br /> |
|||
</p> |
|||
<dl><dt>Related Task</dt> |
|||
<dd></dd></dl> |
|||
<ul><li> <a href="/wiki/15_Puzzle_Game" title="15 Puzzle Game">15 puzzle game</a></li></ul> |
|||
<p><br /><br /><b>...and so on... </b> |
|||
</p> |
|||
</div> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
Revision as of 21:30, 19 November 2017
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.
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".
Perl 6
<lang perl6>use HTTP::UserAgent; use Gumbo;
my $ua = HTTP::UserAgent.new; my $taskfile = './RC_tasks.html';
- Get list of Tasks
say "Updating Programming_Tasks list..."; my $page = "http://rosettacode.org/wiki/Category:Programming_Tasks"; my $html = $ua.get($page).content;
my $xmldoc = parse-html($html, :TAG
my $task = cleanup $0.Str;
# save to a file my $fh = $taskfile.IO.open :a;$fh.put: "
\n $title\n
\n$task";
$fh.close;
sleep 3; # Don't pound the server }
}
sub cleanup ( $string ) {
$string.subst( /^.+ ''/, )
}</lang>
- Abridged sample output:
100_doors
There are 100 doors in a row that are all initially closed.
You make 100 <a href="/wiki/Rosetta_Code:Multiple_passes" title="Rosetta Code:Multiple passes">passes</a> 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?
<a href="/wiki/Rosetta_Code:Extra_credit" title="Rosetta Code:Extra credit">Alternate</a>: As noted in this page's <a href="/wiki/Talk:100_doors" title="Talk:100 doors">discussion page</a>, the only doors that remain open are those whose numbers are perfect squares.
Opening only those doors is an <a href="/wiki/Rosetta_Code:Optimization" title="Rosetta Code:Optimization">optimization</a> that may also be expressed;
however, as should be obvious, this defeats the intent of comparing implementations across programming languages.
15_Puzzle_Game
- Task
Implement the <a href="http://en.wikipedia.org/wiki/15_puzzle" class="extiw" title="wp:15 puzzle">Fifteen Puzzle Game</a>.
- Related Task
- <a href="/wiki/15_puzzle_solver" title="15 puzzle solver">15 Puzzle Solver</a>
15_puzzle_solver
Your task is to write a program that finds a solution in the fewest single moves (no multimoves) possible to a random <a href="http://en.wikipedia.org/wiki/15_puzzle" class="extiw" title="wp:15 puzzle">Fifteen Puzzle Game</a>.
For this task you will be using the following puzzle:
15 14 1 6 9 11 4 12 0 10 7 3 13 8 5 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
The output must show the moves' directions, like so: left, left, left, down, right... and so on.
There are 2 solutions with 52 moves:
rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd
rrruldluuldrurdddluulurrrdlddruldluurddlulurruldrrdd
finding either one, or both is an acceptable result.
see: <a rel="nofollow" class="external text" href="http://www.rosettacode.org/wiki/15_puzzle_solver/Optimal_solution">Pretty Print of Optimal Solution</a>
- Extra credit.
Solve the following problem:
0 12 9 13 15 11 10 14 3 7 2 5 4 8 6 1
- Related Task
- <a href="/wiki/15_Puzzle_Game" title="15 Puzzle Game">15 puzzle game</a>
...and so on...
VBScript
Uses Internet Explorer (v9 and above) to traverse the DOM in the list page and subsequent task pages. Echoes title and href to the console. Outputs files containing html.
Could output text by retrieving innerText instead of innerHTML in Slurp(). <lang vbscript>Option Explicit
Dim oIE : Set oIE = CreateObject("InternetExplorer.Application")
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oRE : Set oRE = New RegExp oRE.Pattern = "class=[" & Chr(34) & "'](.*?)[" & Chr(34) & "']" oRE.IgnoreCase = True
oIE.Navigate "http://rosettacode.org/wiki/Category:Programming_Tasks" While oIE.Busy WScript.Sleep 100 Wend
Dim oDoc : Set oDoc = oIE.Document Dim oDict : Set oDict = CreateObject("Scripting.Dictionary")
' build a dictionary of anchors Dim oAnchor Dim oAnchors : Set oAnchors = oDoc.getElementById("mw-pages").getElementsByTagName("a") For Each oAnchor In oAnchors oDict.Add oAnchor.innerText, oAnchor.href Next
'traverse the dictionary of anchors Dim aKeys : aKeys = oDict.Keys() Dim aKey For Each aKey In aKeys Slurp aKey, oDict(aKey) Next
oIE.Quit
Function Slurp(sTitle, sHref) WScript.Echo sTitle, sHref
oIE.Navigate sHref While oIE.Busy WScript.Sleep 100 Wend
Dim oDoc : Set oDoc = oIE.Document
Dim oStart : Set oStart = oDoc.getElementsByClassName("infobox")(0) Dim oCursor : Set oCursor = oStart Dim sDescription : sDescription = vbNullString Dim oThere Dim iErr
Do While oCursor.tagName <> "TABLE" And oCursor.id <> "toc" Set oThere = oCursor sDescription = sDescription & oThere.innerHTML & vbNewLine On Error Resume Next Set oCursor = oCursor.nextElementSibling iErr = Err.Number On Error Goto 0 If iErr <> 0 Then Exit Do End If Loop
dim sTitle2 sTitle2 = Replace(sTitle,"/","_") sTitle2 = Replace(sTitle2,Chr(34),"'") sTitle2 = Replace(sTitle2,"*",".")
Dim oHandle : Set oHandle = oFSO.CreateTextFile(sTitle2 & ".htm", True, True)
oHandle.Write "<a href='" & sHref & "'>" & sTitle & "</a>
"
oHandle.Write sDescription
oHandle.Close
oIE.Stop
End Function </lang>