当前位置:网站首页>golang Make a list of intervals with sequential numbers
golang Make a list of intervals with sequential numbers
2022-06-25 21:59:00 【大魔法师云中君】
package main
import (
"log"
"sort"
"strconv"
)
func main() {
a := []int{
1, 2, 3, 9, 8, 7, 6, 11}
// need sorted slice
sort.Slice(a, func(i, j int) bool {
return a[i] < a[j]
})
log.Println(a)
var result []string
rangeStart, previousNumber := a[0], a[0]
tmp := ""
for _, number := range a[1:] {
log.Println(number)
if number == previousNumber+1 {
previousNumber = number
} else {
if rangeStart == previousNumber {
tmp = strconv.Itoa(rangeStart)
} else {
tmp = strconv.Itoa(rangeStart) + "-" + strconv.Itoa(previousNumber)
}
result = append(result, tmp)
rangeStart, previousNumber = number, number
}
}
if rangeStart == previousNumber {
tmp = strconv.Itoa(rangeStart)
} else {
tmp = strconv.Itoa(rangeStart) + "-" + strconv.Itoa(previousNumber)
}
result = append(result, tmp)
log.Println(result)
}
边栏推荐
- ES6学习-- LET
- Paper notes: multi tag learning MSWl
- Transformers load pre training model
- Xampp重启后,MySQL服务就启动不了。
- UE4_UE5结合offline voice recognition插件做语音识别功能
- Es6-- set
- Meta universe standard forum established
- STM32 development board + smart cloud aiot+ home monitoring and control system
- Why is BeanUtils not recommended?
- My C language learning process
猜你喜欢

问题记录与思考

Use and difference between ue4\ue5 blueprint node delay and retroggable delay

ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function

What aspects should we start with in the feasibility analysis of dry goods?

Meta universe standard forum established

首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千

Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert

Live800 online customer service system: do business across time and space, starting from each interaction

元宇宙标准论坛成立

LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block
随机推荐
OpenJudge NOI 2.1 15:Counterfeit Dollar
MySQL数据库常用函数和查询
Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
NLP text summary: use the pre training model to perform text summary tasks [transformers:pipeline, T5, Bart, Pegasus]
New network security competition of the secondary vocational group in 2022
oracle -- 表操作
How to download the software package of CDH version
22 years of a doctor in Huawei
ES6 --- 数值扩展、对象拓展
Fegin client entry test
My C language learning process
2、一个向量乘它的转置,其几何意义是什么?
Idea FAQ collection
Svn icon disappearing solution
使用EAST ocr遇到的坑解决方法(编译lanms)
干货丨产品的可行性分析要从哪几个方面入手?
What is Unified Extensible Firmware Interface (UEFI)?
一位博士在华为的22年
Problem recording and thinking
做接口测试,这3种工具到底什么时候用?