tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
58
Task/Semordnilap/Go/semordnilap.go
Normal file
58
Task/Semordnilap/Go/semordnilap.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// read file into memory as one big block
|
||||
data, err := ioutil.ReadFile("unixdict.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// copy the block, split it up into words
|
||||
words := strings.Split(string(data), "\n")
|
||||
// optional, free the first block for garbage collection
|
||||
data = nil
|
||||
// put words in a map, also determine length of longest word
|
||||
m := make(map[string]bool)
|
||||
longest := 0
|
||||
for _, w := range words {
|
||||
m[string(w)] = true
|
||||
if len(w) > longest {
|
||||
longest = len(w)
|
||||
}
|
||||
}
|
||||
// allocate a buffer for reversing words
|
||||
r := make([]byte, longest)
|
||||
// iterate over word list
|
||||
sem := 0
|
||||
var five []string
|
||||
for _, w := range words {
|
||||
// first, delete from map. this prevents a palindrome from matching
|
||||
// itself, and also prevents it's reversal from matching later.
|
||||
delete(m, w)
|
||||
// use buffer to reverse word
|
||||
last := len(w) - 1
|
||||
for i := 0; i < len(w); i++ {
|
||||
r[i] = w[last-i]
|
||||
}
|
||||
rs := string(r[:len(w)])
|
||||
// see if reversed word is in map, accumulate results
|
||||
if m[rs] {
|
||||
sem++
|
||||
if len(five) < 5 {
|
||||
five = append(five, w+"/"+rs)
|
||||
}
|
||||
}
|
||||
}
|
||||
// print results
|
||||
fmt.Println(sem, "pairs")
|
||||
fmt.Println("examples:")
|
||||
for _, e := range five {
|
||||
fmt.Println(" ", e)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue