Skip to content

Instantly share code, notes, and snippets.

@dairdr
Created February 1, 2019 14:17
Show Gist options
  • Save dairdr/aa710e9344b2a418ae8cf6039561d2f3 to your computer and use it in GitHub Desktop.
Save dairdr/aa710e9344b2a418ae8cf6039561d2f3 to your computer and use it in GitHub Desktop.
import kotlin.math.ceil
fun friendly(a: Int, b: Int) : Boolean {
val evenA: MutableList<Int> = mutableListOf()
val evenB: MutableList<Int> = mutableListOf()
for (n in 1..a) {
if (a % n == 0 && n != a) {
evenA.add(n)
}
}
for (n in 1..b) {
if (b % n == 0 && n != b) {
evenB.add(n)
}
}
return evenA.sum() == b && evenB.sum() == a
}
fun prices(values: List<Float>) {
val minimumPrice = values.min()
val maximimPrice = values.max()
val dayToBuy = values.indexOf(minimumPrice)
val dayToSell = values.indexOf(maximimPrice)
println("day to buy $dayToBuy")
println("day to sell $dayToSell")
}
fun replace(value: String) {
val grouped = value.groupingBy { it }
.eachCount()
.filter {
it.value > 1
}
var finalString = value
grouped.entries.forEach {
val exp = it.key.toString().repeat(it.value)
finalString = finalString.replace(exp.toRegex(), "${it.value}${it.key}")
}
println(finalString)
}
fun main(args: Array<String>) {
println(friendly(220, 285))
prices(listOf(1f, 2f, 1f, 5f, 2.1f, 6f, 3f))
replace("ABBBBCFLL")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment