Advertisement
Alex9090

tema2

Jan 6th, 2020
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.74 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strings"
  6.     "sync"
  7. )
  8. func generareCifru(s string) string {
  9.     s = strings.ToLower(s)
  10.     var temp string
  11.     var rez string
  12.     for i := 0; i < len(s); i++ {
  13.         for i, j := 'a', 'z'; i <= 'z' && j >= 'a'; i, j = i+1, j+1 {
  14.             if( s[i] == i ){
  15.                 temp = j
  16.             }
  17.         }
  18.         if(ch <= 'z' && ch >= 'a'){
  19.             rez = temp
  20.         }
  21.     }
  22.     return rez
  23. }
  24.  
  25. func mapper(in <-chan string, out chan<- map[string]int) {
  26.     count := map[string]int{}
  27.  
  28.     cifrat := "pereche"
  29.     necifrat := "nepereche"
  30.  
  31.     for word := range in {
  32.         if generareCifru(word) == word {
  33.             count[cifrat] = count[cifrat] + 1
  34.         } else {
  35.             count[necifrat] = count[necifrat] + 1
  36.         }
  37.     }
  38.     //}
  39.     out <- count
  40.     close(out)
  41. }
  42.  
  43. func reducer(in <-chan int, out chan<- float32) {
  44.     sum, count := 0, 0
  45.     for n := range in {
  46.         sum += n
  47.         count++
  48.     }
  49.     out <- float32(sum) / float32(count)
  50.     close(out)
  51. }
  52.  
  53. func inputReader(out [3]chan<- string) {
  54.  
  55.     input := [][]string{
  56.         {"a1551a", "parc", "ana", "minim", "1pcl3"},
  57.         {"calabalac", "tivit", "leu", "zece10", "ploaie", "9ana9"},
  58.         {"lalalal", "tema", "papa", "ger"},
  59.     }
  60.  
  61.     for i := range out {
  62.         go func(ch chan<- string, word []string) {
  63.             for _, w := range word {
  64.                 ch <- w
  65.             }
  66.             close(ch)
  67.         }(out[i], input[i])
  68.     }
  69. }
  70.  
  71. func shuffler(in []<-chan map[string]int, out [2]chan<- int) {
  72.     var wg sync.WaitGroup
  73.     wg.Add(len(in))
  74.     for _, ch := range in {
  75.         go func(c <-chan map[string]int) {
  76.             for m := range c {
  77.                 pc, ok := m["palindrom"]
  78.                 if ok {
  79.                     out[0] <- pc
  80.                 }
  81.                 nc, ok := m["nepalindrom"]
  82.                 if ok {
  83.                     out[1] <- nc
  84.                 }
  85.             }
  86.             wg.Done()
  87.         }(ch)
  88.     }
  89.  
  90.     go func() {
  91.         wg.Wait()
  92.         close(out[0])
  93.         close(out[1])
  94.     }()
  95. }
  96.  
  97. func outputWriter(in []<-chan float32) {
  98.     var wg sync.WaitGroup
  99.     wg.Add(len(in))
  100.  
  101.     name := []string{"palindrom", "nepalindrom"}
  102.     for i := 0; i < len(in); i++ {
  103.         go func(n int, c <-chan float32) {
  104.             for avg := range c {
  105.                 fmt.Printf("Average number of %ss per input text: %f\n", name[n], avg)
  106.             }
  107.             wg.Done()
  108.         }(i, in[i])
  109.     }
  110.     wg.Wait()
  111. }
  112.  
  113. func main() {
  114.     size := 10
  115.     text1 := make(chan string, size)
  116.     text2 := make(chan string, size)
  117.     text3 := make(chan string, size)
  118.     map1 := make(chan map[string]int, size)
  119.     map2 := make(chan map[string]int, size)
  120.     map3 := make(chan map[string]int, size)
  121.     reduce1 := make(chan int, size)
  122.     reduce2 := make(chan int, size)
  123.     avg1 := make(chan float32, size)
  124.     avg2 := make(chan float32, size)
  125.  
  126.     go inputReader([3]chan<- string{text1, text2, text3})
  127.     go mapper(text1, map1)
  128.     go mapper(text2, map2)
  129.     go mapper(text3, map3)
  130.     go shuffler([]<-chan map[string]int{map1, map2, map3}, [2]chan<- int{reduce1, reduce2})
  131.     go reducer(reduce1, avg1)
  132.     go reducer(reduce2, avg2)
  133.  
  134.     outputWriter([]<-chan float32{avg1, avg2})
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement