How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Ruby programming language

Table of Contents

Problem Statement

Find all     tags without a language specified in the text of a page.
Display counts by language section: should display something like

Allow multiple files to be read.   Summarize all results by language:

Use the   Media Wiki API   to test actual RC tasks.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Ruby programming language

The code snippet is a Ruby program that processes a list of tasks (named tasks) and counts the number of occurrences of a certain pattern (bare language tags, like <lang>) in a website's HTML source code. It first defines a Report struct, which is a convenient way to store data in Ruby. The Report struct has two attributes: count (to store the count of occurrences) and tasks (to store a list of tasks associated with each occurrence). Then, it creates a hash called result, which is initialized with a default value of Report.new(0, []). This means that if a key doesn't exist in the hash, it will be created with the default value. The program iterates over the list of tasks, prints the current task being processed, and opens the HTML source code of the corresponding Rosetta Code wiki page. It then iterates over each line of the HTML source code, and if it finds a line that matches the pattern for a bare language tag (<lang>), it updates the current_lang variable to the name of the language. It also counts the number of occurrences of the bare language tag pattern in the line and adds it to the count attribute of the corresponding Report object in the result hash. It also appends the task name to the tasks attribute of the Report object. After processing all the tasks, the program prints a summary of the results. It prints the total number of bare language tags found, and then lists the number of occurrences of the pattern for each language, along with the tasks associated with each occurrence.

Source code in the ruby programming language

require "open-uri"
require "cgi" 

tasks  = ["Greatest_common_divisor", "Greatest_element_of_a_list", "Greatest_subsequential_sum"]
part_uri  = "http://rosettacode.org/wiki?action=raw&title="
Report = Struct.new(:count, :tasks)
result = Hash.new{|h,k| h[k] = Report.new(0, [])}

tasks.each do |task|
  puts "processing #{task}"
  current_lang = "no language"
  open(part_uri + CGI.escape(task)).each_line do |line|
    current_lang = Regexp.last_match["lang"] if /==\{\{header\|(?<lang>.+)\}\}==/ =~ line 
    num_no_langs = line.scan(/<lang\s*>/).size
    if num_no_langs > 0 then
      result[current_lang].count += num_no_langs
      result[current_lang].tasks << task
    end
  end
end

puts "\n#{result.values.map(&:count).inject(&:+)} bare language tags.\n\n"
result.each{|k,v| puts "#{v.count} in #{k} (#{v.tasks})"}


  

You may also check:How to resolve the algorithm Convex hull step by step in the Racket programming language
You may also check:How to resolve the algorithm System time step by step in the BASIC programming language
You may also check:How to resolve the algorithm Gamma function step by step in the Factor programming language
You may also check:How to resolve the algorithm O'Halloran numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Befunge programming language