Rosetta Code/Tasks without examples: Difference between revisions
Line 137: | Line 137: | ||
A* search algorithm |
A* search algorithm |
||
</pre> |
</pre> |
||
=={{header|Java}}== |
|||
{{trans|Kotlin}} |
|||
<lang Java>import java.io.IOException; |
|||
import java.net.URI; |
|||
import java.net.URISyntaxException; |
|||
import java.net.http.HttpClient; |
|||
import java.net.http.HttpRequest; |
|||
import java.net.http.HttpResponse; |
|||
import java.time.Duration; |
|||
import java.util.ArrayList; |
|||
import java.util.regex.Pattern; |
|||
public class TasksWithoutExamples { |
|||
private static String readPage(HttpClient client, URI uri) throws IOException, InterruptedException { |
|||
var request = HttpRequest.newBuilder() |
|||
.GET() |
|||
.uri(uri) |
|||
.timeout(Duration.ofSeconds(5)) |
|||
.setHeader("accept", "text/html") |
|||
.build(); |
|||
var response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
|||
return response.body(); |
|||
} |
|||
private static void process(HttpClient client, String base, String task) { |
|||
try { |
|||
var re = Pattern.compile(".*using any language you may know.</div>(.*?)<div id=\"toc\".*", Pattern.DOTALL + Pattern.MULTILINE); |
|||
var re2 = Pattern.compile("</?[^>]*>"); |
|||
var page = base + task; |
|||
String body = readPage(client, new URI(page)); |
|||
var matcher = re.matcher(body); |
|||
if (matcher.matches()) { |
|||
var group = matcher.group(1); |
|||
var m2 = re2.matcher(group); |
|||
var text = m2.replaceAll(""); |
|||
System.out.println(text); |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException { |
|||
var re = Pattern.compile("<li><a href=\"/wiki/(.*?)\"", Pattern.DOTALL + Pattern.MULTILINE); |
|||
var client = HttpClient.newBuilder() |
|||
.version(HttpClient.Version.HTTP_1_1) |
|||
.followRedirects(HttpClient.Redirect.NORMAL) |
|||
.connectTimeout(Duration.ofSeconds(5)) |
|||
.build(); |
|||
var uri = new URI("http", "rosettacode.org", "/wiki/Category:Programming_Tasks", ""); |
|||
var body = readPage(client, uri); |
|||
var matcher = re.matcher(body); |
|||
var tasks = new ArrayList<String>(); |
|||
while (matcher.find()) { |
|||
tasks.add(matcher.group(1)); |
|||
} |
|||
var base = "http://rosettacode.org/wiki/"; |
|||
var limit = 3L; |
|||
tasks.stream().limit(limit).forEach(task -> process(client, base, task)); |
|||
} |
|||
}</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
Revision as of 23:18, 29 October 2020
A RosettaCode contributor is going on a long journey. To ease the boredom of a long flight he takes his favourite programming language manual and a list of RosettaCode tasks, but there's a catch: the tasks have to be description only and not include all the solutions already supplied by other programmers.
Task: Scrape http://rosettacode.org/wiki/Category:Programming_Tasks for all the tasks. Traverse the links. Extract the text or html between a tag with a class of "infobox" and the beginning of the table with the id of "toc".
Go
<lang go>package main
import (
"fmt" "html" "io/ioutil" "net/http" "regexp" "strings" "time"
)
func main() {
ex := `
ex2 := `</?[^>]*>` // to remove all tags including links re = regexp.MustCompile(ex) re2 := regexp.MustCompile(ex2) for i, task := range tasks { page = base + task resp, _ = http.Get(page) body, _ = ioutil.ReadAll(resp.Body) match := re.FindStringSubmatch(string(body)) resp.Body.Close() text := html.UnescapeString(re2.ReplaceAllLiteralString(match[1], "")) fmt.Println(strings.Replace(task, "_", " ", -1), "\n", text) if i == limit-1 { break } time.Sleep(5 * time.Second) // wait 5 seconds before processing next task }
}</lang>
- Output:
Text rather than HTML:
100 doors There are 100 doors in a row that are all initially closed. You make 100 passes by the doors. The first time through, visit every door and toggle the door (if the door is closed, open it; if it is open, close it). The second time, only visit every 2nd door (door #2, #4, #6, ...), and toggle it. The third time, visit every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door. Task Answer the question: what state are the doors in after the last pass? Which are open, which are closed? Alternate: As noted in this page's discussion page, the only doors that remain open are those whose numbers are perfect squares. Opening only those doors is an optimization that may also be expressed; however, as should be obvious, this defeats the intent of comparing implementations across programming languages. 15 Puzzle Game Task Implement the Fifteen Puzzle Game. The 15-puzzle is also known as: Fifteen Puzzle Gem Puzzle Boss Puzzle Game of Fifteen Mystic Square 14-15 Puzzle and many others. Related Tasks 15 Puzzle Solver 16 Puzzle Game 15 puzzle solver Your task is to write a program that finds a solution in the fewest moves possible single moves to a random Fifteen Puzzle Game. For this task you will be using the following puzzle: 15 14 1 6 9 11 4 12 0 10 7 3 13 8 5 2 Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 The output must show the moves' directions, like so: left, left, left, down, right... and so on. There are two solutions, of fifty-two moves: rrrulddluuuldrurdddrullulurrrddldluurddlulurruldrdrd rrruldluuldrurdddluulurrrdlddruldluurddlulurruldrrdd see: Pretty Print of Optimal Solution Finding either one, or both is an acceptable result. Extra credit. Solve the following problem: 0 12 9 13 15 11 10 14 3 7 2 5 4 8 6 1 Related Task 15 puzzle game A* search algorithm
Java
<lang Java>import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.util.ArrayList; import java.util.regex.Pattern;
public class TasksWithoutExamples {
private static String readPage(HttpClient client, URI uri) throws IOException, InterruptedException { var request = HttpRequest.newBuilder() .GET() .uri(uri) .timeout(Duration.ofSeconds(5)) .setHeader("accept", "text/html") .build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString()); return response.body(); }
private static void process(HttpClient client, String base, String task) { try {var re = Pattern.compile(".*using any language you may know.(.*?)<div id=\"toc\".*", Pattern.DOTALL + Pattern.MULTILINE);
var re2 = Pattern.compile("</?[^>]*>");
var page = base + task; String body = readPage(client, new URI(page));
var matcher = re.matcher(body); if (matcher.matches()) { var group = matcher.group(1); var m2 = re2.matcher(group); var text = m2.replaceAll(""); System.out.println(text); } } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {var re = Pattern.compile("
Kotlin
<lang scala>import java.net.URI import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse import java.time.Duration import java.util.regex.Pattern
fun readPage(client: HttpClient, uri: URI): String {
val request = HttpRequest.newBuilder() .GET() .uri(uri) .timeout(Duration.ofSeconds(5)) .setHeader("accept", "text/html") .build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString()) return response.body()
}
fun main() {
var re = Pattern.compile("val re2 = Pattern.compile("</?[^>]*>") for (task in tasks.stream().limit(limit)) { val page = base + task body = readPage(client, URI(page))
matcher = re.matcher(body) if (matcher.matches()) { val group = matcher.group(1) val m2 = re2.matcher(group) val text = m2.replaceAll("") println(text) } }
}</lang>
Perl
Slice and dice the HTML. Output is as for the other examples. <lang perl>use strict; use warnings;
use LWP::UserAgent; my $ua = LWP::UserAgent->new;
- get list of task titles
my $html = $ua->request( HTTP::Request->new( GET => 'http://rosettacode.org/wiki/Category:Programming_Tasks'))->content;
my @tasks = $html =~ m#- download tasks, and extract task descriptions
print "$title\n$task_at_hand\n\n"; sleep 10; # so you have time to read each task...
}</lang>
Phix
Since downloading all the pages can be very slow, this uses a cache. Limiting by "Phix" fairly obviously speeds it up tenfold :-)
Output similar to zkl, I assume the first four constants are self-explanatory.
<lang Phix>-- demo\rosetta\Tasks_without_examples.exw
constant output_html = true,
include_drafts = true, summary = false, notlang = "Phix" -- "" for all
include builtins\timedate.e integer refresh_cache = timedelta(days:=30) -- 0 for always
include builtins\libcurl.e atom curl = NULL atom pErrorBuffer
function write_callback(atom pData, integer size, integer nmemb, integer fn)
integer bytes_written = size * nmemb puts(fn,peek({pData,bytes_written})) return bytes_written
end function constant write_cb = call_back({'+', routine_id("write_callback")})
function open_download(string filename, url)
bool refetch = true if get_file_type("rc_cache")!=FILETYPE_DIRECTORY then if not create_directory("rc_cache") then crash("cannot create rc_cache directory") end if end if filename = join_path({"rc_cache",filename}) if file_exists(filename) then -- use existing file if <= refresh_cache (30+ days) old sequence last_mod = get_file_date(filename) -- (0.8.1+) atom delta = timedate_diff(last_mod,date()) refetch = (delta>refresh_cache) else string directory = get_file_path(filename) if get_file_type(directory)!=FILETYPE_DIRECTORY then if not create_directory(directory,make_parent:=true) then crash("cannot create %s directory",{directory}) end if end if end if if refetch then printf(1,"Downloading %s...\n",{filename}) if curl=NULL then curl_global_init() curl = curl_easy_init() pErrorBuffer = allocate(CURL_ERROR_SIZE) curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, pErrorBuffer) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb) end if url = substitute(url,"%3A",":") url = substitute(url,"%2A","*") curl_easy_setopt(curl, CURLOPT_URL, url) integer fn = open(filename,"wb") if fn=-1 then ?9/0 end if curl_easy_setopt(curl, CURLOPT_WRITEDATA, fn) CURLcode res = curl_easy_perform(curl) if res!=CURLE_OK then string error = sprintf("%d",res) if res=CURLE_COULDNT_RESOLVE_HOST then error &= " [CURLE_COULDNT_RESOLVE_HOST]" end if printf(1, "Error %s downloading file\n", error) {} = wait_key() abort(0) end if close(fn) refresh_cache += timedelta(days:=1) -- did I mention it is slow? end if return get_text(filename)
end function
function open_category(string filename)
return open_download(filename&".htm","http://rosettacode.org/wiki/Category:"&filename)
end function
function dewiki(string s)
sequence tasks = {}integer start = 1, finish = match(`
integer finish = match(`<div id="toc"`,contents,start)-1 -- ... but draft tasks with too few languages have no toc:if finish=-1 then finish = match(`