Rosetta Code/Rank languages by popularity: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Put in RC cat)
No edit summary
Line 24: Line 24:
Works with: Python 2.6
Works with: Python 2.6


<python>import urllib
<lang python>import urllib
import re
import re


Line 41: Line 41:


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


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


<ruby>require 'open-uri'
<lang ruby>require 'open-uri'


entries = []
entries = []
Line 58: Line 58:
entries.sort_by {|x| -x.to_i}.each_with_index do |line, c|
entries.sort_by {|x| -x.to_i}.each_with_index do |line, c|
puts "%3d. %s" % [c+1, line]
puts "%3d. %s" % [c+1, line]
end</ruby>
end</lang>

Revision as of 15:52, 3 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

Python

Works with: Python 2.6

<lang python>import urllib import re

def key1(x):

   return int(x.split()[0])

a = urllib.urlopen("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=500")

entries = []

for line in a:

   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>

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>