Element assignment

Report a typo

Make a copy of the s slice, then assign x to the element at the i index, and return the copied slice from the solve function.

This problem uses the reflect package to check the contents of the newSlice variable and the bufio package to read a space-separated string from the stdin. However, don't be scared! to solve this task, it is not necessary for you to know how the reflect or the bufio packages work.

Sample Input 1:

my string value

Sample Output 1:

[my string value]
Write a program in Go
package main

import (
"bufio"
"fmt"
"log"
"os"
"reflect"
)

func solve(s []string, x string, i int) []string {
// Write the code to make a copy of the `s` slice below:
sliceCopy := ?
?(?, ?)

// Assign `x` to the element at the `i` index of the `sliceCopy` slice:
?[?] = ?

return ? // Return `sliceCopy` here!
}

// DO NOT delete or modify the contents of the main function!
func main() {
var i int
var x string

mySlice := make([]string, 1)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
mySlice[i], x = scanner.Text(), scanner.Text()

newSlice := solve(mySlice, x, i)
if !reflect.DeepEqual(mySlice, newSlice) {
log.Fatal("You didn't assign `x` to the element at the `i` index of the slice!")
}
fmt.Println(newSlice)
___

Create a free account to access the full topic