gpt4 book ai didi

java - 如何在 Java/Groovy 中获得最长的连续日和平均日?

转载 作者:行者123 更新时间:2023-11-30 08:55:55 25 4
gpt4 key购买 nike

我在根据给定 map 计算最长连续天数和平均天数时遇到问题>。

代码片段如下:

def testData = [:] as Map<Long, List<String>>;
testData[1L] = [
"2015-01-02", "2015-01-03", "2015-01-04", "2015-01-08", "2015-01-09",
"2015-01-11", "2015-01-12", "2015-01-13", "2015-01-17"];
testData[2L] = [
"2015-01-01", "2015-01-03", "2015-01-04", "2015-01-08",
"2015-01-10", "2015-01-13", "2015-01-14"];

List results = [];
testData.each { kv ->

// something to do here too..
kv.value.each { dateInString ->
// 1. convert string to date
// 2. calculate average days
// 3. calculate longest streak
}

results << [
key: kv.key,
longestStreak: 0 + " days",
longestStreakDates: [
[startDate: "", endDate: ""], [more..]
],
averageDay: 0 + " days"
]
}

results.each { it ->
println "${it.key} | Longest streak: ${it.longestStreak} | Average Day: ${it.averageDay}";
println "Longest streak dates:";
it.longestStreakDates.eachWithIndex { dateStreak, index ->
println "\t\t${index}. ${dateStreak.startDate} - ${dateStreak.endDate}";
}
}

first 键 ID 的平均天数计算: 1. 平均天数:(3 天 + 2 天 + 3 天 + 1 天)/4(连续天数)= 9 天/4 = 2.25 天

second 键 ID 的平均天数计算:2。平均天数:(1 天 + 2 天 + 1 天 + 1 天 + 2 天)/5(连续天数)= 7天/5 = 1.4 天

Longest Streak计算请看http://www.quora.com/What-is-your-longest-streak-on-GitHub

上面的代码片段最终会打印成这样:

1 | Longest streak: 3 days | Average Day: 2.25 days.
Longest streak dates:
1. 2015-01-02 - 2015-01-04
2 | Longest streak: 2 days | Average Day: 1.4 days.
Longest streak dates:
1. 2015-01-03 - 2015-01-04
2. 2015-01-13 - 2015-01-14

谁能帮我在每个循环“这里也要做点什么……”中实现逻辑?我使用的数据模型是否过于复杂或难以实现?如果有人有更好、更有效的方法来解决这个问题,请告诉我。

此外,除了“平均”计算,我可能还需要“众数”和“中位数”计算。

谢谢。

最佳答案

给你:

import java.util.Calendar

def testData = [
1L:[
"2015-01-02", "2015-01-03", "2015-01-04", "2015-01-08", "2015-01-09",
"2015-01-11", "2015-01-12", "2015-01-13", "2015-01-17"
],
2L:[
"2015-01-01", "2015-01-03", "2015-01-04", "2015-01-08",
"2015-01-10", "2015-01-13", "2015-01-14"
]
]
testData.each { k, v ->
def daysOfYear = v.collect { d -> Date.parse('yyyy-MM-dd', d) }.sort().collect { it[Calendar.DAY_OF_YEAR] }
//println(daysOfYear)
def streaks = [:]
def streakIdx = 0
def streakCnt = 1
for(int i = 0; i < daysOfYear.size(); i++) {
if(daysOfYear[i] + 1 == daysOfYear[i+1]) {
streakCnt++
} else {
streaks[streakIdx++] = streakCnt
streakCnt = 1
}
}
def max = streaks.values().max()
def avg = streaks.values().sum() / streaks.values().size()
println("Key: $k, max: $max, avg: $avg")
}

关于java - 如何在 Java/Groovy 中获得最长的连续日和平均日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com