Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
57
Task/Mad-Libs/Go/mad-libs.go
Normal file
57
Task/Mad-Libs/Go/mad-libs.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pat := regexp.MustCompile("<.+?>")
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Println("usage: madlib <story template file>")
|
||||
return
|
||||
}
|
||||
b, err := ioutil.ReadFile(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tmpl := string(b)
|
||||
s := []string{} // patterns in order of appearance
|
||||
m := map[string]string{} // mapping from patterns to replacements
|
||||
for _, p := range pat.FindAllString(tmpl, -1) {
|
||||
if _, ok := m[p]; !ok {
|
||||
m[p] = ""
|
||||
s = append(s, p)
|
||||
}
|
||||
}
|
||||
fmt.Println("Enter replacements:")
|
||||
br := bufio.NewReader(os.Stdin)
|
||||
for _, p := range s {
|
||||
for {
|
||||
fmt.Printf("%s: ", p[1:len(p)-1])
|
||||
r, isPre, err := br.ReadLine()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if isPre {
|
||||
log.Fatal("you're not playing right. :P")
|
||||
}
|
||||
s := strings.TrimSpace(string(r))
|
||||
if s == "" {
|
||||
fmt.Println(" hmm?")
|
||||
continue
|
||||
}
|
||||
m[p] = s
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println("\nYour story:\n")
|
||||
fmt.Println(pat.ReplaceAllStringFunc(tmpl, func(p string) string {
|
||||
return m[p]
|
||||
}))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue