Rosetta Code/Tasks without examples: Difference between revisions
(Added Go) |
|||
Line 294: | Line 294: | ||
</div> |
</div> |
||
=={{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. |
|||
<lang Phix>-- demo\rosetta\Tasks_without_examples.exw |
|||
constant output_html = true, |
|||
include_drafts = true, |
|||
summary = false, |
|||
notlang = "Phix" -- "" for all |
|||
include builtins\timedate.e |
|||
integer refresh_cache = timedelta(days:=30) -- 0 for always |
|||
include builtins\libcurl.e |
|||
atom curl = NULL |
|||
atom pErrorBuffer |
|||
function write_callback(atom pData, integer size, integer nmemb, integer fn) |
|||
integer bytes_written = size * nmemb |
|||
puts(fn,peek({pData,bytes_written})) |
|||
return bytes_written |
|||
end function |
|||
constant write_cb = call_back({'+', routine_id("write_callback")}) |
|||
function open_download(string filename, url) |
|||
bool refetch = true |
|||
if get_file_type("rc_cache")!=FILETYPE_DIRECTORY then |
|||
if not create_directory("rc_cache") then |
|||
crash("cannot create rc_cache directory") |
|||
end if |
|||
end if |
|||
filename = join_path({"rc_cache",filename}) |
|||
if file_exists(filename) then |
|||
-- use existing file if <= refresh_cache (30+ days) old |
|||
sequence last_mod = get_file_date(filename) -- (0.8.1+) |
|||
atom delta = timedate_diff(last_mod,date()) |
|||
refetch = (delta>refresh_cache) |
|||
else |
|||
string directory = get_file_path(filename) |
|||
if get_file_type(directory)!=FILETYPE_DIRECTORY then |
|||
if not create_directory(directory,make_parent:=true) then |
|||
crash("cannot create %s directory",{directory}) |
|||
end if |
|||
end if |
|||
end if |
|||
if refetch then |
|||
printf(1,"Downloading %s...\n",{filename}) |
|||
if curl=NULL then |
|||
curl_global_init() |
|||
curl = curl_easy_init() |
|||
pErrorBuffer = allocate(CURL_ERROR_SIZE) |
|||
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, pErrorBuffer) |
|||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb) |
|||
end if |
|||
url = substitute(url,"%3A",":") |
|||
url = substitute(url,"%2A","*") |
|||
curl_easy_setopt(curl, CURLOPT_URL, url) |
|||
integer fn = open(filename,"wb") |
|||
if fn=-1 then ?9/0 end if |
|||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fn) |
|||
CURLcode res = curl_easy_perform(curl) |
|||
if res!=CURLE_OK then |
|||
string error = sprintf("%d",res) |
|||
if res=CURLE_COULDNT_RESOLVE_HOST then |
|||
error &= " [CURLE_COULDNT_RESOLVE_HOST]" |
|||
end if |
|||
printf(1, "Error %s downloading file\n", error) |
|||
{} = wait_key() |
|||
abort(0) |
|||
end if |
|||
close(fn) |
|||
refresh_cache += timedelta(days:=1) -- did I mention it is slow? |
|||
end if |
|||
return get_text(filename) |
|||
end function |
|||
function open_category(string filename) |
|||
return open_download(filename&".htm","http://rosettacode.org/wiki/Category:"&filename) |
|||
end function |
|||
function dewiki(string s) |
|||
sequence tasks = {} |
|||
integer start = 1, finish = match(`<div class="printfooter">`,s) |
|||
s = s[1..finish-1] |
|||
while true do |
|||
start = match("<li><a href=\"/wiki/",s,start) |
|||
if start=0 then exit end if |
|||
start += length("<li><a href=\"/wiki/") |
|||
finish = find('"',s,start) |
|||
string task = s[start..finish-1] |
|||
task = substitute(task,"*","%2A") |
|||
task = substitute(task,":","%3A") |
|||
tasks = append(tasks,task) |
|||
start = finish+1 |
|||
end while |
|||
return tasks |
|||
end function |
|||
function extract_tasks() |
|||
-- extract tasks from eg `<li><a href="/wiki/100_doors"` |
|||
sequence tasks = dewiki(open_category("Programming_Tasks")) |
|||
if include_drafts then |
|||
tasks &= dewiki(open_category("Draft_Programming_Tasks")) |
|||
end if |
|||
if length(notlang) then |
|||
-- filter already done in specified language |
|||
string langurl = "http://rosettacode.org/wiki/Category:"¬lang |
|||
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|VBScript}}== |
=={{header|VBScript}}== |
Revision as of 00:10, 10 May 2019
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".
Go
<lang go>package main
import (
"fmt" "html" "io/ioutil" "net/http" "regexp" "strings" "time"
)
func main() {
ex := `
ex2 := `</?[^>]*>` // to remove all tags including links re = regexp.MustCompile(ex) re2 := regexp.MustCompile(ex2) for i, task := range tasks { page = base + task resp, _ = http.Get(page) body, _ = ioutil.ReadAll(resp.Body) match := re.FindStringSubmatch(string(body)) resp.Body.Close() text := html.UnescapeString(re2.ReplaceAllLiteralString(match[1], "")) fmt.Println(strings.Replace(task, "_", " ", -1), "\n", text) if i == limit-1 { break } time.Sleep(5 * time.Second) // wait 5 seconds before processing next task }
}</lang>
- Output:
Text rather than HTML:
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. 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 many others. Related Tasks 15 Puzzle Solver 16 Puzzle Game 15 puzzle solver Your task is to write a program that finds a solution in the fewest moves possible single moves to a random Fifteen Puzzle Game. 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 Solution: 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 two solutions, of fifty-two moves: rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd rrruldluuldrurdddluulurrrdlddruldluurddlulurruldrrdd see: Pretty Print of Optimal Solution Finding either one, or both is an acceptable result. Extra credit. Solve the following problem: 0 12 9 13 15 11 10 14 3 7 2 5 4 8 6 1 Related Task 15 puzzle game A* search algorithm
Perl
Slice and dice the HTML. Output is as for the other examples. <lang perl>use strict; use warnings;
use LWP::UserAgent; my $ua = LWP::UserAgent->new;
- get list of task titles
my $html = $ua->request( HTTP::Request->new( GET => 'http://rosettacode.org/wiki/Category:Programming_Tasks'))->content;
my @tasks = $html =~ m#- download tasks, and extract task descriptions
print "$title\n$task_at_hand\n\n"; sleep 10; # so you have time to read each task...
}</lang>
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, :TAGmy $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...
Phix
Since downloading all the pages can be very slow, this uses a cache. Limiting by "Phix" fairly obviously speeds it up tenfold :-)
Output similar to zkl, I assume the first four constants are self-explanatory.
<lang Phix>-- demo\rosetta\Tasks_without_examples.exw
constant output_html = true,
include_drafts = true, summary = false, notlang = "Phix" -- "" for all
include builtins\timedate.e integer refresh_cache = timedelta(days:=30) -- 0 for always
include builtins\libcurl.e atom curl = NULL atom pErrorBuffer
function write_callback(atom pData, integer size, integer nmemb, integer fn)
integer bytes_written = size * nmemb puts(fn,peek({pData,bytes_written})) return bytes_written
end function constant write_cb = call_back({'+', routine_id("write_callback")})
function open_download(string filename, url)
bool refetch = true if get_file_type("rc_cache")!=FILETYPE_DIRECTORY then if not create_directory("rc_cache") then crash("cannot create rc_cache directory") end if end if filename = join_path({"rc_cache",filename}) if file_exists(filename) then -- use existing file if <= refresh_cache (30+ days) old sequence last_mod = get_file_date(filename) -- (0.8.1+) atom delta = timedate_diff(last_mod,date()) refetch = (delta>refresh_cache) else string directory = get_file_path(filename) if get_file_type(directory)!=FILETYPE_DIRECTORY then if not create_directory(directory,make_parent:=true) then crash("cannot create %s directory",{directory}) end if end if end if if refetch then printf(1,"Downloading %s...\n",{filename}) if curl=NULL then curl_global_init() curl = curl_easy_init() pErrorBuffer = allocate(CURL_ERROR_SIZE) curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, pErrorBuffer) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb) end if url = substitute(url,"%3A",":") url = substitute(url,"%2A","*") curl_easy_setopt(curl, CURLOPT_URL, url) integer fn = open(filename,"wb") if fn=-1 then ?9/0 end if curl_easy_setopt(curl, CURLOPT_WRITEDATA, fn) CURLcode res = curl_easy_perform(curl) if res!=CURLE_OK then string error = sprintf("%d",res) if res=CURLE_COULDNT_RESOLVE_HOST then error &= " [CURLE_COULDNT_RESOLVE_HOST]" end if printf(1, "Error %s downloading file\n", error) {} = wait_key() abort(0) end if close(fn) refresh_cache += timedelta(days:=1) -- did I mention it is slow? end if return get_text(filename)
end function
function open_category(string filename)
return open_download(filename&".htm","http://rosettacode.org/wiki/Category:"&filename)
end function
function dewiki(string s)
sequence tasks = {}integer start = 1, finish = match(`
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(`