Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
median calculation in kotlin

I wrote this code to calculate median range but I want to give every column and row a specific name like:

Columns A B C D
Rows 1 2 3

I have to calculate the median of a range like in excel A2:C3 so the program can calculate A2 A3 B2 B3 C2 C3 and if it's B3:D3 I want it to calculate B3 C3 D3.

This is my code:

fun med(list: List<Double>) = list.sorted().let { 
    (it[it.size / 2] + it[(it.size - 1) / 2]) / 2 
}

fun main(args: Array<String>) {
    med(listOf(1.5, 2.67, 3.0, 1.4)).let { println(it) }
    med(listOf(5.2, 7.1, -4.8, 0.0)).let { println(it) }
    med(listOf(1.4, 6.0, 2.5, -1.9)).let { println(it) }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should check the length of a list and return the middle element if the length is odd:

fun med(list: List<Double>) = list.sorted().let {
    if (it.size % 2 == 0)
        (it[it.size / 2] + it[(it.size - 1) / 2]) / 2
    else
        it[it.size / 2]
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!