Rosetta Code/Count examples: Difference between revisions
m (Ada : simplified code) |
m (→{{header|Ada}}: Adjust to Rosetta/Find unimplemented task) |
||
Line 11: | Line 11: | ||
Total: X examples.</lang> |
Total: X examples.</lang> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{libheader|AWS}} |
|||
Libraries : Ada Web server (AWS) and XMLAda from AdaCore |
|||
Parsing XML file with XMLAda from AdaCore |
|||
<lang Ada>with AWS.Response; use AWS.Response; |
|||
<lang Ada>with Aws.Client, Aws.Messages, Aws.Response, Aws.Resources, Aws.Url; |
|||
with AWS.Resources; use AWS.Resources; |
|||
with Dom.Readers, Dom.Core, Dom.Core.Documents, Dom.Core.Nodes, Dom.Core.Attrs; |
|||
with AWS.Messages; use AWS.Messages; |
|||
⚫ | |||
with AWS.URL; |
|||
⚫ | |||
with DOM.Readers; use DOM.Readers; |
|||
with DOM.Core; use DOM.Core; |
|||
with DOM.Core.Documents; use DOM.Core.Documents; |
|||
with DOM.Core.Nodes; use DOM.Core.Nodes; |
|||
with DOM.Core.Attrs; use DOM.Core.Attrs; |
|||
with AWS.Client; use AWS.Client; |
|||
⚫ | |||
with Unicode; |
|||
with Unicode.CES.Utf8; |
|||
⚫ | |||
⚫ | |||
with Ada.Containers.Vectors; |
with Ada.Containers.Vectors; |
||
use Ada, AWS; |
|||
use Aws.Client, Aws.Messages, Aws.Response, Aws.Resources, Aws.Url; |
|||
⚫ | |||
use Dom.Readers, Dom.Core, Dom.Core.Documents, Dom.Core.Nodes, Dom.Core.Attrs; |
|||
use Aws, Ada.Strings.Unbounded, Ada.Strings.Fixed, Input_Sources.Strings; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Index_Type => Positive, |
Index_Type => Positive, |
||
Element_Type => Unbounded_String); |
Element_Type => Unbounded_String); |
||
use |
use Members_Vectors; |
||
Exemples : Vector; |
Exemples : Vector; |
||
Nbr_Lg, Total : Natural := 0; |
Nbr_Lg, Total : Natural := 0; |
||
procedure |
procedure Get_Vector (Category : in String; Mbr_Vector : in out Vector) is |
||
Reader : Tree_Reader; |
Reader : Tree_Reader; |
||
Doc : Document; |
Doc : Document; |
||
Line 46: | Line 40: | ||
N : Node; |
N : Node; |
||
A : Attr; |
A : Attr; |
||
Page : |
Page : Aws.Response.Data; |
||
Uri_Xml : constant String := |
|||
"http://rosettacode.org/mw/api.php?action=query&list=categorymembers |
"http://rosettacode.org/mw/api.php?action=query&list=categorymembers" |
||
& |
& |
||
" |
"&format=xml&cmlimit=500&cmtitle=Category:"; |
||
begin |
begin |
||
Page := Client.Get ( |
Page := Client.Get (Uri_Xml & Category); |
||
if Response.Status_Code (Page) not in Success then |
if Response.Status_Code (Page) not in Messages.Success then |
||
raise Client.Connection_Error; |
raise Client.Connection_Error; |
||
end if; |
end if; |
||
declare |
declare |
||
Xml : constant String := Message_Body (Page); |
|||
Source : String_Input; |
Source : String_Input; |
||
begin |
begin |
||
Open |
Open |
||
( |
(Xml'Unrestricted_Access, |
||
Unicode. |
Unicode.Ces.Utf8.Utf8_Encoding, |
||
Source); |
Source); |
||
Parse (Reader, Source); |
Parse (Reader, Source); |
||
Line 72: | Line 66: | ||
N := Item (List, Index - 1); |
N := Item (List, Index - 1); |
||
A := Get_Named_Item (Attributes (N), "title"); |
A := Get_Named_Item (Attributes (N), "title"); |
||
Append ( |
Append (Mbr_Vector, To_Unbounded_String (Value (A))); |
||
end loop; |
end loop; |
||
Free (List); |
Free (List); |
||
Free (Reader); |
Free (Reader); |
||
end |
end Get_Vector; |
||
function Scan_Page (Title : String) return Natural is |
function Scan_Page (Title : String) return Natural is |
||
Page : |
Page : Aws.Response.Data; |
||
File : |
File : Aws.Resources.File_Type; |
||
Buffer : String (1 .. 1024); |
Buffer : String (1 .. 1024); |
||
Languages, Position, Last : Natural := 0; |
Languages, Position, Last : Natural := 0; |
||
Line 87: | Line 81: | ||
Client.Get |
Client.Get |
||
("http://rosettacode.org/mw/index.php?title=" & |
("http://rosettacode.org/mw/index.php?title=" & |
||
Aws.Url.Encode (Title) & |
|||
"&action=raw"); |
"&action=raw"); |
||
Response.Message_Body (Page, File); |
Response.Message_Body (Page, File); |
||
Line 93: | Line 87: | ||
Resources.Get_Line (File, Buffer, Last); |
Resources.Get_Line (File, Buffer, Last); |
||
Position := |
Position := |
||
Index |
|||
(Source => Buffer (Buffer'First .. Last), |
(Source => Buffer (Buffer'First .. Last), |
||
Pattern => "=={{header|"); |
Pattern => "=={{header|"); |
||
Line 105: | Line 99: | ||
begin |
begin |
||
Get_Vector ("Programming_Tasks", Exemples); |
|||
Get_List; |
|||
for I in First_Index (Exemples) .. Last_Index (Exemples) loop |
for I in First_Index (Exemples) .. Last_Index (Exemples) loop |
||
declare |
declare |
||
Title : constant String := |
Title : constant String := |
||
To_String ( |
To_String (Members_Vectors.Element (Exemples, I)); |
||
begin |
begin |
||
Nbr_Lg := Scan_Page (Title); |
Nbr_Lg := Scan_Page (Title); |
||
Total := Total + Nbr_Lg; |
Total := Total + Nbr_Lg; |
||
Put_Line (Title & " :" & Integer'Image (Nbr_Lg) & " exemples."); |
|||
(Title & " :" & Integer'Image (Nbr_Lg) & " exemples."); |
|||
end; |
end; |
||
end loop; |
end loop; |
||
Put_Line ("Total :" & Integer'Image (Total) & " exemples."); |
|||
end |
end Count_Examples; |
||
</lang> |
</lang> |
||
Output : |
|||
<lang>$>count_examples |
|||
<pre> |
|||
100 doors : 107 exemples. |
100 doors : 107 exemples. |
||
24 game : |
24 game : 30 exemples. |
||
⚫ | |||
24 game/Solve : 20 exemples. |
|||
99 Bottles of Beer : 124 exemples. |
|||
⚫ | |||
Yahoo! search interface : 10 exemples. |
Yahoo! search interface : 10 exemples. |
||
Zig-zag matrix : 49 exemples. |
Zig-zag matrix : 49 exemples. |
||
Total : |
Total : 17238 exemples.</pre> |
||
</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
Revision as of 19:51, 24 March 2011
You are encouraged to solve this task according to the task description, using any language you may know.
Find the total number of programming examples for each task and the total for all tasks.
Essentially, count the number of occurrences of =={{header| on each task page.
Output:
<lang>100 doors: 20 examples. 99 Bottles of Beer: 29 examples. Abstract type: 10 examples.
Total: X examples.</lang>
Ada
Parsing XML file with XMLAda from AdaCore <lang Ada>with Aws.Client, Aws.Messages, Aws.Response, Aws.Resources, Aws.Url; with Dom.Readers, Dom.Core, Dom.Core.Documents, Dom.Core.Nodes, Dom.Core.Attrs; with Input_Sources.Strings, Unicode, Unicode.Ces.Utf8; with Ada.Strings.Unbounded, Ada.Strings.Fixed, Ada.Text_IO, Ada.Command_Line; with Ada.Containers.Vectors;
use Aws.Client, Aws.Messages, Aws.Response, Aws.Resources, Aws.Url; use Dom.Readers, Dom.Core, Dom.Core.Documents, Dom.Core.Nodes, Dom.Core.Attrs; use Aws, Ada.Strings.Unbounded, Ada.Strings.Fixed, Input_Sources.Strings; use Ada.Text_IO, Ada.Command_Line;
procedure Count_Examples is
package Members_Vectors is new Ada.Containers.Vectors ( Index_Type => Positive, Element_Type => Unbounded_String); use Members_Vectors;
Exemples : Vector; Nbr_Lg, Total : Natural := 0;
procedure Get_Vector (Category : in String; Mbr_Vector : in out Vector) is Reader : Tree_Reader; Doc : Document; List : Node_List; N : Node; A : Attr; Page : Aws.Response.Data; Uri_Xml : constant String := "http://rosettacode.org/mw/api.php?action=query&list=categorymembers" & "&format=xml&cmlimit=500&cmtitle=Category:"; begin Page := Client.Get (Uri_Xml & Category); if Response.Status_Code (Page) not in Messages.Success then raise Client.Connection_Error; end if; declare Xml : constant String := Message_Body (Page); Source : String_Input; begin Open (Xml'Unrestricted_Access, Unicode.Ces.Utf8.Utf8_Encoding, Source); Parse (Reader, Source); Close (Source); end; Doc := Get_Tree (Reader); List := Get_Elements_By_Tag_Name (Doc, "cm"); for Index in 1 .. Length (List) loop N := Item (List, Index - 1); A := Get_Named_Item (Attributes (N), "title"); Append (Mbr_Vector, To_Unbounded_String (Value (A))); end loop; Free (List); Free (Reader); end Get_Vector;
function Scan_Page (Title : String) return Natural is Page : Aws.Response.Data; File : Aws.Resources.File_Type; Buffer : String (1 .. 1024); Languages, Position, Last : Natural := 0; begin Page := Client.Get ("http://rosettacode.org/mw/index.php?title=" & Aws.Url.Encode (Title) & "&action=raw"); Response.Message_Body (Page, File); while not End_Of_File (File) loop Resources.Get_Line (File, Buffer, Last); Position := Index (Source => Buffer (Buffer'First .. Last), Pattern => "=={{header|"); if Position > 0 then Languages := Languages + 1; end if; end loop; Close (File); return Languages; end Scan_Page;
begin
Get_Vector ("Programming_Tasks", Exemples);
for I in First_Index (Exemples) .. Last_Index (Exemples) loop declare Title : constant String := To_String (Members_Vectors.Element (Exemples, I)); begin Nbr_Lg := Scan_Page (Title); Total := Total + Nbr_Lg; Put_Line (Title & " :" & Integer'Image (Nbr_Lg) & " exemples."); end; end loop;
Put_Line ("Total :" & Integer'Image (Total) & " exemples.");
end Count_Examples; </lang> Output :
100 doors : 107 exemples. 24 game : 30 exemples. .... Yahoo! search interface : 10 exemples. Zig-zag matrix : 49 exemples. Total : 17238 exemples.
AutoHotkey
<lang AutoHotkey>UrlDownloadToFile
, http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml , tasks.xml
FileRead, tasks, tasks.xml pos = 0 quote = " ; " regtitle := "<cm.*?title=" . quote . "(.*?)" . quote While, pos := RegExMatch(tasks, regtitle, title, pos + 1) {
UrlDownloadToFile , % "http://www.rosettacode.org/w/index.php?title=" . title1 . "&action=raw" , task.xml FileRead, task, task.xml RegExReplace(task, "\{\{header\|", "", count) current := title1 . ": " . count . " examples.`n" output .= current TrayTip, current, % current
} MsgBox % output Return</lang>
C#
Object-oriented solution.
<lang csharp>using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Net;
class Task {
private string _task; private int _examples;
public Task(string task, int examples) { _task = task; _examples = examples; }
public string Name { get { return _task; } }
public int Examples { get { return _examples; } }
public override string ToString() { return String.Format("{0}: {1} examples.", this._task, this._examples); }
}
class Program {
static List<string> GetTitlesFromCategory(string category, WebClient wc) { string content = wc.DownloadString( String.Format("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:{0}&cmlimit=500&format=json", category) );
return new Regex("\"title\":\"(.+?)\"").Matches(content).Cast<Match>().Select(x => x.Groups[1].Value.Replace("\\/", "/")).ToList(); }
static string GetSourceCodeFromPage(string page, WebClient wc) { return wc.DownloadString( String.Format("http://www.rosettacode.org/w/index.php?title={0}&action=raw", page) ); }
static void Main(string[] args) { WebClient wc = new WebClient(); List<Task> tasks = new List<Task>(); List<string> tasknames = GetTitlesFromCategory("Programming_Tasks", wc);
foreach (string task in tasknames) { string content = GetSourceCodeFromPage(task, wc); int count = new Regex("=={{header", RegexOptions.IgnoreCase).Matches(content).Count; Task t = new Task(task, count);
Console.WriteLine(t); tasks.Add(t); }
Console.WriteLine("\nTotal: {0} examples.", tasks.Select(x => x.Examples).Sum()); }
}</lang>
Clojure
<lang clojure>(ns count-examples
(:import [java.net URLEncoder]) (:use [clojure.contrib.http.agent :only (http-agent string)] [clojure.contrib.json :only (read-json)] [clojure.contrib.string :only (join)]))
(defn url-encode [v] (URLEncoder/encode (str v) "utf-8"))
(defn rosettacode-get [path params]
(let [param-string (join "&" (for [[n v] params] (str (name n) "=" (url-encode v))))] (string (http-agent (format "http://www.rosettacode.org/w/%s?%s" path param-string)))))
(defn rosettacode-query [params]
(read-json (rosettacode-get "api.php" (merge {:action "query" :format "json"} params))))
(defn list-cm
([params] (list-cm params nil)) ([params continue] (let [cm-params (merge {:list "categorymembers"} params (or continue {})) result (rosettacode-query cm-params)] (concat (-> result (:query) (:categorymembers)) (if-let [cmcontinue (-> result (:query-continue) (:categorymembers))] (list-cm params cmcontinue))))))
(defn programming-tasks []
(let [result (list-cm {:cmtitle "Category:Programming_Tasks" :cmlimit 50})] (map #(:title %) result)))
(defn task-count [task]
[task (count (re-seq #"==\{\{header" (rosettacode-get "index.php" {:action "raw" :title task})))])
(defn print-result []
(let [task-counts (map task-count (programming-tasks))] (doseq [[task count] task-counts] (println (str task ":") count) (flush)) (println "Total: " (reduce #(+ %1 (second %2)) 0 task-counts))))
</lang> <lang clojure>count-examples> (print-result) 100 doors: 73 24 game: 18 24 game/Solve: 14 99 Bottles of Beer: 89 Abstract type: 27 Accumulator factory: 23 Ackermann function: 73 Active Directory/Connect: 4 Active Directory/Search for a user: 3 Active object: 14 Add a variable to a class instance at runtime: 21 Address of a variable: 20 ... Total: 11216 nil </lang>
D
<lang D> import tango.io.Stdout; import tango.net.http.HttpClient; import tango.net.http.HttpHeaders; import tango.text.xml.Document; import tango.text.Util;
alias HttpHeader.ContentLength CL;
auto url = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"; void main() {
auto client = new HttpClient (HttpClient.Get, url); client.open(); char[] mainData, tmp; int total, i;
void cat(void[] content) { tmp ~= cast(char[]) content; }
if (client.isResponseOK) { client.read(&cat, client.getResponseHeaders.getInt(CL)); mainData = tmp; tmp = null;
auto doc = new Document!(char); doc.parse(mainData); foreach (n; doc.query.descendant("cm").attribute("title")) { auto subClient = new HttpClient(HttpClient.Get, "http://www.rosettacode.org/w/index.php?title=" ~ replace(n.value.dup, ' ', '_') ~ "&action=raw"); subClient.open(); if (! subClient.isResponseOK) { Stderr (client.getResponse); break; } subClient.read(&cat, subClient.getResponseHeaders.getInt(CL)); foreach (segment; patterns(cast(char[])tmp, "==[[:Category:"" IsTitle} fun {$ A} {Atom.toString A.value} end} end fun {PercentEncode Xs} case Xs of nil then nil [] X|"" IsTitle} fun {$ A} {Atom.toString A.value} end} end fun {PercentEncode Xs} case Xs of nil then nil [] X]] [[Category:"" IsTitle} fun {$ A} {Atom.toString A.value} end} end fun {PercentEncode Xs} case Xs of nil then nil [] X]] Property "Implemented in language" (as page type) with input value """ IsTitle}</br> fun {$ A} {Atom.toString A.value} end}</br> end</br> </br> fun {PercentEncode Xs}</br> case Xs of nil then nil</br> [] X" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process. end end end fun {ToHex2 X} [{ToHex1 X div 16} {ToHex1 X mod 16}] end fun {ToHex1 X} if X >= 0 andthen X =< 9 then &0 + X elseif X >= 10 andthen X =< 15 then &A + X - 10 end end
in
{Main}</lang>
Example output:
100 doors: 86 examples. 24 game: 22 examples. 24 game/Solve: 15 examples. 99 Bottles of Beer: 108 examples. A+B: 59 examples. ... Xiaolin Wu's line algorithm: 5 examples. Y combinator: 29 examples. Yahoo! Search: 10 examples. Zig-zag matrix: 43 examples. Total: 14099 examples.
Perl
<lang Perl>
- !/usr/bin/perl -w
use strict ; use LWP::UserAgent ; use HTML::Parser ; use constant DOCROOT => "http://www.rosettacode.org/wiki" ; use constant SOLUTIONROOT => "http://www.rosettacode.org/w/index.php?title=" ; my %tasklist = ( ) ; #key: last part of solution list URL, value: title of solution my $ua = new LWP::UserAgent ; my $url = DOCROOT . "/Category:Programming_Tasks" ; my $request = HTTP::Request->new( 'GET' => "$url" ) ; my $response = $ua->request( $request ) ; my $counted = 0 ; my $total_examples = 0 ; my $solresponse ; my $p = HTML::Parser->new( api_version => 3 ) ; #parser for list of tasks my $q = HTML::Parser->new( api_version => 3 ) ; #parser for solutions by task $p->handler( start => \&process , "tagname , attr" ) ; $q->handler( text => \&langfinder, "text" ) ;
if ( $response->is_success( ) ) {
$p->parse( $response->content( ) ) ; foreach my $task( keys %tasklist ) { $request->uri( SOLUTIONROOT . "$task" . "&action=edit" ) ; $solresponse = $ua->request( $request ) ; if ( $solresponse->is_success( )) { $q->parse( $solresponse->content( ) ) ; if ( $tasklist{$task} ) { print "$tasklist{$task} : $counted examples!\n" ; } $counted = 0 ; $q->eof( ) ; } else { print "Error: " . $solresponse->code( ) . " " . $solresponse->message( ) . "\n" ; } } $p->eof( ) ; print "\nTotal: $total_examples examples.\n" ;
} else {
print "Error " . $response->code( ) . " " . $response->message( ) . "\n" ;
} sub process( ) {
return if shift ne "a" ; my $props = shift ; if ( $props->{href} && $props->{href} =~ m,/wiki/([^:]+), ) { if ( $1 !~ /Category/ ) { $tasklist{ $1 } = $props->{title} ; } }
} sub langfinder( ) {
my $text = shift ; while ( $text =~ /header\|.+\}/g ) { $counted++ ; $total_examples++ ; }
} </lang>
PicoLisp
<lang PicoLisp>(load "@lib/http.l")
(client "rosettacode.org" 80
"mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml"
(while (from " title=\"")
(let Task (till "\"")
(client "rosettacode.org" 80 (pack "wiki/" (replace Task " " "_"))
(let Cnt 0
(while (from "")
(unless (sub? "." (till "<" T))
(inc 'Cnt) ) )
(out NIL (prinl (ht:Pack Task) ": " Cnt)) ) ) ) ) )</lang>
Output (05may10):
100 doors: 79 24 game: 21 24 game/Solve: 15 99 Bottles of Beer: 95 A+B: 37 Abstract type: 29 ...
PureBasic
<lang PureBasic>OpenConsole() URLDownloadToFile_( #Null, "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml", "tasks.xml", 0, #Null) ReadFile(0, "tasks.xml")
x1$ = ReadString(0) Repeat x2 = FindString(x1$, "title=", x2 + 1) If x2 title$ = Mid(x1$, x2 + 7, 99) title$ = Left(title$, FindString(title$, ">", 1) - 4) URLDownloadToFile_( #Null, "http://www.rosettacode.org/w/index.php?title=" + title$ + "&action=raw", "task.xml", 0, #Null) ReadFile(1, "task.xml") While Not Eof(1) y1$ = ReadString(1) If FindString(y1$, "=={{header|", 1) j + 1 EndIf Wend PrintN( title$ +": " + Str(j) + " examples") k + j j = 0 CloseFile(1) EndIf Until x2 = 0 PrintN("Total: " + Str(k) + " examples") Input()</lang>
Python
<lang python>import urllib, xml.dom.minidom
x = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml")
tasks = [] for i in xml.dom.minidom.parseString(x.read()).getElementsByTagName("cm"):
t = i.getAttribute('title').replace(" ", "_") y = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=%s&action=raw" % t.encode('utf-8')) tasks.append( y.read().lower().count("{{header|") ) print t.replace("_", " ") + ": %d examples." % tasks[-1]
print "\nTotal: %d examples." % sum(tasks)</lang>
R
<lang R> library(XML) library(RCurl) doc <- xmlInternalTreeParse("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml") nodes <- getNodeSet(doc,"//cm") titles = as.character( sapply(nodes, xmlGetAttr, "title") ) headers <- list() counts <- list() for (i in 1:length(titles)){ headersi <- getURL( paste("http://rosettacode.org/mw/index.php?title=", gsub(" ", "_", titles[i]), "&action=raw", sep="") ) countsi <- strsplit(headersi,split=" ")1 countsi <- grep("\\{\\{header", countsi) cat(titles[i], ":", length(countsi), "examples\n") } cat("Total: ", length(unlist(counts)), "examples\n") </lang>
Ruby
First, a RosettaCode module, saved as rosettacode.rb: <lang ruby>require 'open-uri' require 'rexml/document'
module RosettaCode
URL_ROOT = "http://rosettacode.org/mw"
def self.get_url(page, query) url = "#{URL_ROOT}/%s?%s" % [ URI.escape(page), URI.escape(query.map {|k,v| "%s=%s" % [k,v]}.join("&")) ] url.gsub!(/\+/, '%2B') p url if $DEBUG url end
def self.get_api_url(query) get_url "api.php", query end
def self.category_members(category) query = { "action" => "query", "list" => "categorymembers", "cmtitle" => "Category:#{category}", "format" => "xml", "cmlimit" => 500, } while true url = get_api_url query doc = REXML::Document.new open(url)
REXML::XPath.each(doc, "//cm") do |task| yield task.attribute("title").value end
continue = REXML::XPath.first(doc, "//query-continue") break if continue.nil? cm = REXML::XPath.first(continue, "categorymembers") query["cmcontinue"] = cm.attribute("cmcontinue").value end end
end</lang>
Then, we implement the task with: <lang ruby>require 'rosettacode'
total_examples = 0
RosettaCode.category_members("Programming_Tasks") do |task|
url = RosettaCode.get_url("index.php", {"action" => "raw", "title" => task}) examples = open(url).read.scan("=={{header").length puts "#{task}: #{examples}" total_examples += examples
end
puts puts "Total: #{total_examples}"</lang>
Scala
This was writen for Scala 2.8, but Scala 2.7 can be used with slight modifications to the IO library.
Different than the example for other languages, it parallelizes the reading and counting, and it also encode the URL, because some URLs are now causing problems. These modifications are minor, though.
It was written in script style.
<lang scala>import java.net.{URL, URLEncoder} import scala.io.Source.fromURL
val allTasksURL = "http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml" val allTasks = xml.parsing.XhtmlParser(fromURL(new URL(allTasksURL)))
val regexExpr = "(?i)==\\{\\{header\\|".r def oneTaskURL(title: String) = "http://www.rosettacode.org/w/index.php?title=%s&action=raw" format URLEncoder.encode(title.replace(' ', '_'), "UTF-8") def count(title: String) = regexExpr findAllIn fromURL(new URL(oneTaskURL(title)))(io.Codec.UTF8).mkString length
val counts = for (task <- allTasks \\ "cm" \\ "@title" map (_.text)) yield scala.actors.Futures.future((task, count(task)))
counts map (_.apply) map Function.tupled("%s: %d examples." format (_, _)) foreach println println("\nTotal: %d examples." format (counts map (_.apply._2) sum)) </lang>
Tcl
<lang tcl>package require Tcl 8.5 package require http package require json
fconfigure stdout -buffering none
proc get_tasks {category} {
set start [clock milliseconds] puts -nonewline "getting $category members..." set base_url http://www.rosettacode.org/w/api.php set query {action query list categorymembers cmtitle Category:%s format json cmlimit 500} set this_query [dict create {*}[split [format $query $category]]] set tasks [list]
while {1} { set url [join [list $base_url [http::formatQuery {*}$this_query]] ?] set response [http::geturl $url] if {[set s [http::status $response]] ne "ok" || [http::ncode $response] != 200} { error "Oops: url=$url\nstatus=$s\nhttp code=[http::code $response]" } set data [json::json2dict [http::data $response]] http::cleanup $response # add tasks to list foreach task [dict get $data query categorymembers] { lappend tasks [dict get [dict create {*}$task] title] } if {[catch {dict get $data query-continue categorymembers cmcontinue} continue_task] != 0} { # no more continuations, we're done break } dict set this_query cmcontinue $continue_task } puts " found [llength $tasks] tasks in [expr {[clock milliseconds] - $start}] milliseconds" return $tasks
}
- This proc can be replaced by a single regexp command:
- set count [regexp -all "***=$needle" $haystack]
- However this proc is more efficient -- we're dealing with plain strings only.
proc count_substrings {needle haystack} {
set count 0 set idx 0 while {[set idx [string first $needle $haystack $idx]] != -1} { incr count incr idx } return $count
}
set total 0 foreach task [get_tasks Programming_Tasks] {
set url [format "http://www.rosettacode.org/w/index.php?title=%s&action=raw" [string map {{ } _} $task]] set response [http::geturl $url] if {[set s [http::status $response]] ne "ok" || [http::ncode $response] != 200} { error "Oops: url=$url\nstatus=$s\nhttp code=[http::code $response]" } set count [count_substrings "\{\{header|" [http::data $response]] puts [format "%3d examples in %s" $count $task] http::cleanup $response incr total $count
}
puts "\nTotal: $total examples"</lang>
TUSCRIPT
<lang tuscript> $$ MODE TUSCRIPT url="http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml" data=REQUEST (url)
BUILD S_TABLE beg=* DATA :title=": BUILD S_TABLE end=* DATA :":
titles=EXTRACT (data,beg|,end,1,0,"~~") titles=SPLIT (titles,":~~:") sz_titles=SIZE (titles)
BUILD R_TABLE header=":==\{\{header|:" all=*
ERROR/STOP CREATE ("tasks",seq-e,-std-)
COMPILE LOOP title=titles ask=* ask =SET_VALUE(ask,"title",title) ask =SET_VALUE(ask,"action","raw") ask =ENCODE (ask,cgi) http ="http://www.rosettacode.org/mw/index.php" url =CONCAT (http,"?",ask) data =REQUEST (url) header =FILTER_INDEX (data,header,-) sz_header=SIZE(header) line =CONCAT (title,"=",sz_header," members") FILE "tasks" = line all =APPEND(all,sz_header) ENDLOOP
ENDCOMPILE all =JOIN(all),sum=SUM(all),time=time() line=CONCAT (time,": ", sz_titles, " Programing Tasks: ", sum, " solutions")
FILE "tasks" = line </lang> Output in file "tasks":
100 doors=104 members 24 game=27 members 24 game/Solve=21 members 99 Bottles of Beer=124 members A+B=76 members Abstract type=35 members Accumulator factory=44 members ... XML/Input=39 members XML/Output=32 members XML/XPath=24 members Xiaolin Wu's line algorithm=0 members Y combinator=33 members Yahoo! search interface=10 members Zig-zag matrix=46 members 2011-01-15 03:41:30: 455 Programing Tasks: 16009 solutions