How to resolve the algorithm Rosetta Code/Count examples step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Rosetta Code/Count examples step by step in the Scala programming language
Table of Contents
Problem Statement
Essentially, count the number of occurrences of ==header on each task page. Output: For a full output, updated periodically, see Rosetta Code/Count examples/Full list. You'll need to use the Media Wiki API, which you can find out about locally, here, or in Media Wiki's API documentation at, API:Query
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Rosetta Code/Count examples step by step in the Scala programming language
Source code in the scala programming language
import scala.language.postfixOps
object TaskCount extends App {
import java.net.{ URL, URLEncoder }
import scala.io.Source.fromURL
System.setProperty("http.agent", "*")
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) = {
println(s"Check $title")
"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))
}
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Ring programming language
You may also check:How to resolve the algorithm HTTP step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the zkl programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the Chipmunk Basic programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the TorqueScript programming language