Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
73 changes: 73 additions & 0 deletions Day6/conversation.go
Original file line number Diff line number Diff line change
@@ -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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whenever you get time. try to use select for reading from channel

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure.

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()
}
}
47 changes: 47 additions & 0 deletions Day6/reserse_string.go
Original file line number Diff line number Diff line change
@@ -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)
}