Rosetta Code/Tasks without examples: Difference between revisions

Added Go
(Added Perl example)
(Added Go)
Line 2:
 
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".
 
=={{header|Go}}==
<lang go>package main
 
import (
"fmt"
"html"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
)
 
func main() {
ex := `<li><a href="/wiki/(.*?)"`
re := regexp.MustCompile(ex)
page := "http://rosettacode.org/wiki/Category:Programming_Tasks"
resp, _ := http.Get(page)
body, _ := ioutil.ReadAll(resp.Body)
matches := re.FindAllStringSubmatch(string(body), -1)
resp.Body.Close()
tasks := make([]string, len(matches))
for i, match := range matches {
tasks[i] = match[1]
}
const base = "http://rosettacode.org/wiki/"
const limit = 3 // number of tasks to print out
ex = `(?s)using any language you may know.</div>(.*?)<div id="toc"`
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>
 
{{out}}
Text rather than HTML:
<pre>
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
</pre>
 
=={{header|Perl}}==
9,485

edits