Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Amb/Go/amb-1.go
Normal file
49
Task/Amb/Go/amb-1.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func ambStrings(ss []string) chan []string {
|
||||
c := make(chan []string)
|
||||
go func() {
|
||||
for _, s := range ss {
|
||||
c <- []string{s}
|
||||
}
|
||||
close(c)
|
||||
}()
|
||||
return c
|
||||
}
|
||||
|
||||
func ambChain(ss []string, cIn chan []string) chan []string {
|
||||
cOut := make(chan []string)
|
||||
go func() {
|
||||
var w sync.WaitGroup
|
||||
for chain := range cIn {
|
||||
w.Add(1)
|
||||
go func(chain []string) {
|
||||
for s1 := range ambStrings(ss) {
|
||||
if s1[0][len(s1[0])-1] == chain[0][0] {
|
||||
cOut <- append(s1, chain...)
|
||||
}
|
||||
}
|
||||
w.Done()
|
||||
}(chain)
|
||||
}
|
||||
w.Wait()
|
||||
close(cOut)
|
||||
}()
|
||||
return cOut
|
||||
}
|
||||
|
||||
func main() {
|
||||
s1 := []string{"the", "that", "a"}
|
||||
s2 := []string{"frog", "elephant", "thing"}
|
||||
s3 := []string{"walked", "treaded", "grows"}
|
||||
s4 := []string{"slowly", "quickly"}
|
||||
c := ambChain(s1, ambChain(s2, ambChain(s3, ambStrings(s4))))
|
||||
for s := range c {
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
39
Task/Amb/Go/amb-2.go
Normal file
39
Task/Amb/Go/amb-2.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
import "fmt"
|
||||
|
||||
func amb(wordsets [][]string, res []string) bool {
|
||||
if len(wordsets) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
var s string
|
||||
|
||||
l := len(res)
|
||||
if l > 0 { s = res[l - 1] }
|
||||
|
||||
res = res[0:len(res) + 1]
|
||||
|
||||
for _, res[l] = range(wordsets[0]) {
|
||||
if l > 0 && s[len(s) - 1] != res[l][0] { continue }
|
||||
|
||||
if amb(wordsets[1:len(wordsets)], res) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
wordset := [][]string { { "the", "that", "a" },
|
||||
{ "frog", "elephant", "thing" },
|
||||
{ "walked", "treaded", "grows" },
|
||||
{ "slowly", "quickly" } }
|
||||
res := make([]string, len(wordset))
|
||||
|
||||
if amb(wordset, res[0:0]) {
|
||||
fmt.Println(res)
|
||||
} else {
|
||||
fmt.Println("No amb found")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue