karelpeeters
02/06/2019, 10:13 PMtoReplace in list and replace it with replacement? Because the code doesn't require an exact match right now.Daniel
02/06/2019, 10:19 PMtoReplace is part of the listkarelpeeters
02/06/2019, 10:21 PMreplaceInList(listOf(1,2,1,5,2), listOf(1,5,2), listOf(6))Daniel
02/06/2019, 10:22 PMkarelpeeters
02/06/2019, 10:23 PMDaniel
02/06/2019, 10:26 PM1, 2, then + 6 and the last sublist, yeah would throw an index out of bounds exception, I simplified a bit too much, it seems. Thank you for your reply!karelpeeters
02/06/2019, 10:27 PMDaniel
02/06/2019, 10:30 PMlistOf(1,2,3,4) replace listOf(2,3) with listOf(6,7,8) or listOf(6) and should end up with listOf(1,6,7,8,4) or listOf(1,6,4) so any replacement should be possible.
For the toReplace you can be sure that it is part of the listkarelpeeters
02/06/2019, 10:32 PMtoReplace in list because it only checks the first and last positions. The size of toReplace and the matched part of list don't even have to match.Daniel
02/06/2019, 10:54 PMnkiesel
02/07/2019, 12:43 AM[1,1,1,2,2,2] and you want to replace [1,2] with [3]. I think you want [1,1,3,2,2] but your code would produce [3,2,2] instead. But even that is not good enough because replaceInList(listOf(1,2,3,4,5), listOf(4,2), listOf(0)) would produce [1,2,3,0,3,4,5] and replaceInList(listOf(1,2,3,4,5),listOf(1,5),listOf(0)) would produce [0]nkiesel
02/07/2019, 1:35 AMfun replaceInList(list: List<Int>, toReplace: List<Int>, replacement: List<Int>): List<Int> {
val i = list.windowed(toReplace.size).indexOfFirst { it == toReplace }
return if (i == -1) list else list.take(i) + replacement + list.drop(i + toReplace.size)
}