# Non-Programmer Welcome- Sum, Average and Other Data Set Fun With Kotlin

Jul 8, 2020 · 3 min read

Given a list of Employees, find the average and sum of salary for each department.

You don’t have to be a Kotlin-programmer or any programmer to complete the following 4 tasks, just a curious mind and a love for numbers will do

Each Employee has the following properties:

• Name
• Department
• Salary
• Title

Does that sound familiar? I’m sure it does to all of us who have been through the 101 tutorial for Excel and SQL.

Buckled up as today we are going to learn how to achieve the same tasks with Kotlin aka. one of the most well-rounded and loved programming language of our era!

# Preparation

Our data set is here (salary, name, department and title are random)

The code to generate the `.csv` data is here. You can easily modify it to generate more than 12 employees for example. The best part is you can run and have fun instantly without installing anything!

# Task 0 — Total sum

Get the total salary amount of the whole list

Outputs

`total salary: 822284`

# Task 1 — Group member counts

Count the number of employees in each department

Outputs

`there are 3 employees for department Engineeringthere are 1 employees for department Otherthere are 4 employees for department UXthere are 3 employees for department Marketingthere are 1 employees for department Finance`

# Task 2 — Groups’ sums

Get the list of departments and the total salary amount for each of them

Outputs

`Total salary for department Engineering is 252238Total salary for department Other is 58611Total salary for department UX is 248359Total salary for department Marketing is 186886Total salary for department Finance is 76190`

Behind the scene, `.fold()` uses `.aggregate()` so alternatively, the same result can be achieved by:

# Task 3 — Groups’ averages

Get the list of departments and the average salary for each of them

outputs

`Average salary for department Engineering is 84079Average salary for department Other is 58611Average salary for department UX is 62089Average salary for department Marketing is 62295Average salary for department Finance is 76190`

If the current average of K employees is `kAverage`, the new average when process the K+1th employee with salary `kPlusOneSalary` will be

kPlusOneAverage = kAverage + ((kAverage — kPlusOneSalary)/K+1)

There’s a bug currently. Can you spot it?

# Live Action

All of the above tasks can be found here, feel free to run, modify and run again for a maximum amount of fun :)

Ask me questions and let me know once and I might give you a certificate ;)

The following sections are intended for programmers only

# Performance

All of the aforementioned functions have O(N) time and space (`fold()` and `aggregate()` use a Map of “group name/id” to group/sub-list of items) complexity. There’s no performance magic behind them. They are just convenient and fun to use :)

If you want to process millions of records or more, your data might not fit inside the memory. You’d need to process the values in a stream, one at a time.

# Next part

Sum and average for large collections of data with RxJava and perhaps Kotlin Sequence

## The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

### By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

Medium sent you an email at to complete your subscription.

Written by

## Eric N

Hiring Android engineer shorturl.at/bivJO Clean coder, walker & biker. Hater of inefficiency

## The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Written by

## Eric N

Hiring Android engineer shorturl.at/bivJO Clean coder, walker & biker. Hater of inefficiency

## The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

## A call for sustainability on the web.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app