gpt4 book ai didi

java - 从逗号分隔整数的字符串数组映射的连续天数

转载 作者:行者123 更新时间:2023-12-02 12:46:30 24 4
gpt4 key购买 nike

我收到了多个以逗号分隔的字符串整数作为输入,例如,以下字符串:

  1. “5,6,0”
  2. “0,1,2”
  3. “1,2,3,4”

这些整数中的每一个都代表一周中的一天

  • 0 = 周日 1 = 周一 2 = 周二 3 = 周三 4 = 周四 5 =星期五 6 = 星期六

对于第一个字符串,这意味着星期四星期日第二个字符串从星期日星期二有效第三个字符串从星期一星期四

有效

目前,我正在使用以下

  private fun mapOfDays(validDays: String): LinkedHashMap<Int, String>
{
if (!validDays.isBlank())
{
val daysArray = validDays.split("\\s*,\\s*") as Array<String>
var mapDays = LinkedHashMap<Int, String>()
var mapDay = LinkedHashMap<Int, String>()
mapDays[0] = "SUNDAY"
mapDays[1] = "MONDAY"
mapDays[2] = "TUESDAY"
mapDays[3] = "WEDNESDAY"
mapDays[4] = "THURSDAY"
mapDays[5] = "FRIDAY"
mapDays[6] = "SATURDAY"

for (day in daysArray)
{
if (mapDays.containsKey(day.toInt()))
{
mapDay[day.toInt()] = mapDays[day.toInt()]!!
}
}
return mapDay
}
return LinkedHashMap()
}

private fun mappedDays(mapOfDays: LinkedHashMap<Int, String>?): String
{
if (!mapOfDays.isNullOrEmpty())
{
val mapSize = mapOfDays.size

if (mapSize > 6) return "All Day"
if (mapSize > 5) return sixDayString(mapOfDays)
if (mapSize > 4) return fiveDayString(mapOfDays)
if (mapSize > 3) return fourDayString(mapOfDays)
if (mapSize > 2) return threeDayString(mapOfDays)
if (mapSize > 1) return twoDayString(mapOfDays)
if (mapSize > 0) return oneDayString(mapOfDays)
}
return ""
}

private fun twoDayString(mapOfDays: LinkedHashMap<Int, String>): String
{
val firstPosition: Int = mapOfDays.keys.toIntArray()[0]
val lastPosition: Int = mapOfDays.keys.toIntArray()[1]

val lastDay = Days.values()[lastPosition]
val firstDay = Days.values()[firstPosition]

return "$firstDay and $lastDay"
}

private fun oneDayString(mapOfDays: LinkedHashMap<Int, String>): String
{
var firstPosition: Int = mapOfDays.keys.toIntArray()[0]
val firstDay = Days.values()[firstPosition]
return "$firstDay"
}

private fun threeDayString(mapOfDays: LinkedHashMap<Int, String>): String
{
val firstPosition: Int = mapOfDays.keys.toIntArray()[0]
val secondPosition: Int = mapOfDays.keys.toIntArray()[1]
val thirdPosition: Int = mapOfDays.keys.toIntArray()[2]

val firstDay = Days.values()[firstPosition]
val secondDay = Days.values()[secondPosition]
val lastDay = Days.values()[thirdPosition]
return "$firstDay, $secondDay and $lastDay"
}

private fun fourDayString(mapOfDays: LinkedHashMap<Int, String>): String
{
val firstPosition: Int = mapOfDays.keys.toIntArray()[0]
val secondPosition: Int = mapOfDays.keys.toIntArray()[1]
val thirdPosition: Int = mapOfDays.keys.toIntArray()[2]
val fourthPosition: Int = mapOfDays.keys.toIntArray()[3]

val firstDay = Days.values()[firstPosition]
val secondDay = Days.values()[secondPosition]
val thirdDay = Days.values()[thirdPosition]
val lastDay = Days.values()[fourthPosition]
return "$firstDay, $secondDay, $thirdDay and $lastDay"
}

private fun fiveDayString(mapOfDays: LinkedHashMap<Int, String>): String
{
val firstPosition: Int = mapOfDays.keys.toIntArray()[0]
val secondPosition: Int = mapOfDays.keys.toIntArray()[1]
val thirdPosition: Int = mapOfDays.keys.toIntArray()[2]
val fourthPosition: Int = mapOfDays.keys.toIntArray()[3]
val fifthPosition: Int = mapOfDays.keys.toIntArray()[4]

val firstDay = Days.values()[firstPosition]
val secondDay = Days.values()[secondPosition]
val thirdDay = Days.values()[thirdPosition]
val fourthDay = Days.values()[fourthPosition]
val lastDay = Days.values()[fifthPosition]
return "$firstDay, $secondDay, $thirdDay, $fourthDay and $lastDay"
}

private fun sixDayString(mapOfDays: LinkedHashMap<Int, String>): String
{
var firstPosition: Int = mapOfDays.keys.toIntArray()[0]
var lastPosition: Int = 0

for (day in mapOfDays.keys)
{
lastPosition = day
}

val lastDay = Days.values()[lastPosition]
val firstDay = Days.values()[firstPosition]

return "$firstDay to $lastDay"
}

}

enum class Days()
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

但是,我当前的实现能够告诉我包括哪些天,但无法映射出天数组,例如:

如果我得到“0,1,3,4,5,6”,我想要的最终字符串输出如下:Wednesday to Monday

“0,1,3,4,5”将导致以下结果:星期日星期一星期三星期五

最佳答案

package days

import java.lang.IllegalArgumentException


class DaysFactory {
fun dayFromInt(index: Int): Day {
return when (index) {
0 -> Day.Sunday
1 -> Day.Monday
2 -> Day.Tuesday
3 -> Day.Wednesday
4 -> Day.Thursday
5 -> Day.Friday
6 -> Day.Saturday
else -> throw IllegalArgumentException("illigal index :$index")
}
}

enum class Day(val index: Int) {
Sunday(0), Monday(1), Tuesday(2), Wednesday(3), Thursday(4), Friday(5), Saturday(6)
}
}


class DaysRange(val seed: String) {

var stringFormat = ""

private fun getTomorrow(dayIndex: Int): Int {
if (dayIndex != 6) return dayIndex + 1
return 0
}

override fun toString(): String =stringFormat


init {
if (isValidInput(seed)) {
val dayFactory = DaysFactory()
val indexes = seed.split(",").map { it.toInt() }
val days = indexes.map { dayFactory.dayFromInt(it) }
val ranges = splitIndexesToRanges(indexes)
ranges.forEach { range ->
if (range.size > 2) {
stringFormat += "${dayFactory.dayFromInt(range.first())} to ${dayFactory.dayFromInt(range.last())},"
} else
range.forEach {
stringFormat += "${dayFactory.dayFromInt(it)},"
}
}
stringFormat = stringFormat.dropLast(1)
}
}

private fun splitIndexesToRanges(daysRange: List<Int>): ArrayList<List<Int>> {
val result = ArrayList<List<Int>>()
val slicePoint = ArrayList<Int>()
for (i in 0 until daysRange.size - 1) {
if (getTomorrow(daysRange[i]) != daysRange[i + 1]) {
slicePoint.add(i)
}
}

var start = 0
slicePoint.forEach {
result.add(daysRange.slice(start..it))
start = it + 1
}
result.add(daysRange.slice(start until daysRange.size))
return result

}

}

private fun isValidInput(seed: String): Boolean = true


fun main(args: Array<String>) {

val input = listOf(
"0,1,2,4,5,6",
"5,6,0",
"1,2,3,4"
)

input.forEach {
val dr = DaysRange(it)
println(dr)
}
}

示例输出:

周日到周二,周四到周六

周五到周日

周一到周四

关于java - 从逗号分隔整数的字符串数组映射的连续天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354592/

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