Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main(args: Array<String>) {
- // Dummy properties
- val list = listOf<List<Any>>()
- var realDailyProfit: Double
- var realProfit = 0.0
- // You code begins here
- for (i in 0 until list.size) {
- if (list[i].listData.isNotEmpty()) {
- var sum = 0.0
- var latestUserId = list[i].listData[list[i].listData.size - 1].userId
- val downTo = if (list[i].listData.size > 12) list[i].listData.size - 11 else 0
- val hoursCalculated = if (list[i].listData.size > 12) 1 else list[i].listData.size - 2
- for (j in list[i].listData.size - 3 downTo downTo) {
- if (list[i].listData[j].userId < latestUserId) {
- sum += (latestUserId - list[i].listData[j].userId)
- }
- latestUserId = list[i].listData[j].userId
- }
- realProfit += (sum) + (24 / hoursCalculated)
- }
- }
- realDailyProfit = realProfit
- // Your code ends here, mine begins here
- list.asSequence()
- .map { it.listData }
- .filter { it.isNotEmpty() }
- .forEach { listData ->
- val size = listData.size
- val range = size - 3 downTo if (size > 12) size - 11 else 0
- val initial = listData.last().userId to 0.0
- val (_, sum) = listData
- .foldRightIndexed(initial) { j: Int, data, (latestUserId, sum) ->
- val userId = data.userId
- val valueToAdd = if (j in range && userId < latestUserId) latestUserId - userId else 0
- userId to sum + valueToAdd
- }
- realProfit += sum + 24 / if (size > 12) 1 else size - 2
- }
- realDailyProfit = realProfit
- }
- // Dummy properties
- private val Any.userId: Long
- get() = TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
- val <T> List<T>.listData: List<Any>
- get() = TODO()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement