diff --git a/Day6/Day 6 _ Routines & Channels _ Go Training Session Notes.docx b/Day6/Day 6 _ Routines & Channels _ Go Training Session Notes.docx new file mode 100644 index 0000000..19ae289 Binary files /dev/null and b/Day6/Day 6 _ Routines & Channels _ Go Training Session Notes.docx differ diff --git a/Day6/conversation.go b/Day6/conversation.go new file mode 100644 index 0000000..0f7ae1b --- /dev/null +++ b/Day6/conversation.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "sync" +) + +/* +1. Given a string containing conversation between alice and bob. In the string, if it reaches $, it means end of alice message and if it reaches #, it means end of bob's message. and if it reaches ^, +it means end of conversation ignore string after that. + +Note: given string doesn't contain any spaces. If message contains two continuous conversations from one person it should be printed one after another as given in the example. + +write a program to separate out messages from alice and bob. Write messages from alice and bob on seperate channels, whenever a message from alice/bob, print it in front of their name as shown in the example below: + +Note: there is a single space before and after colon(:) and no space before and after comma. + +e.g: "helloBob$helloalice#howareyou?#Iamgood.howareyou?$^" +output: alice : helloBob,bob : helloalice,bob : howareyou?,alice : Iamgood.howareyou? +*/ + +func main() { + var message string = "helloBob$helloalice#howareyou?#Iamgood.howareyou?$^" + aliceChannel := make(chan string) + bobChannel := make(chan string) + var wg sync.WaitGroup + + // Index for start of new message. + var prevIndex int = 0 + + for i := 0; i < len(message)-1; i++ { + + if message[i] == '$' { + + // If alice talks + messageSlice := message[prevIndex:i] + wg.Add(1) + + go func(i int) { + if i == len(message)-2 { + // End of conversation + fmt.Print("alice : ", <-aliceChannel) + } else { + fmt.Print("alice : ", <-aliceChannel, ",") + } + wg.Done() + }(i) + + aliceChannel <- messageSlice + prevIndex = i + 1 + + } else if message[i] == '#' { + + // If bob talks + messageSlice := message[prevIndex:i] + wg.Add(1) + + go func(i int) { + if i == len(message)-2 { + // End of conversation + fmt.Print("bob : ", <-bobChannel) + } else { + fmt.Print("bob : ", <-bobChannel, ",") + } + wg.Done() + }(i) + + bobChannel <- messageSlice + prevIndex = i + 1 + } + wg.Wait() + } +} diff --git a/Day6/reserse_string.go b/Day6/reserse_string.go new file mode 100644 index 0000000..9d4b7c7 --- /dev/null +++ b/Day6/reserse_string.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + "runtime" + "sync" +) + +/* +2. Given a string, reverse it using one go routine.And inside go routine print reversed string and number of goroutines launched + +e.g: Input: test123 output: 321tset 2 +*/ + +func reverseString(message string, wg *sync.WaitGroup) { + + rns := []rune(message) + + for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 { + + rns[i], rns[j] = rns[j], rns[i] + + } + + fmt.Print(string(rns), " ") + wg.Done() +} + +func main() { + + var message string + fmt.Println("Enter string to reverse") + _, err := fmt.Scanf("%v", &message) + if err != nil { + fmt.Println("Invalid input string") + return + } + + var wg sync.WaitGroup + + wg.Add(1) + go reverseString(message, &wg) + numGoroutines := runtime.NumGoroutine() + wg.Wait() + + fmt.Println(numGoroutines) +}