最右-Ai小说-一面
算法题:
1.第一道
使⽤两个goroutine交替打印序列,⼀个goroutine打印数字, 另外⼀个goroutine打印字⺟, 最终效果如下:
12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
2.第二道
#### 题目描述
在日常的后端开发中,我们经常需要处理和转换从 API、数据库或消息队列中获取的复杂数据结构。这些数据通常以 `map` 或 `struct` 的形式存在,并且可能包含多层嵌套。
现在,请你手写一个 Go 函数,该函数的功能是**深度遍历**一个给定的 `map[string]interface{}`,查找并替换其中所有**数值**为 `100` 的地方,将其更新为**数值** `200`。
#### 函数签名
请实现以下函数:
Go
```
func replaceValue(data map[string]interface{})
```
#### 示例
**输入 (Input):**
假设函数接收到以下 `map`:
Go
```
// 实践中,这个 map 通常由 json.Unmarshal 生成
response := map[string]interface{}{
"id": "SN12345",
"status": 100,
"info": map[string]interface{}{
"name": "productA",
"price": 100.0,
"is_hot": true,
},
"history": []interface{}{
100,
"note_100",
map[string]interface{}{
"code": 200,
"prev_id": 100,
},
},
"extra": "some string value 100",
}
```
**期望输出 (Expected Output):**
经过函数处理后,原 `map` 应被修改为:
Go
```
// 修改后的 map
response := map[string]interface{}{
"id": "SN12345",
"status": 200, // 整数 100 -> 200
"info": map[string]interface{}{
"name": "productA",
"price": 200.0, // 浮点数 100.0 -> 200.0
"is_hot": true,
},
"history": []interface{}{
200, // 数组中的整数 100 -> 200
"note_100", // 字符串 "note_100" 不变
map[string]interface{}{
"code": 200,
"prev_id": 200, // 嵌套 map 中的整数 100 -> 200
},
},
"extra": "some string value 100", // 字符串 "some string value 100" 不变
}
```
#### 要求与提示
1. 函数应**原地修改**(in-place)输入的 `map`,不需要返回值。
2. 数据结构中可能包含任意层级的 `map[string]interface{}` 和 `[]interface{}`。
3. **请特别注意**:当一个 JSON 对象被反序列化到 `interface{}` 中时,其数字类型在 Go 中通常会被解析成什么具体类型。
4. 只有**数值**等于 `100` 的需要被替换,字符串或其他类型的值(如 `"100"`)不应被修改。
1.第一道
使⽤两个goroutine交替打印序列,⼀个goroutine打印数字, 另外⼀个goroutine打印字⺟, 最终效果如下:
12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
2.第二道
#### 题目描述
在日常的后端开发中,我们经常需要处理和转换从 API、数据库或消息队列中获取的复杂数据结构。这些数据通常以 `map` 或 `struct` 的形式存在,并且可能包含多层嵌套。
现在,请你手写一个 Go 函数,该函数的功能是**深度遍历**一个给定的 `map[string]interface{}`,查找并替换其中所有**数值**为 `100` 的地方,将其更新为**数值** `200`。
#### 函数签名
请实现以下函数:
Go
```
func replaceValue(data map[string]interface{})
```
#### 示例
**输入 (Input):**
假设函数接收到以下 `map`:
Go
```
// 实践中,这个 map 通常由 json.Unmarshal 生成
response := map[string]interface{}{
"id": "SN12345",
"status": 100,
"info": map[string]interface{}{
"name": "productA",
"price": 100.0,
"is_hot": true,
},
"history": []interface{}{
100,
"note_100",
map[string]interface{}{
"code": 200,
"prev_id": 100,
},
},
"extra": "some string value 100",
}
```
**期望输出 (Expected Output):**
经过函数处理后,原 `map` 应被修改为:
Go
```
// 修改后的 map
response := map[string]interface{}{
"id": "SN12345",
"status": 200, // 整数 100 -> 200
"info": map[string]interface{}{
"name": "productA",
"price": 200.0, // 浮点数 100.0 -> 200.0
"is_hot": true,
},
"history": []interface{}{
200, // 数组中的整数 100 -> 200
"note_100", // 字符串 "note_100" 不变
map[string]interface{}{
"code": 200,
"prev_id": 200, // 嵌套 map 中的整数 100 -> 200
},
},
"extra": "some string value 100", // 字符串 "some string value 100" 不变
}
```
#### 要求与提示
1. 函数应**原地修改**(in-place)输入的 `map`,不需要返回值。
2. 数据结构中可能包含任意层级的 `map[string]interface{}` 和 `[]interface{}`。
3. **请特别注意**:当一个 JSON 对象被反序列化到 `interface{}` 中时,其数字类型在 Go 中通常会被解析成什么具体类型。
4. 只有**数值**等于 `100` 的需要被替换,字符串或其他类型的值(如 `"100"`)不应被修改。
全部评论
相关推荐
急跑考拉:方便透露小红书后端今年薪资范围么
点赞 评论 收藏
分享

点赞 评论 收藏
分享