当前位置:网站首页>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)))
}
边栏推荐
猜你喜欢
Demis Hassabis:AI 的强大,超乎我们的想象
QSslSocket has not been declared
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
TortoiseSVN小乌龟的使用
Knowledge map Knowledge Graph
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme | Palladium Nanozyme | GMP-Pd Nanozyme | Gold-Palladium Composite Nanozyme | Ternary Metal Pd-M-Ir Nanozyme |shell nanozyme
参天生长大模型:昇腾AI如何强壮模型开发与创新之根?
Apache DolphinScheduler 3.0.0 正式版发布!
详叙c中的分支与循环
随机推荐
【一致性hash】负载均衡器分发请求
2021 CybricsCTF
论文解读(g-U-Nets)《Graph U-Nets》
根心与根轴
Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
【go】依赖注入
Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme | Palladium Nanozyme | GMP-Pd Nanozyme | Gold-Palladium Composite Nanozyme | Ternary Metal Pd-M-Ir Nanozyme |shell nanozyme
Are you hungry - Institution tree radio
Auto.js找图找色常用功能
爱丁堡大学最新《因果机器学习: 医疗健康与精准医疗应用》2022综述
[SemiDrive source code analysis] [MailBox inter-core communication] 51 - DCF_IPCC_Property implementation principle analysis and code combat
JS中的filter、map、reduce
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
Date picker component (restrict year to set only displayed months)
测试开发【Mock 平台】08 开发:项目管理(四)编辑功能和Component抽离
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
C语言详解系列——关于调试那些事
"POJ 3666" Making the Grade problem solution (two methods)
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
将视图模型转换为使用 Hilt 依赖注入