Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "strings"
- "sync"
- )
- func generareCifru(s string) string {
- s = strings.ToLower(s)
- var temp string
- var rez string
- for i := 0; i < len(s); i++ {
- for i, j := 'a', 'z'; i <= 'z' && j >= 'a'; i, j = i+1, j+1 {
- if( s[i] == i ){
- temp = j
- }
- }
- if(ch <= 'z' && ch >= 'a'){
- rez = temp
- }
- }
- return rez
- }
- func mapper(in <-chan string, out chan<- map[string]int) {
- count := map[string]int{}
- cifrat := "pereche"
- necifrat := "nepereche"
- for word := range in {
- if generareCifru(word) == word {
- count[cifrat] = count[cifrat] + 1
- } else {
- count[necifrat] = count[necifrat] + 1
- }
- }
- //}
- out <- count
- close(out)
- }
- func reducer(in <-chan int, out chan<- float32) {
- sum, count := 0, 0
- for n := range in {
- sum += n
- count++
- }
- out <- float32(sum) / float32(count)
- close(out)
- }
- func inputReader(out [3]chan<- string) {
- input := [][]string{
- {"a1551a", "parc", "ana", "minim", "1pcl3"},
- {"calabalac", "tivit", "leu", "zece10", "ploaie", "9ana9"},
- {"lalalal", "tema", "papa", "ger"},
- }
- for i := range out {
- go func(ch chan<- string, word []string) {
- for _, w := range word {
- ch <- w
- }
- close(ch)
- }(out[i], input[i])
- }
- }
- func shuffler(in []<-chan map[string]int, out [2]chan<- int) {
- var wg sync.WaitGroup
- wg.Add(len(in))
- for _, ch := range in {
- go func(c <-chan map[string]int) {
- for m := range c {
- pc, ok := m["palindrom"]
- if ok {
- out[0] <- pc
- }
- nc, ok := m["nepalindrom"]
- if ok {
- out[1] <- nc
- }
- }
- wg.Done()
- }(ch)
- }
- go func() {
- wg.Wait()
- close(out[0])
- close(out[1])
- }()
- }
- func outputWriter(in []<-chan float32) {
- var wg sync.WaitGroup
- wg.Add(len(in))
- name := []string{"palindrom", "nepalindrom"}
- for i := 0; i < len(in); i++ {
- go func(n int, c <-chan float32) {
- for avg := range c {
- fmt.Printf("Average number of %ss per input text: %f\n", name[n], avg)
- }
- wg.Done()
- }(i, in[i])
- }
- wg.Wait()
- }
- func main() {
- size := 10
- text1 := make(chan string, size)
- text2 := make(chan string, size)
- text3 := make(chan string, size)
- map1 := make(chan map[string]int, size)
- map2 := make(chan map[string]int, size)
- map3 := make(chan map[string]int, size)
- reduce1 := make(chan int, size)
- reduce2 := make(chan int, size)
- avg1 := make(chan float32, size)
- avg2 := make(chan float32, size)
- go inputReader([3]chan<- string{text1, text2, text3})
- go mapper(text1, map1)
- go mapper(text2, map2)
- go mapper(text3, map3)
- go shuffler([]<-chan map[string]int{map1, map2, map3}, [2]chan<- int{reduce1, reduce2})
- go reducer(reduce1, avg1)
- go reducer(reduce2, avg2)
- outputWriter([]<-chan float32{avg1, avg2})
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement