Created
December 17, 2019 10:07
-
-
Save rasmusfaber/82294f98979306fc2224ded75c3c9b2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun findSubFunctions(input: List<List<String>>, maxFunctionLength: Int, maxSubFunctions: Int): List<List<String>>? { | |
if (input.isEmpty()) { | |
return emptyList() | |
} | |
if (maxSubFunctions == 0) { | |
return null | |
} | |
for (i in 1..input[0].size) { | |
val candidate = input[0].subList(0, i) | |
if (candidate.joinToString(",").length > maxFunctionLength) { | |
break; | |
} | |
val fragments = input.flatMap { it.split(candidate) }.filter { it.isNotEmpty() } | |
val res = findSubFunctions(fragments, maxFunctionLength, maxSubFunctions - 1) | |
if (res != null) { | |
return res.plusAt(0, candidate) | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment