当前位置:网站首页>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)))
}
边栏推荐
- 2021 GKCTF X DASCTF应急挑战杯
- 什么是抽象类?什么时候用?什么是接口?抽象类与接口的区别?
- win7开机有画面进系统黑屏怎么办
- leetcode:45. 跳跃游戏II
- Implementation of graceful exit in Golang
- 壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
- ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
- PostgreSQL 介绍
- 如何提高代码的可读性 学习笔记
- In 2021 China industrial Internet security competition (competition) in fujian province and the first industry of fujian province Internet innovation competition
猜你喜欢

【图像分类】2017-MobileNetV1 CVPR

图扑智慧电力可视化大屏,赋能虚拟电厂精准减碳

链表应用----约瑟夫问题

双 TL431 级联振荡器

leetcode:45. 跳跃游戏II

Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs

三子棋的设计和代码

关于 NFT 版权保护的争议

apr_thread使用内存之谜
[email prot"/>Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
随机推荐
Detailed explanation and use of each module of ansible
找的笔试题的复盘(一)
根心与根轴
win10 xbox录屏功能不能录声音怎么办
How to submit a PR?【OpenHarmony Growth Plan】【OpenHarmony Open Source Community】
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
idea插件 协议 。。 公司申请软件用
【go】依赖注入
【语义分割】2017-PSPNet CVPR
leetcode:45. 跳跃游戏II
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
Go程序员进化史
社区分享|货拉拉通过JumpServer纳管大规模云上资产
Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
详叙c中的分支与循环
(10) Sequence and deserialization of image data
2021 GKCTF X DASCTF应急挑战杯
(十二)STM32——NVIC中断优先级管理
内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布