Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.58 KB | None | 0 0
  1. fun main(args: Array<String>) {
  2.                     // Dummy properties
  3.                     val list = listOf<List<Any>>()
  4.                     var realDailyProfit: Double
  5.                     var realProfit = 0.0
  6.  
  7.                     // You code begins here
  8.  
  9.                     for (i in 0 until list.size) {
  10.                         if (list[i].listData.isNotEmpty()) {
  11.                             var sum = 0.0
  12.                             var latestUserId = list[i].listData[list[i].listData.size - 1].userId
  13.                             val downTo = if (list[i].listData.size > 12) list[i].listData.size - 11 else 0
  14.                             val hoursCalculated = if (list[i].listData.size > 12) 1 else list[i].listData.size - 2
  15.  
  16.                             for (j in list[i].listData.size - 3 downTo downTo) {
  17.                                 if (list[i].listData[j].userId < latestUserId) {
  18.                                     sum += (latestUserId - list[i].listData[j].userId)
  19.                                 }
  20.                                 latestUserId = list[i].listData[j].userId
  21.                             }
  22.                             realProfit += (sum) + (24 / hoursCalculated)
  23.                         }
  24.                     }
  25.  
  26.                     realDailyProfit = realProfit
  27.  
  28.                     // Your code ends here, mine begins here
  29.                     list.asSequence()
  30.                             .map { it.listData }
  31.                             .filter { it.isNotEmpty() }
  32.                             .forEach { listData ->
  33.                                 val size = listData.size
  34.                                 val range = size - 3 downTo if (size > 12) size - 11 else 0
  35.                                 val initial = listData.last().userId to 0.0
  36.                                 val (_, sum) = listData
  37.                                         .foldRightIndexed(initial) { j: Int, data, (latestUserId, sum) ->
  38.                                             val userId = data.userId
  39.                                             val valueToAdd = if (j in range && userId < latestUserId) latestUserId - userId else 0
  40.                                             userId to sum + valueToAdd
  41.                                         }
  42.  
  43.                 realProfit += sum + 24 / if (size > 12) 1 else size - 2
  44.             }
  45.  
  46.     realDailyProfit = realProfit
  47. }
  48.  
  49. // Dummy properties
  50. private val Any.userId: Long
  51.     get() = TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  52.  
  53. val <T> List<T>.listData: List<Any>
  54.     get() = TODO()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement