Skip to content

Instantly share code, notes, and snippets.

@rasmusfaber
Created December 17, 2019 10:07
Show Gist options
  • Save rasmusfaber/82294f98979306fc2224ded75c3c9b2a to your computer and use it in GitHub Desktop.
Save rasmusfaber/82294f98979306fc2224ded75c3c9b2a to your computer and use it in GitHub Desktop.
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