Sobes.tech
Junior — Middle+

Calculating the final widths of child elements within a container

livecode

Task condition

Given:

  • parentWidth — the total width of the parent container.
  • childSpecs — a list of width specifications for child Views, represented as integers, where:
    • If childSpecs[i] ≥ 0, then the corresponding View has a fixed width equal to this value.
    • If childSpecs[i] < 0, then it is a coefficient that determines the share of the remaining free space after placing all fixed-width Views, which is parentWidth. It is necessary to calculate the final widths of all Views. Example: measureWidths(100, listOf(50, -3, -2)) should return listOf(50, 30, 20).
fun measureWidths(parentWidth: Int, childSpecs: List<Int>): List<Int> {
 // your code
}