当前位置:网站首页>Bedtime story | made a Bitmap and AST length system configuration
Bedtime story | made a Bitmap and AST length system configuration
2022-08-10 21:14:00 【The user 9959267】
“This article is explained by way of storytellingbitmap&AST的一个应用场景.This story takes place in an online game company,Main characters 左边:导师,名字不重要 右边:实习生,三多
Sanduo works as an intern in a game company,He was paddling one afternoon,Was called by the instructor:
Sanduo cursed in his heart,But the thought of myself having to pay rent again next month,Still full of tears, I opened the requirements document,The general meaning of the requirements document is as follows:
Sanduo began to brush his hair at the same time,Design the system on one side,干到晚上12点,Developed the first version of the system architecture diagram:
其中DB表的schema为:
字段名 | 类型 | 含义 |
---|---|---|
id | bigint | 自增主键 |
user_id | bigint | 用户id |
stat_date | date | 统计日期 |
online_duration | int | The total amount of time the user has been online |
fight_duration | int | The total time of the user fighting monsters |
pet_duration | int | Total time spent with pets |
deal_duration | int | Total transaction time |
mining_duration | int | Total mining time |
fight_hour_0 | int | 0-1The total time to hit monsters |
fight_hour_1 | int | 1-2The total time to hit monsters |
... | ||
fight_hour_23 | int | 23-24The total time to hit monsters |
pet_hour_0 | int | 0-1Total time spent on pets |
pet_hour_1 | int | 1-2Total time spent on pets |
... | ||
pet_hour_23 | Int | 23-24点1-2Total time spent on pets |
... | Other duration information is similar,忽略 |
So Sanduo showed the design to the instructor the next day:
So Sanduo redesigned the technical solution:
After Sanduo designed the technical scheme,就开始写代码,然后项目上线,一切顺利,直到几天后...
Sanduo's tutor just picked up the guy and drew a picture and then threw it to Sanduo for him to realize:
So Sanduo realized this time-length system,Because the system capability is very flexible,老板让PMSome very complex duration expressions are configured,It was given a friendly title by the players:monkey company~
The following code is aAST的最小demo,有兴趣的读者可以看看:
package main
import (
"fmt"
"reflect"
"strconv"
"strings"
)
const (
Number = 0
Operator = 1
)
type Node struct {
Type int
Value string
Left *Node
Right *Node
}
// input: 1 + 4 - 2
// result:
// -
// / \
// + 2
// / \
// 1 4
func getAst(expr string) *Node {
operator := make(map[string]int)
operator["+"] = Operator
operator["-"] = Operator
nodeList := make([]Node, 0)
var root *Node
expr = strings.Trim(expr, " ")
words := strings.Split(expr, " ")
for _, word := range words {
var node Node
if _, ok := operator[word]; ok {
node.Type = Operator
} else {
node.Type = Number
}
node.Value = word
nodeList = append(nodeList, node)
}
for i := 0; i < len(nodeList); i++ {
if root == nil {
root = &nodeList[i]
continue
}
switch nodeList[i].Type {
case Operator:
nodeList[i].Left = root
root = &nodeList[i]
case Number:
root.Right = &nodeList[i]
}
}
return root
}
func getResult(node *Node) string {
switch node.Type {
case Number:
return node.Value
case Operator:
return calc(getResult(node.Left), getResult(node.Right), node.Value)
}
return ""
}
func calc(left, right string, operator string) string {
leftVal, _ := TransToInt(left)
rightVal, _ := TransToInt(right)
val := 0
switch operator {
case "+":
val = leftVal + rightVal
case "-":
val = leftVal - rightVal
}
return TransToString(val)
}
func main() {
expr := `1 + 4 - 2 + 100 - 20 + 12 `
//expr := ` 1 + 4 `
ast := getAst(expr)
result := getResult(ast)
fmt.Println(result)
}
func TransToString(data interface{}) (res string) {
val := reflect.ValueOf(data)
return strconv.FormatInt(val.Int(), 10)
}
func TransToInt(data interface{}) (res int, err error) {
return strconv.Atoi(strings.TrimSpace(data.(string)))
}
边栏推荐
猜你喜欢
Detailed explanation and use of each module of ansible
数字化转型:如何引导创新领导者
组合导航精度分析
【nvm】【node多版本管理工具】使用说明和踩坑(exit status 1)
XML小讲
2019河北省大学生程序设计竞赛部分题题解
【实用软件】【VSCode】使用技巧大全(持续更新)
链表应用----约瑟夫问题
ansible各个模块的详解和使用
- [email protected] nanomimetic e"/>
Water-soluble alloy quantum dot nanozymes|CuMoS nanozymes|porous silicon-based Pt(Au) nanozymes|[email protected] nanomimetic e
随机推荐
TortoiseSVN小乌龟的使用
Oracle 的开窗函数使用详解(二)
【语义分割】2016-SegNet TPAMI
Future-oriented IT infrastructure management architecture - Unified IaaS
Web3中值得关注的基础设施
组合导航精度分析
什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
[mysql] 深入分析MySQL版本控制MVCC规则
UE4 - 河流流体插件Fluid Flux
Detailed explanation and use of each module of ansible
In 2021 China industrial Internet security competition (competition) in fujian province and the first industry of fujian province Internet innovation competition
Introduction to PostgreSQL
金鱼哥RHCA回忆录:CL210OpenStack操作的故障排除--章节实验
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
C 语言 时间函数使用技巧(汇总)
突破次元壁垒,让身边的玩偶手办在屏幕上动起来!
链表应用----约瑟夫问题
【golang map】 深入了解map内部存储协议
【图像分类】2018-MobileNetV2