Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Secret Student Data ==================== type Student struct { Name string RollNo int } type SecretStudent struct { SecretCode string ChunkSize int Length int StudentDetail Student

Secret Student Data ====================

type Student struct { Name string RollNo int }

type SecretStudent struct { SecretCode string ChunkSize int Length int StudentDetail Student }

A teacher want to share details of students to school's management. All the data can be accessed using roll number, name and secret code. The teacher shares this information using go channels. But before writing to channel the teacher modifies secret code according to following rules- 1- You divide the SecretCode in equal size chunks 2- Re-arrange the chunks in reverse order 3- If length is such that it cannot be divided in equal size chunks then you pad the secret code using '0'. Eg: secret code length is 8 and chunk size is 5 so you add "00" in secret code which makes the length 10 and get each chunk of size 5

Write a program in which a go routine 'teacher' takes three SecretStudent objects. Modifies the secret code according to the rules. Convert the object to byte array(using json encoding) and write the byte array to A channel

Another go routine 'management' reads from the channel. Decode the byte array and display the recieved SecretStudent. After that fix the SecretCode and display it.

Here are 2 examples for secretCode modification

Examples1 =========== Input: SecretCode: abcdefghijkl ChunkSize: 4 Length: 12

Output: ijklefghabcd

Examples2 =========== Input: SecretCode: abcdefghijkl ChunkSize: 5 Length: 12

Output: kl000fghijabcde NOTE: - This is the question. =============================== package main

import ( "encoding/json" "fmt" "strings" "os" )

type Student struct { Name string RollNo int }

type SecretStudent struct { SecretCode string ChunkSize int Length int StudentDetail Student }

func teacher(secretStudents []SecretStudent, c chan []byte) { for _, secretStudent := range secretStudents { // Modify the SecretCode chunks := chunkString(secretStudent.SecretCode, secretStudent.ChunkSize) secretStudent.SecretCode = reverseChunks(chunks)

// Convert struct to byte array b, _ := json.Marshal(secretStudent) c <- b } close(c) }

func management(c chan []byte) { for b := range c { // Decode byte array to struct var secretStudent SecretStudent json.Unmarshal(b, &secretStudent) fmt.Printf("Received secretStudent: %v ",secretStudent) // fmt.Println("Received From teacher:", secretStudent)

// Fix the SecretCode chunks := chunkString(secretStudent.SecretCode, secretStudent.ChunkSize) secretStudent.SecretCode = reverseChunks(chunks) fmt.Println("Fix the secretcode:", secretStudent) } os.Exit(0) }

func chunkString(s string, chunkSize int) []string { var chunks []string if len(s)%chunkSize != 0 { s += strings.Repeat("0", chunkSize-len(s)%chunkSize) } for i := 0; i < len(s); i += chunkSize { chunks = append(chunks, s[i:i+chunkSize]) } return chunks }

func reverseChunks(chunks []string) string { for i := 0; i < len(chunks)/2; i++ { chunks[i], chunks[len(chunks)-i-1] = chunks[len(chunks)-i-1], chunks[i] } return strings.Join(chunks, "") }

func main() { c := make(chan []byte) // Creating the SecretStudent objects secretStudents := []SecretStudent{ { SecretCode: "abcdefghijkl", ChunkSize: 4, Length: 12, StudentDetail: Student{ Name: "Vamsi", RollNo: 1, }, }, { SecretCode: "abcdefghijkl", ChunkSize: 5, Length: 12, StudentDetail: Student{ Name: "Praveen", RollNo: 2, }, }, { SecretCode: "abcdefghijkl", ChunkSize: 3, Length: 12, StudentDetail: Student{ Name: "Sunil", RollNo: 3, }, }, } go teacher(secretStudents, c) go management(c)

// wait for the goroutines var input string fmt.Scanln(&input) }

NOTE:- I wrote this solution please modify the code as per the above requirements in question.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

Calculate Activity-Based Product Costs? LO1

Answered: 1 week ago

Question

In what ways do personal and social media change how we think?

Answered: 1 week ago

Question

How do virtual communities diff er from physical communities?

Answered: 1 week ago