Rosetta Code/Count examples: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: {{task|Rosetta Code related}}Find the total number of programming examples for each task and the total for all tasks. Essentially, count the number of occu...)
 
No edit summary
Line 2: Line 2:


Essentially, count the number of occurrences of <nowiki>"=={{header|"</nowiki> on each task page.
Essentially, count the number of occurrences of <nowiki>"=={{header|"</nowiki> on each task page.

Output:

<lang>
100_doors: 20 examples.
99_Bottles_of_Beer: 29 examples.
Abstract_type: 10 examples.
...
Total: X examples.
</lang>

=={{header|Python}}==

<lang python>import urllib
import xml.dom.minidom
import re

tasks = {}

x = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml")
x = xml.dom.minidom.parseString(x.read()).getElementsByTagName("cm")
x = [i.getAttribute('title').replace(" ","_") for i in x]

for i in x:
y = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=%s&action=raw" % i).read()
y = y.lower()
tasks[i] = y.count("{{header|")
print i + ": " + str(tasks[i]) + " examples."

print "Total: " + str(sum(tasks.values())) + " examples."</lang>

Revision as of 19:12, 9 February 2009

Task
Rosetta Code/Count examples
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>

Python

<lang python>import urllib import xml.dom.minidom import re

tasks = {}

x = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=xml") x = xml.dom.minidom.parseString(x.read()).getElementsByTagName("cm") x = [i.getAttribute('title').replace(" ","_") for i in x]

for i in x:

   y = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=%s&action=raw" % i).read()
   y = y.lower()    
   tasks[i] = y.count("{{header|")
   print i + ": " + str(tasks[i]) + " examples."

print "Total: " + str(sum(tasks.values())) + " examples."</lang>