Rosetta Code/Rank languages by popularity: Difference between revisions

From Rosetta Code
Content added Content deleted
(Zen of Python. Readabilty.)
No edit summary
Line 21: Line 21:


=={{header|Perl}}==
=={{header|Perl}}==
Sorting only programming languages.
This program checks the name of each category against [[:Category:Programming Languages|this page]] so that only actual languages appear in the ranking.


<lang perl>use LWP::Simple 'get';
<lang perl>use LWP::Simple 'get';
Line 44: Line 44:
=={{header|Python}}==
=={{header|Python}}==


Sorting only programming languages.
Works with: Python 2.6


<lang python>import urllib, re
<lang python>import urllib
import re
key1 = lambda x: int(x[1])


get1 = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json").read()
def key1(x):
get2 = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500").read()
return int(x.split()[0])


langs = re.findall("\"title\":\"Category:(.+?)\"",get1)
a = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500")
qtdmbr = re.findall("title=\"Category:(.+?)\">.+?</a> \((\d+) members\)",get2)


result = [(x,y) for x,y in qtdmbr if x in langs]
entries = []


for n, i in enumerate(sorted(result,key=key1,reverse=True)):
for line in a:
print "%s. %s - %s" % (n+1, i[1], i[0])</lang>
match = re.search('>([^<>]*)</a> \((\d+) members?\)', line)
if match:
entries.append(match.group(2) + ' - ' + match.group(1))

a.close()

for c, line in enumerate(sorted(entries, key=key1, reverse=True), start=1):
print "%3d. %s" % (c, line)</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==

Revision as of 18:00, 21 February 2009

Task
Rosetta Code/Rank languages by popularity
You are encouraged to solve this task according to the task description, using any language you may know.

Sort most popular programming languages based in number of members in Rosetta Code categories (from http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500)

Output:

1. 246 - Programming_Tasks 
2. 203 - Python 
3. 201 - Ada 
4. 188 - OCaml 
5. 171 - Perl 
6. 169 - Java 
7. 168 - Haskell 
8. 157 - C 
9. 141 - Forth 
10. 140 - Ruby 
...

Filtering wrong results is optional. You can check against http://www.rosettacode.org/wiki/Special:MostLinkedCategories

Perl

Sorting only programming languages.

<lang perl>use LWP::Simple 'get';

my $langs_url = 'http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json'; my $cats_url = 'http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500';

my %langs = map {/Category:(.+)/, 1}

   get($langs_url) =~ /"title":"(.+?)"}/g;

get($cats_url) =~ m{

  • (.+?)

}s;

my @pairs =

   sort {$b->[1] <=> $a->[1]}
   grep {$langs{ $_->[0] }}
   map {[ m{>(\S.*?)</a> \((\d+) member} ]}

split '

  • ', $1; for (my $n = 1 ; @pairs ; ++$n) {my ($lang, $tasks) = @{shift @pairs}; printf "%3d. %3d - %s\n", $n, $tasks, $lang;}</lang>

    Python

    Sorting only programming languages.

    <lang python>import urllib import re key1 = lambda x: int(x[1])

    get1 = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json").read() get2 = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500").read()

    langs = re.findall("\"title\":\"Category:(.+?)\"",get1) qtdmbr = re.findall("title=\"Category:(.+?)\">.+?</a> \((\d+) members\)",get2)

    result = [(x,y) for x,y in qtdmbr if x in langs]

    for n, i in enumerate(sorted(result,key=key1,reverse=True)):

       print "%s. %s - %s" % (n+1, i[1], i[0])</lang>
    

    Ruby

    <lang ruby>require 'open-uri'

    entries = []

    open("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500") do |f|

     for line in f
       match = line.match(%r{>([^<>]*)</a> \((\d+) members?\)})
       entries << match[2] + ' - ' + match[1] if match
     end
    

    end

    entries.sort_by {|x| -x.to_i}.each_with_index do |line, c|

     puts "%3d. %s" % [c+1, line]
    

    end</lang>

    UnixPipes

    echo "GET http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500 HTTP/1.0\n\n" 
     | nc www.rosettacode.org 80 
     | sed -n -e 's,<[^>]*>,,g' -e's,^\([^(]*\)(\([^)]*\) members*) *,\2 - \1,g' -e'/^[0-9]\+./p'
     | sort -rn